Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** Order Statistics Tree ***/
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp> // Common file
- #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
- using namespace __gnu_pbds;
- using namespace std;
- typedef tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- // find_by_order() --- returns an iterator to the k-th largest element (counting from zero)
- // order_of_key() --- the number of items in a set that are strictly smaller than our item
- int main()
- {
- ordered_set X;
- X.insert(1);
- X.insert(2);
- X.insert(4);
- X.insert(8);
- X.insert(16);
- cout<<*X.find_by_order(1)<<endl; // 2
- cout<<*X.find_by_order(2)<<endl; // 4
- cout<<*X.find_by_order(4)<<endl; // 16
- cout<<(X.end()==X.find_by_order(6))<<endl; // true
- cout<<X.order_of_key(-5)<<endl; // 0
- cout<<X.order_of_key(1)<<endl; // 0
- cout<<X.order_of_key(3)<<endl; // 2
- cout<<X.order_of_key(4)<<endl; // 2
- cout<<X.order_of_key(400)<<endl; // 5
- }
Advertisement
Add Comment
Please, Sign In to add comment