不太友好的代码
题面依旧是普通平衡树
//Writer : Hsz %WJMZBMR%tourist%hzwer
#include <bits/stdc++.h>
#define LL long long
#define M(a,b) memset(a,b,sizeof a)
using namespace std;
const int inf=0x3fffffff,N=400005;
struct Node{
Node *ch[2];int val,prio,siz;
Node(){}
Node(int w,Node *son){ch[0]=ch[1]=son,val=w,prio=rand(),siz=1;}
void update(){ siz=ch[0]->siz+ch[1]->siz+1;}
}nil,base[N];
typedef Node* node;
node null,len,Root;
void init(){
nil=Node(0,NULL);null=&nil;
null->ch[0]=null->ch[1]=null;
null->siz=0;len=base;Root=null;
}
node newnode(int v){*len=Node(v,null);return len++;}
node merge(node a,node b){
if(a==null) return b;
if(b==null) return a;
if(a->prio>b->prio) {a->ch[1]=merge(a->ch[1],b);a->update();return a;}
b->ch[0]=merge(a,b->ch[0]);b->update();return b;
}
void split(node x,int k,node &l,node &r){
if(x==null) {l=r=null;return ;}
if(x->val<=k){l=x;split(l->ch[1],k,l->ch[1],r);}
else r=x,split(r->ch[0],k,l,r->ch[0]);
x->update();
}
void insert(int k){
node l,r;
split(Root,k,l,r);
Root=merge(merge(l,newnode(k)),r);
}
void del(int k){
node l,mid,r;
split(Root,k-1,l,r);
split(r,k,mid,r);
Root=merge(l,merge(merge(mid->ch[0],mid->ch[1]),r));
}
int get_val(node x,int k){
if(k<=x->ch[0]->siz) return get_val(x->ch[0],k);
else if(k>x->ch[0]->siz+1) return get_val(x->ch[1],k-x->ch[0]->siz-1);
return x->val;
}
int get_rnk(node x,int d){
while(x->ch[d]!=null) x=x->ch[d];
return x->val;
}
inline int read(){
int tot=0,f=1;char ch=getchar();
while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=getchar();}
while ('0'<=ch&&ch<='9') tot=tot*10+ch-48,ch=getchar();
return tot*f;
}
int T,opt,x;
int main() {
srand(568877201);
init();
T=read();
for(int i=1;i<=T;i++){
opt=read();
switch(opt){
case 1:insert(read());break;
case 2:del(read());break;
case 3:x=read();node l,r;split(Root,x-1,l,r);printf("%d
",l->siz+1);Root=merge(l,r);break;
case 4:printf("%d
",get_val(Root,read()));break;
case 5:x=read();node l1,r1;split(Root,x-1,l1,r1);printf("%d
",get_rnk(l1,1));Root=merge(l1,r1);break;
case 6 :x=read();node l2,r2;split(Root,x,l2,r2);printf("%d
",get_rnk(r2,0));Root=merge(l2,r2);break;
}
}
return 0;
}