Splay-2

 

 

 

 

 

 

 

 

 

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int N=1e5+11,Tree_Sz=1e5+11,inf=1<<29;
  4 int n;
  5 
  6 inline int re_ad() {
  7     char ch=getchar(); int x=0,f=1;
  8     while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
  9     while('0'<=ch && ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
 10     return x*f;
 11 }
 12 
 13 namespace ST {
 14     int root,tot;
 15     struct Node {
 16         int fa,val,cnt,siz,ch[2];
 17     }t[Tree_Sz];
 18     inline void Pushup(int d) {
 19         t[d].siz=t[t[d].ch[0]].siz+t[t[d].ch[1]].siz+t[d].cnt;
 20     }
 21     inline void Rotate(int x) {
 22         int y=t[x].fa,z=t[y].fa;
 23         int k1=(t[y].ch[1]==x),k2=(t[z].ch[1]==y);
 24         t[t[x].ch[k1^1]].fa=y;
 25         t[y].ch[k1]=t[x].ch[k1^1];
 26         t[y].fa=x;
 27         t[x].ch[k1^1]=y;
 28         t[x].fa=z;
 29         t[z].ch[k2]=x;
 30         Pushup(y),Pushup(x);
 31     }
 32     inline void Splay(int x,int f) {
 33         while(t[x].fa!=f) {
 34             int y=t[x].fa,z=t[y].fa;
 35             if(z!=f) (t[y].ch[1]==x)^(t[z].ch[1]==y)?Rotate(x):Rotate(y);
 36             Rotate(x);
 37         }
 38         if(!f) root=x;
 39     }
 40     inline void Insert(int d) {
 41         int u=root,f=0;
 42         while(u && t[u].val!=d) f=u,u=t[u].ch[d>t[u].val];
 43         if(u) ++t[u].cnt;
 44         else {
 45             u=++tot;
 46             if(f) t[f].ch[d>t[f].val]=u;
 47             t[u].fa=f,t[u].val=d;
 48             t[u].cnt=t[u].siz=1;
 49             t[u].ch[0]=t[u].ch[1]=0;
 50         }
 51         Splay(u,0);
 52     }
 53     inline void Find(int d) {
 54         int u=root;
 55         if(!u) return ;
 56         while(t[u].ch[d>t[u].val] && t[u].val!=d) u=t[u].ch[d>t[u].val];
 57         Splay(u,0);
 58     }
 59     inline int Next(int d,int k) {
 60         Find(d);
 61         int u=root;
 62         if(t[u].val<d && !k) return u;
 63         if(t[u].val>d && k) return u;
 64         u=t[u].ch[k];
 65         while(t[u].ch[k^1]) u=t[u].ch[k^1];
 66         return u;
 67     }
 68     inline void Get_Rank(int d) {
 69         Find(d);
 70         int u=t[root].ch[0];
 71         printf("%d\n",t[u].siz);
 72     }
 73     inline void Get_Pre(int d) {
 74         printf("%d\n",t[Next(d,0)].val);
 75     }
 76     inline void Get_Suf(int d) {
 77         printf("%d\n",t[Next(d,1)].val);
 78     }
 79     inline void Delete(int d) {
 80         int lst=Next(d,0),nxt=Next(d,1);
 81         Splay(lst,0),Splay(nxt,lst);
 82         int u=t[nxt].ch[0];
 83         if(t[u].cnt>1) --t[u].cnt,Splay(u,0);
 84         else t[nxt].ch[0]=0;
 85     }
 86     inline int Kth(int d) {
 87         int u=root;
 88         if(t[u].siz<d) return 0;
 89         while(1) {
 90             int cur=t[t[u].ch[0]].siz,add=t[u].cnt;
 91             if(d<cur) u=t[u].ch[0];
 92             else if(d<cur+add) return u;
 93             else d-=cur+add,u=t[u].ch[1];
 94         }
 95     }
 96     inline void Get_Num(int d) {
 97         printf("%d\n",t[Kth(d)].val);
 98     }
 99 }
100 
101 int main()
102 {
103     n=re_ad();
104     ST::Insert(inf),ST::Insert(-inf);
105     for(int i=1,op,x;i<=n;++i) {
106         op=re_ad(),x=re_ad();
107         if(op==1) ST::Insert(x);
108         else if(op==2) ST::Delete(x);
109         else if(op==3) ST::Get_Rank(x);
110         else if(op==4) ST::Get_Num(x);
111         else if(op==5) ST::Get_Pre(x);
112         else if(op==6) ST::Get_Suf(x);
113     }
114     return 0;
115 }

 

posted @ 2021-09-22 15:24  上官书房  阅读(81)  评论(0)    收藏  举报