splay

 随便敲敲练手。。。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 const int inf=1e9;
  5 const int maxn=200000;
  6 int N,M,T,tot,root;
  7 int key[maxn],lc[maxn],rc[maxn],fa[maxn],siz[maxn];
  8 inline void update(int x){
  9     siz[x]=siz[lc[x]]+siz[rc[x]]+1;
 10 }
 11 inline void r_rotate(int x){
 12     int y=fa[x];
 13     lc[y]=rc[x];
 14     if(rc[x]) fa[rc[x]]=y;
 15     fa[x]=fa[y];
 16     if(y==lc[fa[y]]) lc[fa[y]]=x;
 17     else rc[fa[y]]=x;
 18     fa[y]=x; rc[x]=y;
 19     update(y); update(x);
 20 }
 21 inline void l_rotate(int x){
 22     int y=fa[x];
 23     rc[y]=lc[x];
 24     if(lc[x]) fa[lc[x]]=y;
 25     fa[x]=fa[y];
 26     if(y==lc[fa[y]]) lc[fa[y]]=x;
 27     else rc[fa[y]]=x;
 28     fa[y]=x; lc[x]=y;
 29     update(y); update(x);
 30 }
 31 inline void splay(int x,int s){
 32     int p;
 33     while(fa[x]!=s){
 34         int p=fa[x];
 35         if(fa[p]==s){
 36             if(x==lc[p]) r_rotate(x);
 37             else l_rotate(x);
 38             break;
 39         }
 40         else if(x==lc[p]){
 41             if(p==lc[fa[p]]) r_rotate(x),r_rotate(x);
 42             else r_rotate(x),l_rotate(x);
 43         }
 44         else if(x==rc[p]){
 45             if(p==rc[fa[p]]) l_rotate(x),l_rotate(x);
 46             else l_rotate(x),r_rotate(x);
 47         }
 48     }
 49     if(s==0) root=x;
 50 }
 51 
 52 inline void insert(int v){
 53     if(root==0){
 54         root=++tot;
 55         rc[0]=tot; key[root]=v; siz[root]=1;
 56         return ;
 57     }
 58     int tmp,x=root;
 59     while(x!=0){
 60         tmp=x;
 61         if(v<=key[x]) siz[x]++,x=lc[x];
 62         else siz[x]++,x=rc[x];
 63     }
 64     if(v<=key[tmp]){
 65         lc[tmp]=++tot;
 66         fa[tot]=tmp; key[tot]=v; siz[tot]=1;
 67     }
 68     else{
 69         rc[tmp]=++tot;
 70         fa[tot]=tmp; key[tot]=v; siz[tot]=1;
 71     }
 72     splay(tot,0);
 73 }
 74 
 75 inline int find(int v){
 76     int x=root;
 77     while(x!=0){
 78         if(v<key[x]) x=lc[x];
 79         else if(v>key[x]) x=rc[x];
 80         else{
 81             splay(x,0);
 82             return x;
 83         }
 84     }
 85     return -1;
 86 }
 87 
 88 inline int getmax(int x){
 89     int tmp;
 90     while(x!=0){
 91         tmp=x; x=rc[x];
 92     }
 93     return tmp;
 94 }
 95 inline int getmin(int x){
 96     int tmp;
 97     while(x!=0){
 98         tmp=x; x=lc[x];
 99     }
100     return tmp;
101 }
102 
103 inline void Delete(int v){
104     int x=find(v);
105     if(x==-1) return ;
106     int pp=getmax(lc[x]),nn=getmin(rc[x]);
107     if(lc[x]==0||rc[x]==0){
108         if(lc[x]==0&&rc[x]==0){
109             root=0; rc[0]=0;
110             return ;
111         }
112         else if(lc[x]==0){
113             rc[0]=rc[x]; fa[rc[x]]=0; root=rc[x]; rc[x]=0;
114             siz[x]=1;
115             return ;
116         }
117         else{
118             rc[0]=lc[x]; fa[lc[x]]=0; root=lc[x]; lc[x]=0;
119             siz[x]=1;
120             return ;
121         }
122     }
123     splay(pp,0); splay(nn,root);
124     fa[lc[nn]]=0; siz[lc[nn]]=1; lc[nn]=0;
125     update(nn); update(pp);
126 }
127 
128 inline int rank(int x,int v){
129     if(x==0) return 1;
130     if(v<=key[x]) return rank(lc[x],v);
131     else return siz[lc[x]]+1+rank(rc[x],v);
132 }
133 inline int findkth(int x,int k){
134     if(siz[lc[x]]+1==k) return key[x];
135     if(siz[lc[x]]+1>k) return findkth(lc[x],k);
136     return findkth(rc[x],k-siz[lc[x]]-1); 
137 }
138 inline int pred(int rt,int v){
139     if(rt==0)  return v;
140     if(v<=key[rt]) return pred(lc[rt],v);
141     else{
142         int ans=pred(rc[rt],v);
143         if(ans==v) return key[rt]; 
144         return ans;
145     }
146 }
147 inline int succ(int rt,int v){
148     if(rt==0) return v;
149     if(v>=key[rt]) return succ(rc[rt],v);
150     else{
151         int ans=succ(lc[rt],v);  
152         if(ans==v) return key[rt];
153         return ans;
154     }
155 }
156 
157 int main(){
158     freopen("3224.in","r",stdin);
159     freopen("3224.out","w",stdout);
160     scanf("%d",&T);
161     while (T--){
162         int kin,num;
163         scanf("%d%d",&kin,&num);
164         if(kin==1) insert(num);
165         else if(kin==2) Delete(num);
166         else if(kin==3) printf("%d\n",rank(root,num));
167         else if(kin==4) printf("%d\n",findkth(root,num));
168         else if(kin==5) printf("%d\n",pred(root,num)); 
169         else if(kin==6) printf("%d\n",succ(root,num));
170     }
171     return 0;
172 }

 

posted @ 2016-04-07 19:38  CXCXCXC  阅读(324)  评论(0编辑  收藏  举报