模板代码(平衡树专辑):
平衡树Scapegoat
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,cnt,s,siz,rsiz;
};
vector<node*>v;
struct scape_goat{
node *root,*null;
scape_goat(){
null=new node;
null->cnt=null->val=0;
null->l=null->r=0;
root=null;
}
void update(node *p){
p->s=p->l->s+p->r->s+1;
p->siz=p->l->siz+p->r->siz+p->cnt;
p->rsiz=p->l->rsiz+p->r->s+bool(p->cnt);
}
bool check(node *p){
return (0.75*p->s<=double(max(p->l->s,p->r->s)) || double(p->siz)<=0.75*p->s);
}
void dfs(node *p){
if(p==null) return;
dfs(p->l);
if(p->cnt) v.push_back(p);
dfs(p->r);
if(!p->cnt) delete p;
}
node *build(int l,int r){
if(l>r) return null;
int mid=l+r>>1;
node *p=v[mid];
p->l=build(l,mid-1);
p->r=build(mid+1,r);
update(p);
return p;
}
void recon(node *&p){
v.clear();
dfs(p);
p=build(0,v.size()-1);
}
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=p->rsiz=1;
return;
}
if(x==p->val) p->cnt++;
else if(x<p->val) insert(x,p->l);
else insert(x,p->r);
update(p);
if(check(p)) recon(p);
}
void erase(int x,node *&p){
if(p==null) return;
if(p->val==x) p->cnt--;
else if(x<p->val) erase(x,p->l);
else erase(x,p->r);
update(p);
if(check(p)) recon(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);
}
}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.get(s.ranks(x)-1,s.root)<<"\n";
if(opt==6) cout<<s.get(s.ranks(x+1),s.root)<<"\n";
}
return 0;
}
平衡树FHQ-treap
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,data,siz;
};
struct fhq{
node *root,*null;
fhq(){
null=new node;
null->l=null->r=null;
null->val=null->data=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;
if(p->data<q->data){
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->data=rand();
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;
}
平衡树Treap
.
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,data,cnt,siz;
};
struct tree{
node *root;
node *null;
node* new_root(int val){
node *p=new node;
p->val=val,p->data=rand();
p->cnt=p->siz=1,p->l=p->r=null;
return p;
}
void update(node *x){
x->siz=x->l->siz+x->r->siz+x->cnt;
}
void build(){
null=new node;
null->l=null->r=null;
null->siz=null->val=null->cnt=null->data=0;
root=null;
}
tree(){
build();
}
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);
}
void zig(node *&p){
node *q=p->l;
p->l=q->r,q->r=p,p=q;
update(p->r),update(p);
}
void zag(node *&p){
node *q=p->r;
p->r=q->l,q->l=p,p=q;
update(p->l),update(p);
}
void insert(int val,node *&p){
if(p==null){
p=new_root(val);
return;
}
if(val==p->val){
p->cnt++;
update(p);
return;
}
if(val<p->val){
insert(val,p->l);
if(p->data<p->l->data) zig(p);
}
else{
insert(val,p->r);
if(p->data<p->r->data) zag(p);
}
update(p);
}
int pre_find(int val){
return get(ranks(val)-1,root);
}
int next_find(int val){
return get(ranks(val+1),root);
}
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 || p->l->data>p->r->data)
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);
update(p);
}
};
tree p=tree();
int main() {
int t;
cin>>t;
while(t--){
int opt,x;
cin>>opt>>x;
if(opt==1) p.insert(x,p.root);
if(opt==2) p.erase(x,p.root);
if(opt==3) cout<<p.ranks(x)<<"\n";
if(opt==4) cout<<p.get(x,p.root)<<"\n";
if(opt==5) cout<<p.pre_find(x)<<"\n";
if(opt==6) cout<<p.next_find(x)<<"\n";
}
return 0;
}
树状数组平衡树
C++#include<bits/stdc++.h>
using namespace std;
int t[int(2e7)+10];
int lowbit(int x){
return x&-x;
}
int sum(int x){
int p=0;
while(x)
p+=t[x],x-=lowbit(x);
return p;
}
void update(int x,int p){
while(x<=2e7+5)
t[x]+=p,x+=lowbit(x);
}
void insert(int x){
update(x,1);
}
void erase(int x){
update(x,-1);
}
int ranks(int x){
return sum(x-1)+1;
}
int getkth(int x){
int k=0,cnt=0;
for(int i=30;i>=0;i--){
k+=(1<<i);
if(k>2e7+5 || cnt+t[k]>=x) k-=(1<<i);
else cnt+=t[k];
}
return k+1;
}
int pre_find(int x){
return getkth(ranks(x)-1);
}
int next_find(int x){
return getkth(ranks(x+1));
}
int main() {
int t;
cin>>t;
while(t--){
int opt,x;
cin>>opt>>x;
if(opt==1) insert(x+int(1e7)+1);
if(opt==2) erase(x+int(1e7)+1);
if(opt==3) cout<<ranks(x+int(1e7)+1)<<"\n";
if(opt==4) cout<<getkth(x)-int(1e7)-1<<"\n";
if(opt==5) cout<<pre_find(x+int(1e7)+1)-int(1e7)-1<<"\n";
if(opt==6) cout<<next_find(x+int(1e7)+1)-int(1e7)-1<<"\n";
}
return 0;
}
平衡树Splay
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *s[2],*fa;
int val,cnt,siz;
};
struct tree{
node *root,*null;
tree(){
null=new node;
null->val=null->cnt=null->siz=0;
null->s[0]=null->s[1]=null->fa=null;
root=null;
insert(-INT_MAX),insert(INT_MAX);
}
void update(node *p){
p->siz=p->s[0]->siz+p->s[1]->siz+p->cnt;
}
bool qson(node *p){
return p==p->fa->s[1];
}
void rorate(node *p){
node *fa=p->fa;
node *gf=fa->fa;
int k=qson(p);
fa->s[k]=p->s[k^1],p->s[k^1]->fa=fa;
gf->s[qson(fa)]=p,p->fa=gf;
p->s[k^1]=fa,fa->fa=p;
update(fa),update(p);
}
void splay(node *p,node *q){
while(p->fa!=q){
node *fa=p->fa;
node *gf=fa->fa;
if(gf!=q){
if(qson(p)==qson(fa)) rorate(fa);
else rorate(p);
}
rorate(p);
}
if(q==null) root=p;
}
void find(int x){
node *p=root;
while(p->s[x>p->val]!=null && x!=p->val)
p=p->s[x>p->val];
splay(p,null);
}
void insert(int x){
node *p=root,*fa=null;
while(p!=null && p->val!=x)
fa=p,p=p->s[x>p->val];
if(p!=null) p->cnt++;
else{
p=new node;
if(fa!=null) fa->s[x>fa->val]=p;
p->s[0]=p->s[1]=null;
p->fa=fa;
p->cnt=p->siz=1;
p->val=x;
if(root==null) root=p;
}
splay(p,null);
}
int find_kth(int k){
node *p=root;
k++;
while(true){
if(p->s[0]!=null && k<=p->s[0]->siz)
p=p->s[0];
else if(k>p->s[0]->siz+p->cnt) k-=p->s[0]->siz+p->cnt,p=p->s[1];
else{
splay(p,null);
return p->val;
}
}
}
int ranks(int x){
find(x);
if(root->val>=x) return root->s[0]->siz;
else return root->s[0]->siz+root->cnt;
}
node *pre_get(int x){
find(x);
if(root->val<x) return root;
node *p=root->s[0];
while(p->s[1]!=null) p=p->s[1];
splay(p,null);
return p;
}
node *next_get(int x){
find(x);
if(root->val>x) return root;
node *p=root->s[1];
while(p->s[0]!=null) p=p->s[0];
splay(p,null);
return p;
}
int pre_find(int x){
return pre_get(x)->val;
}
int next_find(int x){
return next_get(x)->val;
}
void erase(int x){
node *p=pre_get(x),*q=next_get(x);
splay(p,null),splay(q,p);
node *d=q->s[0];
if(d->cnt>1){
d->cnt--;
splay(d,null);
}
else q->s[0]=null;
update(q),update(root);
}
}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.find_kth(x)<<"\n";
if(opt==5) cout<<s.pre_find(x)<<"\n";
if(opt==6) cout<<s.next_find(x)<<"\n";
}
return 0;
}
FHQ-treap区间旋转
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *l,*r;
int val,data;
int siz,lazy;
};
struct fhq{
node *root,*null;
fhq(){
null=new node;
null->l=null->r=null;
null->val=null->data=null->siz=0;
null->lazy=0;
root=null;
}
node *new_node(int x){
node *p=new node;
p->l=p->r=null;
p->val=x;
p->data=rand();
p->siz=1;
p->lazy=0;
return p;
}
void update(node *p){
if(p==null) return;
p->siz=p->l->siz+p->r->siz+1;
}
void pushdown(node *p){
if(p->lazy==0) return;
swap(p->l,p->r);
p->lazy=0;
p->l->lazy^=1,p->r->lazy^=1;
}
void split(node *p,int k,node *&x,node *&y){
if(p==null){
x=y=null;
return;
}
pushdown(p);
if(p->l->siz<k) x=p,split(p->r,k-p->l->siz-1,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;
if(p->data>q->data){
pushdown(p);
p->r=merge(p->r,q);
update(p);
return p;
}
else{
pushdown(q);
q->l=merge(p,q->l);
update(q);
return q;
}
}
void insert(int x){
node *p=new_node(x);
root=merge(root,p);
}
void reverse(int l,int r){
node *p=null,*q=null,*d=null;
split(root,l-1,p,q);
split(q,r-l+1,d,q);
d->lazy^=1;
root=merge(merge(p,d),q);
}
void dfs(node *p){
if(p==null) return;
pushdown(p);
dfs(p->l);
cout<<p->val<<" ";
dfs(p->r);
}
}s;
int n,m;
int main() {
srand(time(0));
cin>>n>>m;
for(int i=1;i<=n;i++){
int x=i;
// cin>>x;
s.insert(x);
}
while(m--){
int l,r;
cin>>l>>r;
s.reverse(l,r);
}
s.dfs(s.root);
return 0;
}
Splay区间旋转
C++#include<bits/stdc++.h>
using namespace std;
struct node{
node *s[2],*fa;
int data;
int val,siz,lazy;
};
int n,m;
struct tree{
node *root,*null;
void build(){
null=new node;
null->val=null->siz=null->lazy=null->data=0;
null->s[0]=null->s[1]=null->fa=null;
root=null;
}
void update(node *p){
p->siz=p->s[0]->siz+p->s[1]->siz+1;
}
bool qson(node *p){
return p==p->fa->s[1];
}
void rorate(node *p){
node *fa=p->fa;
node *gf=fa->fa;
push_down(fa),push_down(p);
int k=qson(p);
fa->s[k]=p->s[k^1],p->s[k^1]->fa=fa;
gf->s[qson(fa)]=p,p->fa=gf;
p->s[k^1]=fa,fa->fa=p;
update(fa),update(p);
}
void splay(node *p,node *q){
while(p->fa!=q){
node *fa=p->fa;
node *gf=fa->fa;
if(gf!=q){
if(qson(p)==qson(fa)) rorate(fa);
else rorate(p);
}
rorate(p);
}
if(q==null) root=p;
}
void find(int x){
node *p=root;
while(p->s[x>p->val]!=null && x!=p->val)
p=p->s[x>p->val];
splay(p,null);
}
void push_down(node *p){
if(!p->lazy) return;
swap(p->s[0],p->s[1]);
p->s[0]->lazy^=1;
p->s[1]->lazy^=1;
p->lazy=0;
}
void insert(int x,int k){
node *p=root,*fa=null;
while(p!=null && p->val!=k)
fa=p,p=p->s[k>p->val];
if(p!=null) splay(p,null);
else{
p=new node;
if(fa!=null) fa->s[k>fa->val]=p;
p->s[0]=p->s[1]=null;
p->fa=fa;
p->siz=1;
p->val=k,p->data=x;
p->lazy=0;
if(root==null) root=p;
}
splay(p,null);
}
node *find_kth(int k){
node *p=root;
while(true){
push_down(p);
if(p->s[0]!=null && k<=p->s[0]->siz)
p=p->s[0];
else if(k>p->s[0]->siz+1) k-=p->s[0]->siz+1,p=p->s[1];
else{
splay(p,null);
return p;
}
}
}
void reverse(int l,int r){
node *p=find_kth(l),*q=find_kth(r+2);
splay(p,null),splay(q,p);
root->s[1]->s[0]->lazy^=1;
}
void dfs(node *p){
if(p==null) return;
push_down(p);
dfs(p->s[0]);
if(p->val>0 && p->val<=n) cout<<p->data<<" ";
dfs(p->s[1]);
}
}s;
int main() {
cin>>n>>m;
s.build();
s.insert(0,0);
for(int i=1;i<=n;i++){
int x=i;
// cin>>x;
s.insert(x,i);
}
s.insert(0,n+1);
s.insert(0,n+2);
s.insert(0,n+3);
while(m--){
int l,r;
cin>>l>>r;
s.reverse(l,r);
}
s.dfs(s.root);
return 0;
}