【平衡树splay实现】

无注释代码

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 const int INF=1e9+7,MAXN=1e5+5;
  5 int N;
  6 int key[MAXN],cnt[MAXN],ch[MAXN][2],siz[MAXN],f[MAXN];
  7 int root,sz;
  8 inline void clear(int x){
  9     key[x]=cnt[x]=ch[x][0]=ch[x][1]=siz[x]=f[x]=0;
 10 }
 11 inline int get(int x){
 12     return x==ch[f[x]][1];
 13 }
 14 inline void upd(int x){
 15     if(x){
 16         siz[x]=cnt[x];
 17         if(ch[x][0]){
 18             siz[x]+=siz[ch[x][0]];
 19         }
 20         if(ch[x][1]){
 21             siz[x]+=siz[ch[x][1]];
 22         }
 23     }
 24 }
 25 inline void rotate(int x){
 26     int fa=f[x],gf=f[fa],which=get(x);
 27     ch[fa][which]=ch[x][which^1];
 28     f[ch[fa][which]]=fa; 
 29     ch[x][which^1]=fa;
 30     f[fa]=x;
 31     f[x]=gf;
 32     if(gf){
 33         ch[gf][ch[gf][1]==fa]=x;
 34     }
 35     upd(fa);
 36     upd(x);
 37 }
 38 inline void splay(int x){
 39     for(int fa;(fa=f[x]);rotate(x)){
 40         if(f[fa]){
 41             rotate(get(x)==get(fa)?fa:x);
 42         }
 43     }
 44     root=x;
 45 }
 46 inline void ins(int x){
 47     if(!root){
 48         sz++;
 49         clear(sz);
 50         root=sz;
 51         cnt[sz]=siz[sz]=1;
 52         key[sz]=x;
 53         return;
 54     }
 55     int cur=root,fa=0;
 56     while(1){
 57         if(x==key[cur]){
 58             cnt[cur]++;
 59             upd(cur);
 60             upd(fa);
 61             splay(cur);
 62             return;
 63         }
 64         fa=cur;
 65         cur=ch[fa][key[fa]<x];
 66         if(!cur){
 67             clear(++sz);
 68             f[sz]=fa;
 69             cnt[sz]=siz[sz]=1;
 70             ch[fa][key[fa]<x]=sz;
 71             key[sz]=x;
 72             upd(fa);
 73             splay(sz);
 74             return;
 75         }
 76     }
 77 }
 78 inline int find(int x){
 79     int cur=root,ret=0;
 80     while(1){
 81         if(x<key[cur]){
 82             cur=ch[cur][0];
 83         }else{
 84             ret+=(ch[cur][0]?siz[ch[cur][0]]:0);
 85             if(key[cur]==x){
 86                 splay(cur);
 87                 return ret+1;
 88             }
 89             ret+=cnt[cur];
 90             cur=ch[cur][1];
 91         }
 92     }
 93 }
 94 inline int findx(int x){
 95     int cur=root;
 96     while(1){
 97         if(ch[cur][0]&&x<=siz[ch[cur][0]]){
 98             cur=ch[cur][0];
 99         }else{
100             int tmp=(ch[cur][0]?siz[ch[cur][0]]:0)+cnt[cur];
101             if(x<=tmp){
102                 return key[cur];
103             }
104             x-=tmp;
105             cur=ch[cur][1];
106         }
107     }
108 }
109 inline int pre(){
110     int cur=ch[root][0];
111     while(ch[cur][1]){
112         cur=ch[cur][1];
113     }
114     return cur;
115 }
116 inline int nxt(){
117     int cur=ch[root][1];
118     while(ch[cur][0]){
119         cur=ch[cur][0];
120     }
121     return cur;
122 }
123 inline void del(int x){
124     find(x);
125     if(cnt[root]>1){
126         cnt[root]--;
127         upd(root);
128         return;
129     }
130     if(!ch[root][0]&&!ch[root][1]){
131         clear(root);
132         root=0;
133         return;
134     }
135     if(!ch[root][0]){
136         int old=root;
137         root=ch[root][1];
138         f[root]=0;
139         clear(old);
140         return;
141     }
142     if(!ch[root][1]){
143         int old=root;
144         root=ch[root][0];
145         f[root]=0;
146         clear(old);
147         return;
148     }
149     int old=root,p=pre();
150     splay(p);
151     ch[root][1]=ch[old][1];
152     f[ch[old][1]]=root;
153     clear(old);
154     upd(root);
155 }
156 int main(){
157     scanf("%d",&N);
158     for(int i=1;i<=N;i++){
159         int ii,jj;
160         scanf("%d%d",&ii,&jj);
161         switch(ii){
162             case 1:{
163                 ins(jj);
164                 break;
165             }
166             case 2:{
167                 del(jj);
168                 break;
169             }
170             case 3:{
171                 printf("%d\n",find(jj));
172                 break;
173             }
174             case 4:{
175                 printf("%d\n",findx(jj));
176                 break;
177             }
178             case 5:{
179                 ins(jj);
180                 printf("%d\n",key[pre()]);
181                 del(jj);
182                 break;
183             }
184             case 6:{
185                 ins(jj);
186                 printf("%d\n",key[nxt()]);
187                 del(jj);
188                 break;
189             }
190         }
191     }
192     return 0;
193 }
View Code

相关文章:

  • 2021-11-20
  • 2021-05-27
  • 2021-06-18
  • 2021-12-26
  • 2021-11-03
  • 2021-08-10
猜你喜欢
  • 2021-08-11
  • 2022-01-26
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-11-17
相关资源
相似解决方案