#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<set>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int inf=0x7fffffff;
struct Node{
int ch[2],size,fa,key,cnt;
}tree[100005];
int cnt,root,tot;
int n,m;
int a[105];
int read(){
char c;int w,f;w=0;f=1;c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9'){w=w*10+c-'0';c=getchar();}
return w*f;
}
int D(int &x){
return tree[tree[x].fa].ch[1]==x;
}
void link(int f,int d,int s){
tree[f].ch[d]=s;tree[s].fa=f;
}
void updata(int x){
tree[x].size=tree[tree[x].ch[0]].size+tree[tree[x].ch[1]].size+tree[x].cnt;
}
int New(int x,int c){
tree[++tot].key=x;
tree[tot].fa=0;
tree[tot].size=tree[tot].cnt=c;
tree[tot].ch[0]=tree[tot].ch[1]=0;
return tot;
}
void rotate(int x){
int y=tree[x].fa;int z=tree[y].fa;int d=D(x);
if(z) link(z,D(y),x);
else tree[x].fa=0;
link(y,d,tree[x].ch[!d]);link(x,!d,y);
updata(x);updata(y);
}
void splay(int x,int f=0){
for(int y,z;tree[x].fa!=f;rotate(x)){
y=tree[x].fa;z=tree[y].fa;
if(z!=f){
D(x)==D(y)?rotate(y):rotate(x);
}
}
if(!f) root=x;
}
int build(int l,int r){
if(l>r) return 0;
int mid=(l+r)>>1;
int u=New(a[m],1);
int ch0=build(l,mid-1);if(ch0) link(u,0,ch0);
int ch1=build(mid+1,r);if(ch1) link(u,1,ch1);
updata(u);return u;
}
void Initsy(){
root=New(-inf,0);int u=New(inf,1);link(root,1,u);
}
int find(int x){
int u=root;int f=0;
while(u){
if(x<tree[u].key){
f=u;u=tree[u].ch[0];
}
else if(x==tree[u].key){
splay(u);
return root;
}
else f=u;u=tree[u].ch[1];
}
return f;
}
int kth(int x){
int u=root;
while(1){
if(tree[u].ch[0]&&x<=tree[tree[u].ch[0]].size) u=tree[u].ch[0];
else{
int tmp=tree[tree[u].ch[0]].size+tree[u].cnt;
if(x<=tmp) return u;
x-=tmp;u=tree[u].ch[1];
}
}
}
int pre(){
int u=tree[root].ch[0];
while(tree[u].ch[1]){
u=tree[u].ch[1];
}
return u;
}
int next(){
int u=tree[root].ch[1];
while(tree[u].ch[0]){
u=tree[u].ch[0];
}
return u;
}
void insert(int x){
int u=find(x);
if(tree[root].key==x){
tree[root].cnt++;
tree[root].size++;
return ;
}
int v=New(x,1);
link(u,tree[u].key<x,v);
splay(v);
}
bool del(int x){
find(x);
if(tree[root].key!=x) return false;
if(tree[root].cnt>1){
tree[root].size--;tree[root].cnt--;return true;
}
splay(pre());
link(root,1,tree[tree[root].ch[1]].ch[1]);
updata(root);return true;
}
void print(int u, int d=0){
if(!u)return;
// if(abs(key[u])==oo) return;
print(tree[u].ch[0],d+1);
for(int i=0;i<d;++i)printf(" ");
printf("k%d:sz%d:u%d:fa%d\n",tree[u].key,tree[u].size,tree[u].cnt,tree[u].fa);
print(tree[u].ch[1],d+1);
}
int main(){
// freopen("p3319.in","r",stdin);
// freopen("p3319.out","w",stdout);
int po,x;tot=0;
n=read();Initsy();
//print(root,0);
//printf("%d\n",n);
while(n--){
po=read();x=read();
// print(root,0);
if(po==1){
insert(x);
continue;
}
if(po==2){
del(x);
continue;
}
if(po==3){
find(x);
printf("%d\n",tree[tree[root].ch[0]].size+1);
continue;
}
if(po==4){
printf("%d\n",tree[kth(x)].key);
continue;
}
if(po==5){
insert(x);
printf("%d\n",tree[pre()].key);
del(x);
continue;
}
if(po==6){
/**/insert(x);
printf("%d\n",tree[next()].key);
del(x);
continue;
}
}
return 0;
}
普通平衡树
最新推荐文章于 2022-10-29 19:57:41 发布