Advertisement
STEFAN_STOYANOV

Treap Almost Pseudo code

Nov 9th, 2022
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct Treap
  4. {
  5.     struct Node
  6.     {
  7.         Node *l,*r;
  8.         int key,pr,sz;
  9.         bool rev;
  10.         Node(){l=r=NULL;}
  11.         Node(int _key)
  12.         {
  13.             key=_key;
  14.             pr=rand();
  15.             sz=1;
  16.             l=r=NULL;
  17.         }
  18.         void con()
  19.         {
  20.             sz=1;
  21.             if(l)sz+=l->sz;
  22.             if(r)sz+=r->sz;
  23.         }
  24.     };
  25.  
  26.     Node *root;
  27.     Treap(){root=NULL;}
  28.  
  29.     void push(Node *T)
  30.     {
  31.         if(T&&T->rev)
  32.         {
  33.             T->rev=false;
  34.             swap(T->l,T->r);
  35.             if(T->l)T->l->rev^=true;
  36.             if(T->r)T->r->rev^=true;
  37.         }
  38.     }
  39.  
  40.     void Split(Node *T,Node *&L,Node *&R,int key)
  41.     {
  42.  
  43.         if(T==NULL)
  44.         {
  45.             L=R=NULL;
  46.             return;
  47.         }
  48.         //push(T);
  49.         if(key>=T->key)
  50.         {
  51.             L=T;
  52.             Split(T->r,L->r,R,key);
  53.         }
  54.         else
  55.         {
  56.             R=T;
  57.             Split(T->l,L,R->l,key);
  58.         }
  59.         T->con();
  60.     }
  61.  
  62.     void Merge(Node *&T,Node *L,Node *R)
  63.     {
  64.         //push(L);push(R);
  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=R=NULL;
  93.             return;
  94.         }
  95.         //push(T);
  96.         int tsz=1;
  97.         if(T->l)tsz+=T->l->sz;
  98.         if(tsz>sz)
  99.         {
  100.             R=T;
  101.             Split_sz(T->l,L,R->l,sz);
  102.         }
  103.         else
  104.         {
  105.             L=T;
  106.             Split_sz(T->r,L->r,R,sz-tsz);
  107.         }
  108.         T->con();
  109.     }
  110.  
  111.     void Insert(int key)
  112.     {
  113.         Node *L,*R,*N=new Node(key);
  114.         Split(root,L,R,key);
  115.         Merge(L,L,N);
  116.         Merge(root,L,R);
  117.         //delete L;
  118.         //delete R;
  119.     }
  120.  
  121.     void Delete(int key)
  122.     {
  123.         Node *L,*R,*N;
  124.         Split(root,L,R,key);
  125.         Split(L,L,N,key-1);
  126.         delete N;
  127.         Merge(root,L,R);
  128.         //delete L;
  129.         //delete R;
  130.     }
  131.  
  132.     void Walkthrough(Node *T)
  133.     {
  134.         if(T==NULL)return;
  135.         Walkthrough(T->l);
  136.         cout<<T->key<<' ';
  137.         Walkthrough(T->r);
  138.     }
  139.  
  140.     int How_many_small(int key)
  141.     {
  142.         Node *L,*R;
  143.         Split(root,L,R,key);
  144.         int ans=0;
  145.         if(L)ans=L->sz;
  146.         Merge(root,L,R);
  147.         //delete L;
  148.         //delete R;
  149.         return ans;
  150.     }
  151.  
  152.     int Insert_with_query(int key)
  153.     {
  154.         Node *L,*R,*N=new Node(key);
  155.         Split(root,L,R,key-1);
  156.         int ans=0;
  157.         if(L)ans=L->sz;
  158.         Merge(L,L,N);
  159.         Merge(root,L,R);
  160.         return ans;
  161.     }
  162.  
  163.     void heapify(Node *T)
  164.     {
  165.         if(T==NULL)return;
  166.         Node *maxi=T;
  167.         if(T->l!=NULL && T->l->pr > maxi->pr)
  168.             maxi=T->l;
  169.         if(T->r!=NULL && T->r->pr > maxi->pr)
  170.             maxi=T->r;
  171.         if(T!=maxi)
  172.         {
  173.             swap(T->pr,maxi->pr);
  174.             heapify(maxi);
  175.         }
  176.     }
  177.  
  178.     Node* fast_build(int *a,int a_sz)///O(N)
  179.     {
  180.         ///a[0]...a[a_sz-1]
  181.         if(a_sz==0)return NULL;
  182.         int mid=a_sz/2;
  183.         Node *T=new Node(a[mid]);
  184.         printf("T->key=%d, a_sz==%d, mid==%d and a[mid]==%d\n\n",T->key,a_sz,mid,a[mid]);
  185.         T->l=fast_build(a,mid);
  186.         T->r=fast_build(a+mid+1,a_sz-mid-1);
  187.         heapify(T);
  188.         T->con();
  189.         return T;
  190.     }
  191.  
  192.     void reverse_seg(Node *T,int l,int r)
  193.     {
  194.         Node *L,*R,*N;
  195.         Split_sz(root,L,R,r);
  196.         Split_sz(L,L,N,l-1);
  197.         N->rev^=true;
  198.         Megre(L,L,N);
  199.         Merge(root,L,R);
  200.     }
  201. };
  202. int main()
  203. {
  204.  
  205.     return 0;
  206. }
  207.     /*T.Insert(23);
  208.     T.Walkthrough(T.root);cout<<endl;
  209.     T.Insert(34);
  210.     T.Walkthrough(T.root);cout<<endl;
  211.     T.Insert(56);
  212.     T.Walkthrough(T.root);cout<<endl;
  213.     T.Delete(23);
  214.     T.Walkthrough(T.root);cout<<endl;
  215.     T.Insert(128);
  216.     T.Walkthrough(T.root);cout<<endl;
  217.     T.Insert(2);
  218.     T.Walkthrough(T.root);cout<<endl;
  219.     T.Delete(128);
  220.     T.Walkthrough(T.root);cout<<endl;
  221.     T.Delete(2);
  222.     T.Walkthrough(T.root);cout<<endl;*/
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement