Tyvj 1728 普通平衡树
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
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
Sample Output
106465 84185 492737
Hint
1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]
splay模板题
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1000005;
int ch[N][2],f[N],siz[N],cnt[N],key[N];
int sz,root;
void clear(int x) {ch[x][0]=ch[x][1]=f[x]=siz[x]=cnt[x]=key[x]=0; } //删除后,清理数据
int get(int x){return ch[f[x]][1]==x; } //判断当前点是左儿子还是右儿子
void update(int x) //更新当前点的siz
{
if(x)
{
siz[x]=cnt[x];
if(ch[x][0]) siz[x]+=siz[ch[x][0]];
if(ch[x][1]) siz[x]+=siz[ch[x][1]];
}
}
void rot(int x) //把x向上转一次
{
int old=f[x],oldf=f[old],w=get(x);
ch[old][w]=ch[x][w^1];f[ch[old][w]]=old;
ch[x][w^1]=old;f[old]=x;f[x]=oldf;
if(oldf) ch[oldf][ch[oldf][1]==old]=x;
update(old);update(x);
}
void splay(int x) //把x翻转成根
{
for(int fa;fa=f[x];rot(x))//如果父节点和x节点在同侧先转父节点后转x节点,异侧先转x
if(f[fa])
rot(get(x)==get(fa)?fa:x);
root=x;
}
void Insert(int x) //插入节点
{
if(root==0){sz++;ch[sz][0]=ch[sz][1]=f[sz]=0;root=sz;siz[sz]=cnt[sz]=1;key[sz]=x;return; } //为空时
int now=root,fa=0;
while(1)
{
if(x==key[now]) {cnt[now]++;update(now);update(fa);splay(now);break; }
fa=now;
now=ch[now][key[now]<x];
if(now==0)
{
sz++;
ch[sz][0]=ch[sz][1]=0;
f[sz]=fa;
siz[sz]=cnt[sz]=1;
ch[fa][key[fa]<x]=sz;
key[sz]=x;
update(fa);
splay(sz);
break;
}
}
}
int Find(int x)
{
int now=root,ans=0;
while(1)
{
if(x<key[now]) now=ch[now][0]; //小于在左儿子
else
{
ans+=(ch[now][0]?siz[ch[now][0]]:0); //加上左儿子的siz
if(x==key[now]) {splay(now); return ans+1; }
ans+=cnt[now];
now=ch[now][1];
}
}
}
int Findx(int x)
{
int now=root;
while(1)
{
if(ch[now][0]&&x<=siz[ch[now][0]]) now=ch[now][0];
else
{
int tmp=(ch[now][0]?siz[ch[now][0]]:0)+cnt[now];
if(x<=tmp) return key[now];
x-=tmp; now=ch[now][1];
}
}
}
int pre() //前驱,左儿子中最大的
{
int now=ch[root][0];
while(ch[now][1]) now=ch[now][1];
return now;
}
int nex() //后继,右儿子中最小的
{
int now=ch[root][1];
while(ch[now][0]) now=ch[now][0];
return now;
}
void del(int x) //删除节点
{
int w=Find(x);
if(cnt[root]>1){cnt[root]--; update(root); return;}
if(!ch[root][0]&&!ch[root][1]){clear(root);root=0;return; }
if(!ch[root][0])
{
int oldroot=root;
root=ch[root][1]; f[root]=0;
clear(oldroot);
return;
}
else if(!ch[root][1])
{
int oldroot=root;
root=ch[root][0]; f[root]=0;
clear(oldroot);
return;
}
int lb=pre(),oldroot=root;
splay(lb);
ch[root][1]=ch[oldroot][1];
f[ch[oldroot][1]]=root;
clear(oldroot);
update(root);
}
int main()
{
int n;
scanf("%d",&n);
int opt,x;
for(int i=0;i<n;i++)
{
scanf("%d%d",&opt,&x);
if(opt==1) Insert(x);
else if(opt==2) del(x);
else if(opt==3) printf("%d\n",Find(x));
else if(opt==4) printf("%d\n",Findx(x));
else if(opt==5)
{
Insert(x);
printf("%d\n",key[pre()]);
del(x);
}
else if(opt==6)
{
Insert(x);
printf("%d\n",key[nex()]);
del(x);
}
}
return 0;
}