下了数据发现是卡在某一次查询中找到了0号节点,死循环了,但是数据太大也不好找问题
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int cnt,root;
int n,k;
struct node{
int l=0,r=0;
int v=0;
int len=0;//高度
int cnt=0;
int size=0;
int rd=0;//随机优先级
}t[N];
void renew(int g){
t[g].len=max(t[t[g].l].len,t[t[g].r].len)+1;
t[g].size=t[t[g].l].size+t[t[g].r].size+t[g].cnt;
}
int left(int g){//左旋
int k=g;//旧根节点
g=t[g].r;
t[k].r=t[g].l;
t[g].l=k;
renew(k);
renew(g);
return g;
}
int right(int g){//右旋
int k=g;//旧根节点
g=t[g].l;
t[k].l=t[g].r;
t[g].r=k;
renew(k);
renew(g);
return g;
}
int lr(int g){
t[g].l=left(t[g].l);
return right(g);
}
int rl(int g){
t[g].r=right(t[g].r);
return left(g);
}
int in(int p,int x){//插入
//cout<<t[p].v<<"s"<<x<<"s"<<t[p].cnt<<endl;
if(t[p].v==x){
t[p].cnt++;
}else if(x<t[p].v){
if(!t[p].l){
t[p].l=++cnt;
t[cnt].v=x;
t[cnt].len=1;
t[cnt].size=1;
t[cnt].cnt=1;
t[cnt].rd=rand();
}else{
t[p].l=in(t[p].l,x);
int h=t[t[p].l].len-t[t[p].r].len;
if(h>1){
if(x<t[t[p].l].v){//ll
p=right(p);
}else{
p=lr(p);
}
}
}
}else{
if(!t[p].r){
t[p].r=++cnt;
t[cnt].v=x;
t[cnt].len=1;
t[cnt].size=1;
t[cnt].cnt=1;
t[cnt].rd=rand();
}else{
t[p].r=in(t[p].r,x);
int h=t[t[p].l].len-t[t[p].r].len;
if(h<-1){
if(x>t[t[p].r].v){//rr
p=left(p);
}else{
p=rl(p);
}
}
}
}
renew(p);
return p;
}
void pre(int x,int d){//前驱
int now=root,ans;
int w=t[t[now].l].size+t[now].cnt;//排名
while(now){
//cout<<t[now].v<<" "<<t[now].size<<endl;
//cout<<w<<endl;
if(t[now].v<x){
ans=t[now].v;
w+=t[t[now].r].cnt+t[t[t[now].r].l].size;
now=t[now].r;
}else{
w-=t[now].cnt+t[t[t[now].l].r].size;
now=t[now].l;
}
}
if(d==3){
printf("%d\n",w+1);
}else{
printf("%d\n",ans);
}
}
int next(int x){//后缀
int now=root,ans;
while(now){
if(t[now].v>x){
ans=t[now].v;
now=t[now].l;
}else{
now=t[now].r;
}
}
return ans;
}
int search(int w){//查询
int p=root;
int f=t[t[p].l].size+t[p].cnt;
//cout<<f<<"s";
int u=0;
while(1){
if(w==3855){
if(u<=30){
cout<<t[p].v<<" "<<t[p].cnt<<" "<<p<<endl;
u++;
}
}
if(f>=w&&f-t[p].cnt+1<=w){
return t[p].v;
}
if(f<w){
f+=t[t[p].r].cnt+t[t[t[p].r].l].size;
p=t[p].r;
}else{
f-=t[p].cnt+t[t[t[p].l].r].size;
p=t[p].l;
}
}
}
void del(int &p,int x){//删除
//if(!p)return;
//cout<<t[p].v<<endl;
if(t[p].v==x){
if(t[p].cnt>1){
t[p].cnt--;
t[p].size--;
}else{
if(t[p].l||t[p].r){
if(!t[p].r||t[t[p].l].rd>t[t[p].r].rd){
//cout<<t[p].v<<endl;
p=right(p);
del(t[p].r,x);
}else{
p=left(p);
del(t[p].l,x);
}
}else{
t[p].l=t[p].r=t[p].v=t[p].size=t[p].len=t[p].cnt=0;
p=0;
t[p].l=t[p].r=t[p].v=t[p].size=t[p].len=t[p].cnt=0;
return;
}
}
}else if(t[p].v<x){
del(t[p].r,x);
}else{
del(t[p].l,x);
}
renew(p);
}
void dfs(int g){
if(!g)return;
dfs(t[g].l);
//cout<<g<<" ";
cout<<t[g].v<<" "<<t[g].cnt<<endl;
dfs(t[g].r);
}
int main(){
// freopen("P3369_8.in","r",stdin);
// freopen("data.out","w",stdout);
cin>>n;
while(n--){
int op,tt;
cin>>op>>tt;
if(op==1){
if(!k){
//cout<<tt<<"s";
t[++cnt].len=1;
t[cnt].v=tt;
t[cnt].size=1;
t[cnt].cnt=1;
root=cnt;
}else{
//cout<<endl;
root=in(root,tt);
}
k++;
}else if(op==2){
k--;
del(root,tt);
}else if(op==3){
pre(tt,op);
}else if(op==4){
printf("%d\n",search(tt));
}else if(op==5){
pre(tt,op);
}else{
printf("%d\n",next(tt));
}
//cout<<endl<<endl;
//cout<<root<<endl;
}
//cout<<root<<" ";
// cout<<endl;
// dfs(root);
}