BotByte

Order Statistics Tree.cpp

Mar 23rd, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. /*** Order Statistics Tree ***/
  2.  
  3. #include <bits/stdc++.h>
  4. #include <ext/pb_ds/assoc_container.hpp> // Common file
  5. #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
  6.  
  7. using namespace __gnu_pbds;
  8. using namespace std;
  9.  
  10. typedef tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  11.  
  12. // find_by_order() --- returns an iterator to the k-th largest element (counting from zero)
  13. // order_of_key() --- the number of items in a set that are strictly smaller than our item
  14.  
  15. int main()
  16. {
  17.     ordered_set X;
  18.     X.insert(1);
  19.     X.insert(2);
  20.     X.insert(4);
  21.     X.insert(8);
  22.     X.insert(16);
  23.  
  24.     cout<<*X.find_by_order(1)<<endl; // 2
  25.     cout<<*X.find_by_order(2)<<endl; // 4
  26.     cout<<*X.find_by_order(4)<<endl; // 16
  27.     cout<<(X.end()==X.find_by_order(6))<<endl; // true
  28.  
  29.     cout<<X.order_of_key(-5)<<endl;  // 0
  30.     cout<<X.order_of_key(1)<<endl;   // 0
  31.     cout<<X.order_of_key(3)<<endl;   // 2
  32.     cout<<X.order_of_key(4)<<endl;   // 2
  33.     cout<<X.order_of_key(400)<<endl; // 5
  34. }
Advertisement
Add Comment
Please, Sign In to add comment