Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<cmath>
- #include<cstring>
- #include<cstdio>
- using namespace std;
- const int maxn=1<<16;
- struct Treap
- {
- struct Node
- {
- int key,pr,sz,pos;
- Node *l,*r;
- Node(){}
- Node(int _key)
- {
- key=_key;
- pr=rand();
- sz=1;
- l=r=NULL;
- }
- Node(int _key,int _pos)
- {
- key=_key;
- pr=rand();
- sz=1;
- l=r=NULL;
- pos=_pos;
- }
- Node(int _key,int _pr,int _pos)
- {
- key=_key;
- pr=_pr;
- sz=1;
- l=r=NULL;
- pos=_pos;
- }
- void con()
- {
- sz=1;
- if(l!=NULL)sz+=l->sz;
- if(r!=NULL)sz+=r->sz;
- }
- };
- Node *root;
- Treap(){root=NULL;}
- void Split(Node *T,Node *&L,Node *&R,int key)
- {
- if(T==NULL)
- {
- L=R=NULL;
- return;
- }
- if(key>=T->key)
- {
- L=T;
- Split(T->r,L->r,R,key);
- }
- else
- {
- R=T;
- Split(T->l,L,R->l,key);
- }
- T->con();
- }
- void Merge(Node *&T,Node *L,Node *R)
- {
- if(L==NULL)
- {
- T=R;
- return;
- }
- if(R==NULL)
- {
- T=L;
- return;
- }
- if(L->pr < R->pr)
- {
- T=L;
- Merge(T->r,L->r,R);
- }
- else
- {
- T=R;
- Merge(T->l,L,R->l);
- }
- T->con();
- }
- void Split_sz(Node *T,Node *&L,Node *&R,int sz)
- {
- if(T==NULL)
- {
- L=R=NULL;
- return;
- }
- int tsz=1;
- if(T->l!=NULL)tsz+=T->l->sz;
- if(sz<tsz)
- {
- R=T;
- Split_sz(T->l,L,R->l,sz);
- }
- else
- {
- L=T;
- Split_sz(T->r,L->r,R,sz-tsz);
- }
- T->con();
- }
- void Insert(int key,int pos)
- {
- Node *L,*R;
- Split(root,L,R,key);
- Node *M=new Node(key,pos);
- Merge(L,L,M);
- Merge(root,L,R);
- }
- void Insert(int key,int pr,int pos)
- {
- Node *L,*R;
- Split(root,L,R,key);
- ///cout<<"passed1\n";
- Node *M=new Node(key,pr,pos);
- Merge(L,L,M);
- ///cout<<"passed2\n";
- Merge(root,L,R);
- ///cout<<"passed3\n";
- }
- void Delete(int key)
- {
- Node *L,*R,*trash;
- Split(root,L,R,key);
- Split_sz(R,trash,R,1);
- Merge(root,L,R);
- }
- void Find(Node *T,Node *Before,int key)///actually printing
- {
- if(T->key==key)
- {
- printf("%d ",Before->pos);
- if(T->l==NULL)printf("0 ");
- else printf("%d ",T->l->pos);
- if(T->r==NULL)printf("0\n");
- else printf("%d\n",T->r->pos);
- return;
- }
- if(key<T->key)Find(T->l,T,key);
- else Find(T->r,T,key);
- }
- void Print(Node *T)
- {
- if(T->l)Print(T->l);
- ///cout<<T->key<<' '<<T->pos<<endl;
- if(T->r)Print(T->r);
- }
- };
- Treap T;
- int n;
- int a[maxn],b[maxn];
- int main()
- {
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- scanf("%d%d",&a[i],&b[i]);
- T.Insert(a[i],b[i],i);
- }
- /*printf("%d\n",T.root->key);
- T.Print(T.root);*/
- printf("YES\n");
- for(int i=1;i<=n;i++)
- {
- ///printf("%d\n",a[i]);
- if(a[i]==T.root->key)
- {
- printf("0 ");
- if(T.root->l==NULL)printf("0 ");
- else printf("%d ",T.root->l->pos);
- if(T.root->r==NULL)printf("0\n");
- else printf("%d\n",T.root->r->pos);
- continue;
- }
- if(a[i]<T.root->key)
- {
- ///cout<<"Here1\n";
- T.Find(T.root->l,T.root,a[i]);
- }
- else
- {
- ///cout<<"Here2\n";
- T.Find(T.root->r,T.root,a[i]);
- }
- ///printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement