fork(1) download
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. //_ ************************** Advanced PBDS ***********************************
  5.  
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. template<class T>
  13. using ordered_set = tree<
  14. T,
  15. null_type,
  16. less<T>,
  17. rb_tree_tag,
  18. tree_order_statistics_node_update
  19. >;
  20.  
  21.  
  22. //_ ****************************************************************************
  23.  
  24.  
  25. #define int long long int
  26. #define double long double
  27. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  28.  
  29.  
  30. const int M = 1000000007;
  31. const int N = 3e5+9;
  32. const int INF = 2e9+1;
  33. const int LINF = 2000000000000000001;
  34.  
  35. inline int power(int a, int b, int mod=M) {
  36. int x = 1;
  37. a %= mod;
  38. while (b) {
  39. if (b & 1) x = (x * a) % mod;
  40. a = (a * a) % mod;
  41. b >>= 1;
  42. }
  43. return x;
  44. }
  45.  
  46.  
  47. //_ ***************************** START Below *******************************
  48.  
  49.  
  50.  
  51.  
  52. void solve() {
  53.  
  54. ordered_set<int> s;
  55.  
  56. for (auto x : {10LL, 5LL, 20LL, 15LL, 8LL, 2LL})
  57. s.insert(x);
  58.  
  59. int n = s.size();
  60.  
  61. cout << "Index : ";
  62. for (int i=0; i<=n; i++) cout << i << " "; cout << endl;
  63.  
  64. cout << "Elements : ";
  65. for (auto x : s) cout << x << " "; cout << endl;
  66. cout << endl;
  67.  
  68.  
  69.  
  70.  
  71. int x = 10;
  72.  
  73. cout << "Index of "<< x <<" : " << s.order_of_key(x) << endl;
  74.  
  75. cout << "Count < "<< x <<" : " << s.order_of_key(x) << endl;
  76. cout << "Count <= "<< x <<" : " << s.order_of_key(x+1) << endl;
  77. cout << "Count > "<< x <<" : " << n - s.order_of_key(x+1) << endl;
  78. cout << "Count >= "<< x <<" : " << n - s.order_of_key(x) << endl;
  79.  
  80. cout << endl;
  81.  
  82. // -------------------------------------------------
  83.  
  84.  
  85.  
  86. cout << "0th Smallest : " << *s.find_by_order(0) << endl;
  87. cout << "2nd Smallest : " << *s.find_by_order(2) << endl;
  88. cout << "1st Largest : " << *s.find_by_order(n-1) << endl;
  89. cout << "2nd Largest : " << *s.find_by_order(n-2) << endl;
  90.  
  91. cout << "Median : " << *s.find_by_order(n / 2) << endl;
  92.  
  93. cout << endl;
  94.  
  95.  
  96. // -------------------------------------------------
  97.  
  98. int L = 5, R = 15;
  99.  
  100. cout << "Elements in [" << L << "," << R << "] : "
  101. << s.order_of_key(R + 1) - s.order_of_key(L) << endl << endl;
  102.  
  103. // -------------------------------------------------
  104.  
  105.  
  106. cout << "Iterate By Index : ";
  107.  
  108. for (int i = 0; i < n; i++) {
  109. cout << *s.find_by_order(i) << " ";
  110. }
  111. cout << endl;
  112.  
  113. // -------------------------------------------------
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. int32_t main() {
  140. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  141.  
  142. int t = 1;
  143. // cin >> t;
  144. while (t--) {
  145. solve();
  146. }
  147.  
  148. return 0;
  149. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Index    : 0 1 2 3 4 5 6 
Elements : 2 5 8 10 15 20 

Index of 10   : 3
Count < 10    : 3
Count <= 10   : 4
Count > 10    : 2
Count >= 10   : 3

0th Smallest  : 2
2nd Smallest  : 8
1st Largest   : 20
2nd Largest   : 15
Median        : 10

Elements in [5,15] : 4

Iterate By Index : 2 5 8 10 15 20