https://www.luogu.org/problemnew/show/P3369
题目描述
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
- 插入xx数
- 删除xx数(若有多个相同的数,因只删除一个)
- 查询xx数的排名(排名定义为比当前数小的数的个数+1+1。若有多个相同的数,因输出最小的排名)
- 查询排名为xx的数
- 求xx的前驱(前驱定义为小于xx,且最大的数)
- 求xx的后继(后继定义为大于xx,且最小的数)
输入输出格式
输入格式:
第一行为nn,表示操作的个数,下面nn行每行有两个数optopt和xx,optopt表示操作的序号( 1 \leq opt \leq 61≤opt≤6 )
输出格式:
对于操作3,4,5,63,4,5,6每行输出一个数,表示对应答案
输入样例
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
输出样例
106465
84185
492737
528ms Splay感觉也不慢
/*
1、插入x数
2、删除x数(若有多个相同的数,因只删除一个)
3、查询x数的排名(排名定义为比当前数小的数的个数+1。若有多个相同的数,因输出最小的排名)
4、查询排名为x的数
5、求x的前驱(前驱定义为小于x,且最大的数)
6、求x的后继(后继定义为大于x,且最小的数)
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int ch[maxn][2],f[maxn],sz[maxn],resz[maxn],val[maxn],tot;
int root;
void pushup(int rt)
{
if(rt) sz[rt]=sz[ch[rt][0]]+sz[ch[rt][1]]+resz[rt];
}
void Rotate(int x,int p)
{
int y=f[x],z=f[y];
ch[y][!p]=ch[x][p],f[ch[x][p]]=y;
ch[z][ch[z][1]==y]=x,f[x]=z;
ch[x][p]=y,f[y]=x;
pushup(y),pushup(x),pushup(z);
}
void splay(int x,int goal)
{
while(f[x]!=goal)
{
int y=f[x],z=f[y];
if(f[y]==goal) Rotate(x,ch[y][0]==x);
else
{
int p=(ch[z][0]==y);
if(ch[y][!p]==x)
{
Rotate(y,p);
Rotate(x,p);
}
else
{
Rotate(x,!p);
Rotate(x,p);
}
}
}
if(goal==0) root=x;
}
void Insert(int &rt,int x,int fa)
{
if(rt==0)
{
rt=++tot;
f[rt]=fa;
ch[rt][0]=ch[rt][1]=0;
sz[rt]=resz[rt]=1;
val[rt]=x;
return;
}
sz[rt]++;
if(val[rt]==x) resz[rt]++;
else if(val[rt]>x) Insert(ch[rt][0],x,rt);
else Insert(ch[rt][1],x,rt);
}
int kth(int rt,int x)
{
if(val[rt]==x) return rt;
else if(val[rt]>x) return kth(ch[rt][0],x);
else return kth(ch[rt][1],x);
}
int pre()
{
int now=ch[root][0];
while(ch[now][1])
{
now=ch[now][1];
}
return now;
}
int aft()
{
int now=ch[root][1];
while(ch[now][0])
{
now=ch[now][0];
}
return now;
}
void delnode(int rt,int fa)
{
if(resz[rt]>1)
{
sz[rt]--;
resz[rt]--;
}
else
{
f[rt]=ch[rt][0]=ch[rt][1]=val[rt]=resz[rt]=sz[rt]=0;
ch[fa][0]=0;
}
}
void Delete(int k)
{
int x=kth(root,k);
splay(x,0);
int y=pre();
int z=aft();
splay(y,0),splay(z,y);
delnode(ch[z][0],z);
pushup(ch[z][0]);
pushup(z);
pushup(y);
}
int atrank(int rt,int k)
{
int x=kth(root,k);
splay(x,0);
return sz[ch[x][0]]+1;
}
int pdrank(int rt,int x)
{
if(sz[ch[rt][0]]>=x) return pdrank(ch[rt][0],x);
else if(sz[ch[rt][0]]+resz[rt]<x) return pdrank(ch[rt][1],x-sz[ch[rt][0]]-resz[rt]);
else return val[rt];
}
int prenode(int rt,int x)
{
int ans=-1e8;
while(rt)
{
if(val[rt]>=x) rt=ch[rt][0];
else
{
ans=max(ans,val[rt]);
rt=ch[rt][1];
}
}
return ans;
}
int aftnode(int rt,int x)
{
int ans=1e8;
while(rt)
{
if(val[rt]<=x) rt=ch[rt][1];
else
{
ans=min(ans,val[rt]);
rt=ch[rt][0];
}
}
return ans;
}
int main()
{
int Q,n,x;
tot=0; //一定要有,是建立新节点的编号
scanf("%d",&n);
Insert(root,1e7+10,0); //!!splay重点建两个数把这个些包起来
Insert(root,-1e7-10,0);
f[0]=sz[0]=resz[0]=ch[0][0]=ch[0][1]=val[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&Q);
if(Q==1)
{
scanf("%d",&x);
Insert(root,x,0);
x=kth(root,x); //!!!!!!!插入数后就旋转,这样可以使树变得平衡,大大缩短时间
splay(x,0); //!!!!!!
}
else if(Q==2)
{
scanf("%d",&x);
Delete(x);
}
else if(Q==3)
{
scanf("%d",&x);
printf("%d\n",atrank(root,x)-1);
}
else if(Q==4)
{
scanf("%d",&x);
printf("%d\n",pdrank(root,x+1));
}
else if(Q==5)
{
scanf("%d",&x);
printf("%d\n",prenode(root,x));
}
else
{
scanf("%d",&x);
printf("%d\n",aftnode(root,x));
}
}
return 0;
}