3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 14814 Solved: 6441
[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
Sample Output
106465
84185
492737
HINT
1.n的数据范围:n<=100000
2.每个数的数据范围:[-2e9,2e9]
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define MAXN 100010
int ch[MAXN][2],fa[MAXN],siz[MAXN],cnt[MAXN],data[MAXN],root,m,n,tot;
#define lc ch[u][0]
#define rc ch[u][1]
inline void UpDate(int 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]];
return ;
}
inline void Create(int x){
tot++; data[tot]=x; cnt[tot]=siz[tot]=1;
ch[tot][0]=ch[tot][1]=fa[tot]=0;
return ;
}
inline int getson(int x){ return x==ch[fa[x]][1]; }
void Rotate(int x){
int f=fa[x],ffa=fa[f],k=getson(x);
ch[f][k]=ch[x][k^1];fa[ch[f][k]]=f;
ch[x][k^1]=f;fa[f]=x; fa[x]=ffa;
if(ffa) ch[ffa][f==ch[ffa][1]]=x;
UpDate(f);UpDate(x);
}
void Splay(int x){
for(int f;f=fa[x];Rotate(x))
if(fa[f]) Rotate(getson(x)==getson(f)?f:x);
root=x;//这句很重要
}
void Insert(int x){
if(!root){ Create(x);root=tot; }
else{
int now=root,f=0;
while(1){
if(data[now]==x){
cnt[now]++;siz[now]++;
Splay(now);break;
}
f=now; now=ch[f][x>data[f]];
if(!now){//走到了叶节点
Create(x);fa[tot]=f;
ch[f][x>data[f]]=tot;Splay(tot);
break;
}
}
}
}
int Get_Rank(int x){//查询数x的排名
int now=root,ans=0;
while(1){
if(x<data[now]) now=ch[now][0];
else {
ans+=siz[ch[now][0]];
if(x==data[now]){ Splay(now);return ans+1; }
ans+=cnt[now]; now=ch[now][1];
}
}
}
int Get_Rank_Is(int x){//查询排名为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 data[now];
x-=tmp;now=ch[now][1];
}
}
}
inline int Get_Next(){// hou ji
int p=ch[root][1];
while(ch[p][0]) p=ch[p][0];
return p;
}
inline int Get_Pre(){//qian qu
int p=ch[root][0];
while(ch[p][1]) p=ch[p][1];
return p;
}
inline void clear(int x){
ch[x][0]=ch[x][1]=fa[x]=cnt[x]=siz[x]=data[x]=0;
}
void Delete(int x){
int rank_x=Get_Rank(x);
//查了一下x 顺便将它转到了根的位置
//有这一句就能A 没有差别很大
if(cnt[root]>1){ cnt[root]--;siz[root]--;return ; }
if(!ch[root][0]&&!ch[root][1]){ clear(root);root=0;return ; }
if(!ch[root][0]){
int tmp=root;
root=ch[root][1];
fa[root]=0;
clear(tmp);
return;
}
if(!ch[root][1]){
int tmp=root;
root=ch[root][0];
fa[root]=0;
clear(tmp);
return;
}
int pre=Get_Pre(),tmp=root;
Splay(pre);
ch[root][1]=ch[tmp][1]; fa[ch[tmp][1]]=root;
clear(tmp);UpDate(root);
}
int main(){
scanf("%d",&n);
while(n--){
int opt,x;
scanf("%d%d",&opt,&x);
if(opt==1) Insert(x);
if(opt==2) Delete(x);
if(opt==3) printf("%d\n",Get_Rank(x));
if(opt==4) printf("%d\n",Get_Rank_Is(x));
if(opt==5) Insert(x),printf("%d\n",data[Get_Pre()]),Delete(x);
if(opt==6) Insert(x),printf("%d\n",data[Get_Next()]),Delete(x);
}
}