Advertisement
STEFAN_STOYANOV

2781

Apr 20th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<cstdio>
  6. using namespace std;
  7. const int maxn=1<<16;
  8. struct Treap
  9. {
  10.     struct Node
  11.     {
  12.         int key,pr,sz,pos;
  13.         Node *l,*r;
  14.         Node(){}
  15.         Node(int _key)
  16.         {
  17.             key=_key;
  18.             pr=rand();
  19.             sz=1;
  20.             l=r=NULL;
  21.         }
  22.         Node(int _key,int _pos)
  23.         {
  24.             key=_key;
  25.             pr=rand();
  26.             sz=1;
  27.             l=r=NULL;
  28.             pos=_pos;
  29.         }
  30.         Node(int _key,int _pr,int _pos)
  31.         {
  32.             key=_key;
  33.             pr=_pr;
  34.             sz=1;
  35.             l=r=NULL;
  36.             pos=_pos;
  37.         }
  38.         void con()
  39.         {
  40.             sz=1;
  41.             if(l!=NULL)sz+=l->sz;
  42.             if(r!=NULL)sz+=r->sz;
  43.         }
  44.     };
  45.  
  46.     Node *root;
  47.     Treap(){root=NULL;}
  48.  
  49.     void Split(Node *T,Node *&L,Node *&R,int key)
  50.     {
  51.         if(T==NULL)
  52.         {
  53.             L=R=NULL;
  54.             return;
  55.         }
  56.         if(key>=T->key)
  57.         {
  58.             L=T;
  59.             Split(T->r,L->r,R,key);
  60.         }
  61.         else
  62.         {
  63.             R=T;
  64.             Split(T->l,L,R->l,key);
  65.         }
  66.         T->con();
  67.     }
  68.     void Merge(Node *&T,Node *L,Node *R)
  69.     {
  70.         if(L==NULL)
  71.         {
  72.             T=R;
  73.             return;
  74.         }
  75.         if(R==NULL)
  76.         {
  77.             T=L;
  78.             return;
  79.         }
  80.         if(L->pr < R->pr)
  81.         {
  82.             T=L;
  83.             Merge(T->r,L->r,R);
  84.         }
  85.         else
  86.         {
  87.             T=R;
  88.             Merge(T->l,L,R->l);
  89.         }
  90.         T->con();
  91.     }
  92.     void Split_sz(Node *T,Node *&L,Node *&R,int sz)
  93.     {
  94.         if(T==NULL)
  95.         {
  96.             L=R=NULL;
  97.             return;
  98.         }
  99.         int tsz=1;
  100.         if(T->l!=NULL)tsz+=T->l->sz;
  101.         if(sz<tsz)
  102.         {
  103.             R=T;
  104.             Split_sz(T->l,L,R->l,sz);
  105.         }
  106.         else
  107.         {
  108.             L=T;
  109.             Split_sz(T->r,L->r,R,sz-tsz);
  110.         }
  111.         T->con();
  112.     }
  113.  
  114.     void Insert(int key,int pos)
  115.     {
  116.         Node *L,*R;
  117.         Split(root,L,R,key);
  118.         Node *M=new Node(key,pos);
  119.         Merge(L,L,M);
  120.         Merge(root,L,R);
  121.     }
  122.     void Insert(int key,int pr,int pos)
  123.     {
  124.         Node *L,*R;
  125.         Split(root,L,R,key);
  126.         ///cout<<"passed1\n";
  127.         Node *M=new Node(key,pr,pos);
  128.         Merge(L,L,M);
  129.         ///cout<<"passed2\n";
  130.         Merge(root,L,R);
  131.         ///cout<<"passed3\n";
  132.     }
  133.     void Delete(int key)
  134.     {
  135.         Node *L,*R,*trash;
  136.         Split(root,L,R,key);
  137.         Split_sz(R,trash,R,1);
  138.         Merge(root,L,R);
  139.     }
  140.  
  141.     void Find(Node *T,Node *Before,int key)///actually printing
  142.     {
  143.         if(T->key==key)
  144.         {
  145.             printf("%d ",Before->pos);
  146.             if(T->l==NULL)printf("0 ");
  147.             else printf("%d ",T->l->pos);
  148.             if(T->r==NULL)printf("0\n");
  149.             else printf("%d\n",T->r->pos);
  150.             return;
  151.         }
  152.         if(key<T->key)Find(T->l,T,key);
  153.         else Find(T->r,T,key);
  154.     }
  155.     void Print(Node *T)
  156.     {
  157.         if(T->l)Print(T->l);
  158.         ///cout<<T->key<<' '<<T->pos<<endl;
  159.         if(T->r)Print(T->r);
  160.     }
  161. };
  162. Treap T;
  163. int n;
  164. int a[maxn],b[maxn];
  165. int main()
  166. {
  167.     scanf("%d",&n);
  168.     for(int i=1;i<=n;i++)
  169.     {
  170.         scanf("%d%d",&a[i],&b[i]);
  171.         T.Insert(a[i],b[i],i);
  172.     }
  173.     /*printf("%d\n",T.root->key);
  174.     T.Print(T.root);*/
  175.     printf("YES\n");
  176.     for(int i=1;i<=n;i++)
  177.     {
  178.         ///printf("%d\n",a[i]);
  179.         if(a[i]==T.root->key)
  180.         {
  181.             printf("0 ");
  182.             if(T.root->l==NULL)printf("0 ");
  183.             else printf("%d ",T.root->l->pos);
  184.             if(T.root->r==NULL)printf("0\n");
  185.             else printf("%d\n",T.root->r->pos);
  186.             continue;
  187.         }
  188.         if(a[i]<T.root->key)
  189.         {
  190.             ///cout<<"Here1\n";
  191.             T.Find(T.root->l,T.root,a[i]);
  192.         }
  193.         else
  194.         {
  195.             ///cout<<"Here2\n";
  196.             T.Find(T.root->r,T.root,a[i]);
  197.         }
  198.         ///printf("\n");
  199.     }
  200. return 0;
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement