对于每个节点 N,SBT 满足一下四点性质:
- size(N.left) >= size(N.right.left)
- size(N.left) >= size(N.right.right)
- size(N.right) >= size(N.left.left)
- size(N.right) >= size(N.left.right)
可以证明其高度是非常接近 l o g 2 ( n ) log_2(n) log2(n) 的,维护操作如下:
void maintain(node *&cur){
if(is(cur->rt)){
if(gsiz(cur->rt->lf)>gsiz(cur->lf)){zig(cur->rt);zag(cur);}
if(gsiz(cur->rt->rt)>gsiz(cur->lf)){zag(cur);}
}
if(is(cur->lf)){
if(gsiz(cur->lf->rt)>gsiz(cur->rt)){zag(cur->lf);zig(cur);}
if(gsiz(cur->lf->lf)>gsiz(cur->rt)){zig(cur);}
}
}
老样子,以下给出 SBT 的模板。(我比较喜欢 treap 这个名字所以类名就不改了)
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <string.h>
#include <vector>
#define lnt long long
#define inf 0x3f3f3f3f
using namespace std;
int xx;char ff,chh;inline int read(){
xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
#define lowbit(x) ((x)&(-x))
const int N=4e5;
struct node{
int v,cnt,siz;
node *lf,*rt;
};
struct treap{
node _new[N],*root;int top=0;
node *newnode(){return &_new[top++];}
#define is(cur) (cur==NULL? 0: 1)
#define gsiz(cur) (cur==NULL? 0: cur->siz)
#define pu(cur) (cur->siz=gsiz(cur->lf)+gsiz(cur->rt)+cur->cnt)
void zig(node *&cur);
void zag(node *&cur);
void maintain(node *&cur);
void insert(node *&cur,int v);
int query_rank(node *cur,int v);
int query_val(node *cur,int k);
int query_lt(node *cur,int v);
int query_nx(node *cur,int v);
void del(node *&cur,int v);
}t;
int main(){
int G=read();
while(G--){
int op=read(),y=read();
switch(op){
case 1: {t.insert(t.root,y);break;}
case 2: {t.del(t.root,y);break;}
case 3: {printf("%d\n",t.query_rank(t.root,y)+1);break;}
case 4: {printf("%d\n",t.query_val(t.root,y));break;}
case 5: {printf("%d\n",t.query_lt(t.root,y));break;}
case 6: {printf("%d\n",t.query_nx(t.root,y));break;}
}
}
return 0;
}
void treap::maintain(node *&cur){
if(is(cur->rt)){
if(gsiz(cur->rt->lf)>gsiz(cur->lf)){zig(cur->rt);zag(cur);}
if(gsiz(cur->rt->rt)>gsiz(cur->lf)){zag(cur);}
}
if(is(cur->lf)){
if(gsiz(cur->lf->rt)>gsiz(cur->rt)){zag(cur->lf);zig(cur);}
if(gsiz(cur->lf->lf)>gsiz(cur->rt)){zig(cur);}
}
}
void treap::insert(node *&cur,int v){
if(cur==NULL){cur=newnode();cur->cnt=cur->siz=1;cur->v=v;return;}
if(cur->v==v){++cur->cnt;++cur->siz;return;}
if(v<cur->v){insert(cur->lf,v);}
else{insert(cur->rt,v);}
pu(cur);maintain(cur);
}
int treap::query_rank(node *cur,int v){
if(cur==NULL){return 0;}
if(cur->v==v){return gsiz(cur->lf);}
if(v<cur->v){return query_rank(cur->lf,v);}
return query_rank(cur->rt,v)+cur->cnt+gsiz(cur->lf);
}
int treap::query_val(node *cur,int k){
if(k<=gsiz(cur->lf)){return query_val(cur->lf,k);}
if(k<=cur->cnt+gsiz(cur->lf)){return cur->v;}
return query_val(cur->rt,k-cur->cnt-gsiz(cur->lf));
}
int treap::query_lt(node *cur,int v){
if(!is(cur)){return -inf;}
if(cur->v>=v){return query_lt(cur->lf,v);}
return max(cur->v,query_lt(cur->rt,v));
}
int treap::query_nx(node *cur,int v){
if(!is(cur)){return inf;}
if(cur->v<=v){return query_nx(cur->rt,v);}
return min(cur->v,query_nx(cur->lf,v));
}
void treap::zig(node *&cur){
node *lf=cur->lf;
cur->lf=lf->rt;lf->rt=cur;
pu(cur);pu(lf);cur=lf;
}
void treap::zag(node *&cur){
node *rt=cur->rt;
cur->rt=rt->lf;rt->lf=cur;
pu(cur);pu(rt);cur=rt;
}
void treap::del(node *&cur,int v){
if(v<cur->v){del(cur->lf,v);}
else if(v>cur->v){del(cur->rt,v);}
else{
if(cur->cnt>1){--cur->cnt;--cur->siz;return;}
else switch((is(cur->lf)<<1)+is(cur->rt)){
case 0: {cur=NULL;return;}
case 1: {cur=cur->rt;return;}
case 2: {cur=cur->lf;return;}
case 3: {zig(cur);del(cur->rt,v);pu(cur);maintain(cur);return;}}
}pu(cur);
maintain(cur);
}
若有疑问请 @