3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11333 Solved: 4840
[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
Source
用来贴splay板子

#include<bits/stdc++.h> using namespace std; struct tree{ int fa,size,num,tim; int s[3]; tree (){ fa = size = num = tim = s[0] = s[1] = s[2] = 0; } }t[100010]; int n,tot = 0,root = 0; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void rotate(int x,int dir){ int y; y = t[x].fa; t[y].size = t[y].size - t[x].size + t[t[x].s[dir]].size; t[x].size = t[x].size + t[y].size - t[t[x].s[dir]].size; t[y].s[3-dir] = t[x].s[dir]; if (t[x].s[dir]) t[t[x].s[dir]].fa = y; t[x].fa = t[y].fa; if (t[y].fa){ if (y == t[t[y].fa].s[1]) t[t[y].fa].s[1] = x; else t[t[y].fa].s[2] = x; } t[y].fa = x; t[x].s[dir] = y; } inline void splay(int x){ int y; while (t[x].fa){ y = t[x].fa; if (!t[y].fa){ if (x == t[y].s[1]) rotate(x,2); else rotate(x,1); }else { if (y == t[t[y].fa].s[1]){ if (x == t[y].s[1])rotate(y,2),rotate(x,2); else rotate(x,1),rotate(x,2); }else { if (x == t[y].s[2])rotate(y,1),rotate(x,1); else rotate(x,2),rotate(x,1); } } } root = x; } int search(int x,int k){ while (t[x].num != k){ if (k == t[x].num) return x; if (k < t[x].num){ if (!t[x].s[1]) break; x=t[x].s[1]; } else{ if (t[x].s[2] == 0) break; x=t[x].s[2]; } } return x; } void ins(int w) { int k,kk;bool flag; if (!tot){ tot = 1; t[1].fa = 0; t[1].size=1; t[1].num = w; root = 1; t[1].tim = 1; return ; } k=search(root,w); if (t[k].num==w){ t[k].tim++; kk=k; flag=1; } else{ tot++; t[tot].num=w; t[tot].fa=k; t[tot].size=1; t[tot].tim=1; if (t[k].num>w) t[k].s[1]=tot;else t[k].s[2]=tot; flag=0; } while (k){ t[k].size++; k=t[k].fa; } if (flag) splay(kk);else splay(tot); } int Extreme(int x,int w) { const int lala[3]={0,2147483647,-2147483647}; int k,tmp; k=search(x,lala[w]); tmp=t[k].num; splay(k); return tmp; } void del(int x) { int k,y; k=search(root,x); if (t[k].num!=x) splay(k);else { splay(k); if (t[k].tim>1) { t[k].tim--; t[k].size--; } else if (!t[k].s[1]) { y=t[k].s[2]; t[k].s[2]=0; t[k].size=0; t[k].num=0; t[k].tim=0; root=y; t[root].fa=0; } else { t[t[k].s[1]].fa=0; y=Extreme(t[k].s[1],1); t[root].s[2]=t[k].s[2]; t[root].size=t[root].size+t[t[k].s[2]].size; if (t[root].s[2]!=0) t[t[root].s[2]].fa=root; t[k].num=0;t[k].s[1];t[k].s[2]=0; t[k].tim=0; } } } int pred(int x) { int k; k=search(root,x); splay(k); if (t[k].num<x) return t[k].num; return Extreme(t[k].s[1],1); } int succ(int x) { int k; k=search(root,x); splay(k); if (t[k].num>x) return t[k].num; return Extreme(t[k].s[2],2); } int kth(int x,int w) { int i,tmp; i=root; while (!((x>=t[t[i].s[w]].size+1)&&(x<=t[t[i].s[w]].size+t[i].tim))&&(i!=0)) { if (x>t[t[i].s[w]].size+t[i].tim) { x=x-t[t[i].s[w]].size-t[i].tim; i=t[i].s[3-w]; } else i=t[i].s[w]; } tmp=i; splay(i); return tmp; } int find(int x){ int k; k=search(root,x);splay(k); root=k; return t[t[k].s[1]].size+1; } int main(){ int n = read(); for (int i=1;i<=n;i++){ int opt = read(),x = read(); switch (opt) { case 1:ins(x);break; case 2:del(x);break; case 3:printf("%d ",find(x));break; case 4:printf("%d ",t[kth(x,1)].num);break; case 5:printf("%d ",pred(x));break; case 6:printf("%d ",succ(x));break; default:break; } } return 0; }