fork 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.  
  76. cout << "Count < "<< x <<" : " << s.order_of_key(x);
  77. cout << " ";
  78. cout << "Count <= "<< x <<" : " << s.order_of_key(x+1) << endl;
  79.  
  80.  
  81. cout << "Count > "<< x <<" : " << n - s.order_of_key(x+1);
  82. cout << " ";
  83. cout << "Count >= "<< x <<" : " << n - s.order_of_key(x) << endl;
  84.  
  85. cout << endl;
  86.  
  87. // -------------------------------------------------
  88.  
  89.  
  90.  
  91. cout << "0th Smallest : " << *s.find_by_order(0);
  92. cout << " ";
  93. cout << "2nd Smallest : " << *s.find_by_order(2) << endl;
  94.  
  95.  
  96. cout << "1st Largest : " << *s.find_by_order(n-1);
  97. cout << " ";
  98. cout << "2nd Largest : " << *s.find_by_order(n-2) << endl;
  99.  
  100.  
  101. cout << "Median : " << *s.find_by_order(n / 2) << endl;
  102.  
  103. cout << endl;
  104.  
  105.  
  106. // -------------------------------------------------
  107.  
  108. int L = 5, R = 15;
  109.  
  110. cout << "Elements in Range [" << L << "," << R << "] : "
  111. << s.order_of_key(R + 1) - s.order_of_key(L) << endl << endl;
  112.  
  113. // -------------------------------------------------
  114.  
  115.  
  116. cout << "Iterate By Index : ";
  117.  
  118. for (int i = 0; i < n; i++) {
  119. cout << *s.find_by_order(i) << " ";
  120. }
  121. cout << endl;
  122.  
  123. // -------------------------------------------------
  124.  
  125.  
  126.  
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. int32_t main() {
  150. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  151.  
  152. int t = 1;
  153. // cin >> t;
  154. while (t--) {
  155. solve();
  156. }
  157.  
  158. return 0;
  159. }
Success #stdin #stdout 0s 5280KB
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 Range [5,15] : 4

Iterate By Index : 2 5 8 10 15 20