模板—splay

  1 #include<iostream> 
  2 #include<cstdio>
  3 #define cin(x) scanf("%d",&x)
  4 using namespace std;
  5 int ch[10000010][2],key[10000010],
  6     cnt[10000010],size[10000010],sz,rt,f[10000010];
  7 bool get(int x) 
  8 {
  9     return ch[f[x]][1]==x;
 10 }
 11 void clear(int x)
 12 {
 13     f[x]=cnt[x]=ch[x][0]=ch[x][1]=size[x]=key[x]=0;
 14 }
 15 void pushup(int x)
 16 {
 17     if(x)
 18     {
 19         size[x]=cnt[x];
 20         if(ch[x][0])size[x]+=size[ch[x][0]];
 21         if(ch[x][1])size[x]+=size[ch[x][1]];
 22     }
 23 }
 24 void rotate(int x)
 25 {
 26     int old=f[x],oldf=f[old],which=get(x);
 27     ch[old][which]=ch[x][which^1];f[ch[old][which]]=old;
 28     ch[x][which^1]=old;f[old]=x;
 29     f[x]=oldf;
 30     if(oldf)ch[oldf][ch[oldf][1]==old]=x;
 31     pushup(old),pushup(x);
 32 }
 33 void splay(int x)
 34 {
 35     for(int fa;fa=f[x];rotate(x))
 36         if(f[fa])
 37             rotate(get(x)==get(fa)?fa:x);
 38     rt=x;
 39 }
 40 void insert(int x)
 41 {
 42     if(rt==0)
 43     {
 44         sz++;key[sz]=x;rt=sz;
 45         cnt[sz]=size[sz]=1;
 46         f[sz]=ch[sz][0]=ch[sz][1]=0;
 47         return;
 48     }
 49     int now=rt,fa=0;
 50     while(1)
 51     {
 52         if(x==key[now])
 53         {
 54             cnt[now]++;
 55             pushup(now);
 56             pushup(fa);
 57             splay(now);
 58             return;
 59         }
 60         fa=now;
 61         now=ch[now][x>key[now]];
 62         if(now==0)
 63         {
 64             sz++;
 65             size[sz]=cnt[sz]=1;
 66             ch[sz][0]=ch[sz][1]=0;
 67             ch[fa][x>key[fa]]=sz;
 68             f[sz]=fa;
 69             key[sz]=x;
 70             pushup(fa);
 71             splay(sz);
 72             return;
 73         }
 74     }
 75 }
 76 int rnk(int x)
 77 {
 78     int now=rt,ans=0;
 79     while(1)
 80     {
 81         if(ch[now][0] && x<key[now])now=ch[now][0];
 82         else
 83         {
 84             ans+=size[ch[now][0]];
 85             if(x==key[now])
 86             {
 87                 splay(now);
 88                 return ans+1;
 89             }
 90             ans+=cnt[now];
 91             now=ch[now][1];
 92         }
 93     }
 94 }
 95 int kth(int x)
 96 {
 97     int now=rt;
 98     while(1)
 99     {
100         if(ch[now][0] && x<=size[ch[now][0]])now=ch[now][0];
101         else
102         {
103             int temp=size[ch[now][0]]+cnt[now];
104             if(temp>=x)
105                 return key[now];
106             x-=temp;now=ch[now][1];
107         }
108     }
109 }
110 int pre()
111 {
112     int now=ch[rt][0];
113     while(ch[now][1])now=ch[now][1];
114     return now;
115 }
116 int next()
117 {
118     int now=ch[rt][1];
119     while(ch[now][0])now=ch[now][0];
120     return now;
121 }
122 void del(int x)
123 {
124     rnk(x);
125     if(cnt[rt]>1){cnt[rt]--;pushup(rt);return;}
126     if(!ch[rt][0] && !ch[rt][1]){clear(rt);rt=0;return;}
127     if(!ch[rt][0])
128     {
129         int old=rt;rt=ch[rt][1];f[rt]=0;clear(old);return;
130     }
131     else if(!ch[rt][1])
132     {
133         int old=rt;rt=ch[rt][0];f[rt]=0;clear(old);return;
134     }
135     int oldrt=rt,leftbig=pre();
136     splay(leftbig);//leftbig无儿子,所以oldrt无左二子
137     ch[rt][1]=ch[oldrt][1];
138     f[ch[oldrt][1]]=rt;
139     clear(oldrt);
140     pushup(rt);
141     
142 }
143 signed main()
144 {
145 //    freopen("input5.in","r",stdin);
146     
147     int n,opt,x;
148     cin(n);
149     while(n--)
150     {
151         cin(opt);cin(x);
152         if(opt==1)insert(x);
153         if(opt==2)del(x);
154         if(opt==3)printf("%ld\n",rnk(x));
155         if(opt==4)printf("%ld\n",kth(x));
156         if(opt==5){insert(x);printf("%ld\n",key[pre()]);del(x);}
157         if(opt==6){insert(x);printf("%ld\n",key[next()]);del(x);}
158     }
159 }

 

posted @ 2019-06-13 17:15  Al_Ca  阅读(116)  评论(0编辑  收藏  举报
ヾ(≧O≦)〃嗷~