我最近刷完团队题单后准备乱搞几个平衡树。
结果,还真就让我搞出来了。
The first tree
这个平衡树的效率在 P3369 上比我写的 treap,Splay,FHQ-treap,scapegoat都快。
这个平衡树也是旋转维护平衡。
旋转与 treap 大差不差。
具体可以看 这篇文章。
维护平衡的策略是,如果左右子树极其不平衡,就旋转一下。
分 与 的情况。
不对的话,就 zig 或 zag 一下。
其它操作与 treap 就大差不差。
代码。
#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,cnt,siz;
};
struct tree{
node *root,*null;
tree(){
null=new node;
null->l=null->r=null;
null->val=null->cnt=null->siz=0;
root=null;
}
void update(node *p){
p->siz=p->l->siz+p->r->siz+p->cnt;
}
void zig(node *&p){
node *q=p->l;
if(q==null) return;
p->l=q->r,q->r=p,p=q;
update(p->r),update(p);
}
void zag(node *&p){
node *q=p->r;
if(q==null) return;
p->r=q->l,q->l=p,p=q;
update(p->l),update(p);
}
int check(node *p){
if(p->r->siz*0.6>=double(p->l->siz)) return 1;
if(p->l->siz*0.6>=double(p->r->siz)) return 2;
return 0;
}
void insert(int x,node *&p){
if(p==null){
p=new node;
p->l=p->r=null;
p->val=x;
p->cnt=1;
p->siz=1;
return;
}
if(x==p->val) p->cnt++;
else if(x<p->val) insert(x,p->l);
else insert(x,p->r);
update(p);
int d=check(p);
if(d==1) zag(p);
if(d==2) zig(p);
}
void erase(int val,node *&p){
if(p==null) return;
if(val==p->val){
if(p->cnt>1){
p->cnt--,update(p);
return;
}
if(p->l!=null || p->r!=null){
if(p->r==null)
zig(p),erase(val,p->r);
else
zag(p),erase(val,p->l);
update(p);
}
else p=null;
return;
}
if(val<p->val) erase(val,p->l);
else erase(val,p->r);
int d=check(p);
if(d==1) zag(p);
if(d==2) zig(p);
update(p);
}
int ranks(int x){
int ans=1;
node *p=root;
while(p!=null){
if(p->val>=x) p=p->l;
else{
ans+=p->l->siz+p->cnt;
p=p->r;
}
}
return ans;
}
int get(int x,node *p){
if(p->l->siz<x && x<=p->l->siz+p->cnt) return p->val;
else if(p->l->siz+p->cnt<x) return get(x-p->l->siz-p->cnt,p->r);
else return get(x,p->l);
}
int pre_find(int val){
return get(ranks(val)-1,root);
}
int next_find(int val){
return get(ranks(val+1),root);
}
}s;
int main() {
int t;
cin>>t;
while(t--){
int opt,x;
cin>>opt>>x;
if(opt==1) s.insert(x,s.root);
if(opt==2) s.erase(x,s.root);
if(opt==3) cout<<s.ranks(x)<<"\n";
if(opt==4) cout<<s.get(x,s.root)<<"\n";
if(opt==5) cout<<s.pre_find(x)<<"\n";
if(opt==6) cout<<s.next_find(x)<<"\n";
}
return 0;
}The second tree
这个平衡树的效率并不咋高,只是用了 FHQ-treap 的合并操作的一些特性。
合并操作是这样的。
node *merge(node *p,node *q){
if(p==null) return q;
if(q==null) return p;
double a=double(p->l->siz)/(p->r->siz),b=double(p->r->siz)/(p->l->siz);//这两行
if(a>b){
p->r=merge(p->r,q);
update(p);
return p;
}
else{
q->l=merge(p,q->l);
update(q);
return q;
}
}其他与 FHQ-treap 一样了。
#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,siz;
};
struct tree{
node *root,*null;
tree(){
null=new node;
null->l=null->r=null;
null->val=null->siz=0;
root=null;
}
void update(node *p){
if(p==null) return;
p->siz=p->l->siz+p->r->siz+1;
}
void split(node *p,int k,node *&x,node *&y){
if(p==null){
x=y=null;
return;
}
else if(p->val<=k) x=p,split(p->r,k,p->r,y);
else y=p,split(p->l,k,x,p->l);
update(p);
}
node *merge(node *p,node *q){
if(p==null) return q;
if(q==null) return p;
double a=double(p->l->siz)/(p->r->siz),b=double(p->r->siz)/(p->l->siz);
if(a>b){
p->r=merge(p->r,q);
update(p);
return p;
}
else{
q->l=merge(p,q->l);
update(q);
return q;
}
}
node *newnode(int x){
node *p=new node;
p->val=x;
p->l=p->r=null;
p->siz=1;
return p;
}
void insert(int x){
node *p=null,*q=null;
split(root,x,p,q);
root=merge(merge(p,newnode(x)),q);
}
void erase(int x){
node *p=null,*q=null,*d=null;
split(root,x,p,q);
split(p,x-1,p,d);
d=merge(d->l,d->r);
root=merge(merge(p,d),q);
}
int ranks(int x){
node *p=null,*q=null;
split(root,x-1,p,q);
int ans=p->siz+1;
root=merge(p,q);
return ans;
}
int getkth(int x){
node *p=root;
while(true)
if(x<=p->l->siz) p=p->l;
else if(x==p->l->siz+1) return p->val;
else x-=p->l->siz+1,p=p->r;
}
int pre_find(int x){
return getkth(ranks(x)-1);
}
int next_find(int x){
return getkth(ranks(x+1));
}
}s;
int main() {
int t;
cin>>t;
while(t--){
int opt,x;
cin>>opt>>x;
if(opt==1) s.insert(x);
if(opt==2) s.erase(x);
if(opt==3) cout<<s.ranks(x)<<"\n";
if(opt==4) cout<<s.getkth(x)<<"\n";
if(opt==5) cout<<s.pre_find(x)<<"\n";
if(opt==6) cout<<s.next_find(x)<<"\n";
}
return 0;
}