Advertisement
STEFAN_STOYANOV

Sofia Autumn 2019 - diff

Nov 9th, 2022
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int inf=(1<<30);
  4. mt19937 rg;
  5. struct Treap
  6. {
  7.     struct Node
  8.     {
  9.         Node *l,*r;
  10.         int sz,key,pr;
  11.         //int ldif,rdif,mdif;
  12.         Node()
  13.         {
  14.             l=NULL;
  15.             r=NULL;
  16.         }
  17.         Node(int _key)
  18.         {
  19.             l=NULL;
  20.             r=NULL;
  21.             key=_key;
  22.             pr=rg();
  23.             sz=1;
  24.         }
  25.         void con()
  26.         {
  27.             sz=1;
  28.             if(l)
  29.             {
  30.                 sz+=l->sz;
  31.             }
  32.             if(r)
  33.             {
  34.                 sz+=r->sz;
  35.             }
  36.         }
  37.     };
  38.  
  39.     Node *root;
  40.     Treap(){root=NULL;}
  41.  
  42.     void Split(Node *T,Node *&L,Node *&R,int key)
  43.     {
  44.         if(T==NULL)
  45.         {
  46.             L=NULL;
  47.             R=NULL;
  48.             return;
  49.         }
  50.         if(key >= T->key)
  51.         {
  52.             L=T;
  53.             Split(T->r,L->r,R,key);
  54.         }
  55.         else
  56.         {
  57.             R=T;
  58.             Split(T->l,L,R->l,key);
  59.         }
  60.         T->con();
  61.     }
  62.  
  63.     void Merge(Node *&T,Node *L,Node *R)
  64.     {
  65.         if(L==NULL)
  66.         {
  67.             T=R;
  68.             return;
  69.         }
  70.         if(R==NULL)
  71.         {
  72.             T=L;
  73.             return;
  74.         }
  75.         if(L->pr > R->pr)
  76.         {
  77.             T=L;
  78.             Merge(T->r,L->r,R);
  79.         }
  80.         else
  81.         {
  82.             T=R;
  83.             Merge(T->l,L,R->l);
  84.         }
  85.         T->con();
  86.     }
  87.  
  88.     void Split_sz(Node *T,Node *&L,Node *&R,int sz)
  89.     {
  90.         if(T==NULL)
  91.         {
  92.             L=NULL;
  93.             R=NULL;
  94.             return;
  95.         }
  96.         int tsz=1;
  97.         if(T->l)tsz+=T->l->sz;
  98.         if(tsz>sz)
  99.             Split_sz(T->l,L,R->l,sz);
  100.         else Split_sz(T->r,L->r,R,sz-tsz);
  101.         T->con();
  102.     }
  103.  
  104.     void Insert(int key)
  105.     {
  106.         Node *L,*R,*N=new Node(key);
  107.         Split(root,L,R,key);
  108.         Merge(L,L,N);
  109.         Merge(root,L,R);
  110.     }
  111.  
  112.     void Delete(int key)
  113.     ///key is always in the tr
  114.     {
  115.         Node *L,*R,*N;
  116.         Split(root,L,R,key);
  117.         Split(L,L,N,key-1);
  118.         delete N;
  119.         Merge(root,L,R);
  120.     }
  121.  
  122.     void just_split(int sz)
  123.  
  124. };
  125. Treap T;
  126. int main()
  127. {
  128.     rg.seed(time(0));
  129.  
  130.     return 0;
  131. }
  132. /*T.Insert(1);
  133.     cout<<T.root->sz<<endl;
  134.     T.Insert(2);
  135.     cout<<T.root->sz<<endl;
  136.     T.Insert(3);
  137.     cout<<T.root->sz<<endl;
  138.     T.Delete(2);
  139.     cout<<T.root->sz<<endl;
  140. */
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement