Advertisement
pdpd123

Untitled

Mar 14th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define mkp make_pair
  3. #define N 200010
  4. using namespace std;
  5.  
  6. int Rand=7122;
  7. int Random(){
  8.     return Rand=Rand*0xdefaced%1000000009+1;
  9. }
  10. struct node{
  11.     node *l,*r;
  12.     int key,pri,siz;
  13.     node(int k):l(nullptr),r(nullptr),key(k),pri(Random()),siz(1){}
  14.     void up(){
  15.         siz=1;
  16.         if(l) siz+=l->siz;
  17.         if(r) siz+=r->siz;
  18.     }
  19. };
  20. inline int siz(node *o){//return the size of the trap
  21.     return o?o->siz:0;
  22. }
  23. node *Merge(node *a,node *b){//merge the treap a and b
  24.     if(!a||!b) return a?a:b;
  25.     if(a->pri <b->pri)
  26.         return a->r=Merge(a->r,b),a->up(),a;
  27.     else
  28.         return b->l=Merge(a,b->l),b->up(),b;
  29. }
  30. void Split(node *o,node *&a,node *&b,int k){//split treap o to a and b by k
  31.     if(!o) a=b=nullptr;
  32.     else if(o->key <k)
  33.         a=o,Split(o->r,a->r,b,k),o->up();
  34.     else
  35.         b=o,Split(o->l,a,b->l,k),o->up();
  36. }
  37. void Insert(node *&root,int k){
  38.     node *a,*b;
  39.     Split(root,a,b,k);
  40.     root=Merge(a,Merge(new node(k),b));
  41. }
  42. bool Erase(node *&o,int k){//delete node that key=k
  43.     if(!o) return false;
  44.     if(o->key ==k){
  45.         node *t=o;
  46.         o=Merge(o->l,o->r);
  47.         delete t;
  48.         return true;
  49.     }
  50.     node *&t=(k<o->key)?o->l:o->r;
  51.     if(Erase(t,k)) return o->up(),true;
  52.     return false;
  53. }
  54. //ranking kth small
  55. inline int Rank(node *&root ,int k){//find rank of key k (0-base)
  56.     node *a,*b;
  57.     Split(root ,a,b,k);
  58.     int ans=siz(a);
  59.     root=Merge(a,b);
  60.     return ans;
  61. }
  62. void Split2(node *o,node *&a,node *&b,int k){//split by rank
  63.     if(!o) a=b=nullptr;
  64.     else if(k>siz(o->l)){
  65.         a=o;
  66.         int newk=k-siz(o->l)-1;
  67.         Split2(o->r,a->r,b,newk);
  68.         o->up();
  69.     }
  70.     else{
  71.         b=o;
  72.         Split2(o->l,a,b->l,k);
  73.         o->up();
  74.     }
  75. }
  76. int Kth(node *&root,int k){//return kth place
  77.     node *a,*b,*c;
  78.     Split2(root,a,c,k);
  79.     Split2(a,a,b,k-1);
  80.     root=Merge(a,Merge(b,c));
  81.     return b->key;
  82. }
  83. void Inorder(node *root){
  84.     if(!root) return;
  85.     Inorder(root->l);
  86.     cout << root->key << ' ';
  87.     Inorder(root->r);
  88. }
  89.  
  90. int main(){
  91.     char in;
  92.     node *treap=nullptr;
  93.     int k;
  94.     while(cin >> in){
  95.         if(in=='p')
  96.             Inorder(treap),cout << endl;
  97.         else{
  98.             cin >> k;
  99.             if(in=='r')
  100.                 cout << k << " is rank " << Rank(treap,k) << endl;
  101.             if(in=='k')
  102.                 cout << "rank " << k << " is "<< Kth(treap,k) << endl;
  103.             if(in=='i')
  104.                 Insert(treap,k),cout << "Complete\n";
  105.             if(in=='e')
  106.                 Erase(treap,k)?cout << "Erased\n":cout << "Not Found\n";
  107.             }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement