传送门
就是普通平衡树,可以拿来练非旋 treap t r e a p 的板子。
贴个代码:
#include
#define N 300005
using namespace std;
typedef pair<int,int> res;
int n;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
struct Treap{
int son[N][2],root,cnt,siz[N],rd[N],val[N];
inline int build(int v){siz[++cnt]=1,val[cnt]=v,rd[cnt]=rand(),son[cnt][0]=son[cnt][1]=0;return cnt;}
inline void pushup(int p){siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;}
inline int merge(int a,int b){
if(!a||!b)return a+b;
if(rd[a]1]=merge(son[a][1],b),pushup(a);return a;}
son[b][0]=merge(a,son[b][0]),pushup(b);return b;
}
inline res split(int p,int k){
if(!p)return res(0,0);
res tmp,ans;
if(siz[son[p][0]]>=k){tmp=split(son[p][0],k),son[p][0]=tmp.second,pushup(p),ans.first=tmp.first,ans.second=p;return ans;}
tmp=split(son[p][1],k-siz[son[p][0]]-1),son[p][1]=tmp.first,pushup(p),ans.first=p,ans.second=tmp.second;return ans;
}
inline int rank(int p,int v){
if(!p)return 0;
if(val[p]>v)return rank(son[p][0],v);
return rank(son[p][1],v)+siz[son[p][0]]+1;
}
inline int kth(int k){
res x=split(root,k),y=split(x.first,k-1);
root=merge(merge(y.first,y.second),x.second);
return val[y.second];
}
inline int find(int p,int v){
if(!p)return 0;
if(val[p]>=v)return find(son[p][0],v);
return find(son[p][1],v)+siz[son[p][0]]+1;
}
inline void ins(int v){
int k=rank(root,v);
res x=split(root,k);
int p=build(v);
root=merge(merge(x.first,p),x.second);
}
inline void del(int v){
int k=rank(root,v);
res x=split(root,k);
root=merge(split(x.first,k-1).first,x.second);
}
inline int pre(int p,int v){
if(!p)return -0x3f3f3f3f;
if(val[p]return max(val[p],pre(son[p][1],v));
return pre(son[p][0],v);
}
inline int suf(int p,int v){
if(!p)return 0x3f3f3f3f;
if(val[p]>v)return min(val[p],suf(son[p][0],v));
return suf(son[p][1],v);
}
}T;
int main(){
srand(time(NULL));
n=read();
while(n--){
int op=read(),x=read();
switch(op){
case 0:{T.ins(x);break;}
case 1:{T.del(x);break;}
case 2:{printf("%d\n",T.kth(x));break;}
case 3:{printf("%d\n",T.find(T.root,x));break;}
case 4:{int tmp=T.pre(T.root,x);printf("%d\n",tmp==-0x3f3f3f3f?-1:tmp);break;}
default:{int tmp=T.suf(T.root,x);printf("%d\n",tmp==0x3f3f3f3f?-1:tmp);break;}
}
}
return 0;
}