3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 15689 Solved: 6832
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737
84185
492737
HINT
1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]
Source
Splay版普通平衡树
#include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #include<iomanip> #include<vector> #include<string> #include<bitset> #include<queue> #include<map> #include<set> using namespace std; typedef long long ll; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();} return x*f; } void print(int x) {if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');} const int N=100100; int root,sz; int fa[N],ch[N][2],size[N],V[N]; inline void pushup(int k) {size[k]=1+size[ch[k][0]]+size[ch[k][1]];} inline void rotate(int x,int &k) { int y=fa[x],z=fa[y],l,r; l=(ch[y][1]==x),r=l^1; y!=k ? ch[z][ch[z][1]==y]=x : k=x; fa[x]=z,fa[y]=x,fa[ch[x][r]]=y; ch[y][l]=ch[x][r],ch[x][r]=y; pushup(y),pushup(x); } void splay(int x,int &k) { int y,z; while(x!=k) { y=fa[x],z=fa[y]; if(y!=k) { if((ch[z][0]==y)^(ch[y][0]==x)) rotate(x,k); else rotate(y,k); } rotate(x,k); } } void insert(int &k,int pre,int x) { if(!k) { k=++sz; size[k]=1,V[k]=x,fa[k]=pre; splay(k,root); return ; } size[k]++; x<V[k] ? insert(ch[k][0],k,x) : insert(ch[k][1],k,x); } void del(int &k,int pre,int x) { if(!k) return ; if(V[k]==x) { if(!ch[k][0]) k=ch[k][1],fa[k]=pre; else if(!ch[k][1]) k=ch[k][0],fa[k]=pre; else { int u=ch[k][0]; while(ch[u][1]) u=ch[u][1]; splay(u,ch[k][0]); ch[u][1]=ch[k][1], fa[ch[k][1]]=u, fa[u]=pre; pushup(u); k=u; } return ; } size[k]--; x<V[k] ? del(ch[k][0],k,x) : del(ch[k][1],k,x); pushup(k); } int query_rank(int k,int x) { if(!k) return 0; if(V[k]<x) return size[ch[k][0]]+1+query_rank(ch[k][1],x); return query_rank(ch[k][0],x); } int query_num(int k,int x) { if(!k) return 0; if(x>size[ch[k][0]]+1) return query_num(ch[k][1],x-size[ch[k][0]]-1); else if(x<=size[ch[k][0]]) return query_num(ch[k][0],x); return V[k]; } int ans; void query_pre(int k,int x) { if(!k) return ; V[k]<x ? (ans=V[k],query_pre(ch[k][1],x)) : query_pre(ch[k][0],x); } void query_sub(int k,int x) { if(!k) return ; V[k]>x ? (ans=V[k],query_sub(ch[k][0],x)) : query_sub(ch[k][1],x); } int main() { register int Q=read(),x,opt; while(Q--) { opt=read(),x=read(); switch(opt) { case 1:insert(root,0,x);break; case 2:del(root,0,x);break; case 3:print(query_rank(root,x)+1);puts("");break; case 4:print(query_num(root,x));puts("");break; case 5:query_pre(root,x);print(ans);puts("");break; case 6:query_sub(root,x);print(ans);puts("");break; default :break; } } return 0; }