跳到正文

算法·理论

平衡树学习小记

liminghao2024约 26 分钟
原文
文章信息与版本

文章信息

发布
2025年8月15日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 26 分钟
获赞
1

版本

  1. v1当前

Treap

模版速度:335ms

Code
C++
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
const int N=101010;
const int INF=1e9;
int n,m,a[N],al,rt;
struct Treap{
	int l,r,size,cnt,pri,wei;
}tree[N];
int get_add(int w){
	tree[++al].wei=w;
	tree[al].size=tree[al].cnt=1;
	tree[al].pri=rand();
	return al;
}
void up(int p){tree[p].size=tree[p].cnt+tree[tree[p].l].size+tree[tree[p].r].size;}
void tl(int &fa){
	int rson=tree[fa].r;
	tree[fa].r=tree[rson].l;
	tree[rson].l=fa;
	fa=rson;
	up(tree[fa].l);//更新原来的父亲
	up(fa);
}
void tr(int &fa){
	int lson=tree[fa].l;
	tree[fa].l=tree[lson].r;
	tree[lson].r=fa;
	fa=lson;
	up(tree[fa].r);
	up(fa);
}
void build(){
	get_add(INF);get_add(-INF);
	tree[1].l=2;rt=1;
	if(tree[1].pri<tree[2].pri)tr(rt);
}
void insert(int &p,int x){
	if(!p)return (void)(p=get_add(x));
	if(tree[p].wei==x)tree[p].cnt++;
	else if(tree[p].wei>x){
		insert(tree[p].l,x);
		if(tree[p].pri<tree[tree[p].l].pri)tr(p);
	}
	else{
		insert(tree[p].r,x);
		if(tree[p].pri<tree[tree[p].r].pri)tl(p);
	}
	up(p);
}
void del(int &p,int x){
	if(!p)return;//理论上不可能
	if(tree[p].wei==x){
		if(tree[p].cnt>1)tree[p].cnt--;
		else if(!tree[p].r&&!tree[p].l)p=0;
		else
			if(!tree[p].r||tree[tree[p].r].pri<tree[tree[p].l].pri)tr(p),del(tree[p].r,x);
			else tl(p),del(tree[p].l,x);
	}
	if(tree[p].wei<x)del(tree[p].r,x);
	if(tree[p].wei>x)del(tree[p].l,x);
	up(p);
}
int countl(int p,int x){
	if(!p) return 0;
	if(tree[p].wei==x)return tree[tree[p].l].size;
	if(tree[p].wei>x)return countl(tree[p].l,x);
	if(tree[p].wei<x)return tree[tree[p].l].size+tree[p].cnt+countl(tree[p].r,x);
}
int countr(int p,int x){
	if(!p) return 0;
	if(tree[p].wei==x)return tree[tree[p].r].size;
	if(tree[p].wei<x)return countr(tree[p].r,x);
	if(tree[p].wei>x)return tree[tree[p].r].size+tree[p].cnt+countr(tree[p].l,x);
}
int fth(int p,int k){//从小到大 
	if(tree[tree[p].l].size>=k)return fth(tree[p].l,k);
	if(tree[tree[p].l].size+tree[p].cnt<k)return fth(tree[p].r,k-tree[tree[p].l].size-tree[p].cnt);
	return tree[p].wei;
}
int pre(int x){return fth(rt,countl(rt,x));}
int nxt(int x){return fth(rt,tree[rt].size-countr(rt,x)+1);}
int main(){
	cin>>n;
	build();
	for(int i=1;i<=n;i++){
		int op,x;
		scanf("%d%d",&op,&x);
		if(op==1)insert(rt,x);
		if(op==2)del(rt,x);
		if(op==3)cout<<countl(rt,x)<<endl;
		if(op==4)cout<<fth(rt,x+1)<<endl;
		if(op==5)cout<<pre(x)<<endl;
		if(op==6)cout<<nxt(x)<<endl;
	}
	return 0;
}

Splay

模版速度:530ms

Code
C++
#include<bits/stdc++.h>
#define ls(x) tree[x].son[0]
#define rs(x) tree[x].son[1]
#define fa(x) tree[x].fa
using namespace std;
const int N=111010,INF=1e9;
int n,al,rt;
struct Stree{
	int wei,cnt,size;
	int son[2],fa;
}tree[N];
int get_add(int w,int f){
	tree[++al].wei=w;
	tree[al].size=tree[al].cnt=1; 
	tree[al].fa=f;
	return al;
}
bool get(int x){
	if(tree[tree[x].fa].son[0]==x){
		return 0;
	}return 1;
}
void up(int p){
	tree[p].size=tree[tree[p].son[0]].size+tree[tree[p].son[1]].size+tree[p].cnt;
	return ;
}
void clearing(int p){
	tree[p].cnt=tree[p].size=tree[p].fa=0;
	tree[p].son[0]=tree[p].son[1]=tree[p].wei=0;
	return ;
}
void turn(int x){
	int y=tree[x].fa;
	int z=tree[y].fa;
	int op=get(x);
	tree[y].son[op]=tree[x].son[op^1];//把儿子没用的东西给父亲 
	tree[tree[x].son[op^1]].fa=y;//没用的东西认爸
	tree[x].son[op^1]=y;tree[y].fa=x;
	tree[x].fa=z;tree[z].son[y==tree[z].son[1]]=x;
	up(y);up(x);
	return ;
}
void splay(int x){
	for(int i=tree[x].fa;i=tree[x].fa,i;turn(x)){
		if(tree[i].fa){
			turn(get(i)==get(x)?i:x);
		}
	}
	rt=x;
	return ;
}
void insert(int x){
	int s=rt,f=0;
	while(s&&tree[s].wei!=x){
		f=s;
		s=tree[s].son[tree[s].wei<x];
	}
	if(s){
		tree[s].cnt++;
	}else{
		get_add(x,f);s=al;
		tree[f].son[tree[f].wei<x]=s;
	}
	splay(s);
}
void find(int x){
	int s=rt;
	while(tree[s].son[tree[s].wei<x]&&tree[s].wei!=x){
		s=tree[s].son[tree[s].wei<x];
	}splay(s);
}

int fth(int x){
	int s=rt;
	while(1){
		if(ls(s)&&x<=tree[ls(s)].size){
			s=ls(s);
		}else if(tree[ls(s)].size+tree[s].cnt<x){
			x-=tree[ls(s)].size+tree[s].cnt;
			s=rs(s);
		}else{
			splay(s);
			return tree[s].wei;
		}
	}
}
void del(int x){
	find(x);
	if(tree[rt].cnt>1){
		tree[rt].cnt--;
		up(rt);
		return ;
	}
	int s=ls(rt),tmp=rt;
	while(rs(s))s=rs(s);
	splay(s);
	if(rs(tmp)){
		fa(rs(tmp))=s;
	}
	rs(s)=rs(tmp);
	fa(s)=0;
	clearing(tmp);
	up(rt);return ;
}
int rnk(int x){
	insert(x);
	int ans=tree[ls(rt)].size;
	del(x);
	return ans;
}
int pre(int x){
	insert(x);
	int s=ls(rt);
	while(rs(s))s=rs(s);
	splay(s);
	del(x);
	return tree[s].wei;
}
int nxt(int x){
	insert(x);
	int s=rs(rt);
	while(ls(s)){
		s=ls(s);
	}
	splay(s);
	del(x);
	return tree[s].wei;
}
int main(){
	//freopen("P3369_1.in","r",stdin);
	scanf("%d",&n);
	insert(-1e9);
	insert(1e9);
	for(int i=1;i<=n;i++){
		int op,o;
		scanf("%d%d",&op,&o);
		if(op==1){
			insert(o);
		}
		if(op==2){
			del(o);
		}
		if(op==3){
			printf("%d\n",rnk(o));
		}
		if(op==4){
			printf("%d\n",fth(o+1));
		}
		if(op==5){
			printf("%d\n",pre(o));
		}
		if(op==6){
			printf("%d\n",nxt(o));
		}
	}return 0;
}

Splay (区间)

区间操作速度:572ms

Code
C++
#include<bits/stdc++.h>
#define ls(x) tree[x].son[0]
#define rs(x) tree[x].son[1]
#define fa(x) tree[x].fa
using namespace std;
const int N=1e5+9;
int n,m,rev[N],rt,tot;
struct ill{
	int wei,fa,son[2],size;
}tree[N];
int get(int x){return rs(fa(x))==x;}
void up(int x){
	tree[x].size=tree[ls(x)].size+tree[rs(x)].size+1;
}
void down(int x){
	if(rev[x]){
		swap(ls(x),rs(x));
		rev[ls(x)]^=1;
		rev[rs(x)]^=1;
		rev[x]=0;
	}
}
void turn(int x){
	int y=fa(x),z=fa(y),k=get(x);
	tree[z].son[get(y)]=x;
	tree[x].fa=z;
	tree[y].son[k]=tree[x].son[!k];
	tree[tree[x].son[!k]].fa=y;
	tree[y].fa=x;
	tree[x].son[!k]=y;
	up(y);up(x);
}
void change(int x){
	if(x!=rt)change(fa(x));
	down(x);
}
void splay(int x,int z){
	change(x);
	for(int f;f=fa(x),f!=z;turn(x)){
		if(fa(f)!=z) turn(get(x)==get(f)?f:x);
	}if(z==0)rt=x;
}
int build(int l, int r, int f) {
    if (l > r) return 0;
    int mid = (l + r) >> 1;
    int now=++tot;
    tree[now].fa = f;
    tree[now].wei = mid;
    tree[now].size = 1;
    rev[now] = 0;
    ls(now) = build(l, mid - 1, now);
    rs(now) = build(mid + 1, r, now);
    up(now);
    return now;
}
void print(int x){
	down(x);
	if(ls(x))print(ls(x));
	if(tree[x].wei>=1&&tree[x].wei<=n)cout<<tree[x].wei<<' ';
	if(rs(x))print(rs(x));
}
int find(int k) {
    int u = rt;
    while (1) {
        down(u);
        if (tree[ls(u)].size >= k) {
            u = ls(u);
        } else {
            k -= tree[ls(u)].size + 1;
            if (k == 0) return u;
            u = rs(u);
        }
    }
}
int main(){
	cin>>n>>m;
	rt=build(0,n+1,0);
	while(m--){
		int l,r;
		cin>>l>>r;
		l++,r++;
		l=find(l-1);
		r=find(r+1);
		splay(l,0);
		splay(r,l);
		rev[ls(rs(rt))]^=1;
	}print(rt);
	return 0;
}

笛卡尔树(初级版)

Code
C++
#include<bits/stdc++.h> 
using namespace std;
int n,m;
long long ans1,ans2;
int a[10101000],al;
struct dicar_tree{
	int k,w,fa,son[2];
	void init(int _k,int _w){
		k=_k;
		w=_w;
		fa=son[0]=son[1]=0;
	}
}tree[10101000];
int build(){
	for(int i=1;i<=n;i++){
		int k=i-1;
		while(k&&tree[k].w>tree[i].w)k=tree[k].fa;
		tree[i].son[0]=tree[k].son[1];
		tree[tree[i].son[0]].fa=i;
		tree[i].fa=k;
		tree[k].son[1]=i;
	}
	return tree[0].son[1];
}
void dfs(int u){
	if(!u)return ;
	dfs(tree[u].son[1]);
	dfs(tree[u].son[0]);
	ans2^=(long long)u*(long long)(1+tree[u].son[1]);
	ans1^=(long long)u*(long long)(1+tree[u].son[0]);
	return ;
}
signed main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++){
		tree[i].init(i,a[i]);
	}
	int rt=build();
	dfs(rt);
	printf("%lld %lld\n",ans1,ans2);
	return 0;
}

无旋Treap

模版速度:293ms(MVP)

Code
C++
#include<bits/stdc++.h>
#include<tuple> 
using namespace std;
int n,m;
struct treap{
	int fa,val,siz,cnt,pri;
	treap *son[2];
	treap(int x):val(x),cnt(1),siz(1){
		son[0]=son[1]=nullptr;
		pri=rand();
	}
	treap(treap *id){
		val=id->val;
		pri=id->pri;
		cnt=id->cnt;
		siz=id->siz;
	}
	void up(){
		siz=cnt;
		if(son[0]!=nullptr)siz+=son[0]->siz;
		if(son[1]!=nullptr)siz+=son[1]->siz;
	} 
};
struct unturn_treap{
	treap *root;
	pair<treap *,treap *> split(treap *rt,int key){
		if(rt==nullptr)return {nullptr,nullptr};
		if(rt->val<=key){
			auto tmp=split(rt->son[1],key);
			rt->son[1]=tmp.first;
			rt->up();
			return {rt,tmp.second};
		}else{
			auto tmp=split(rt->son[0],key);
			rt->son[0]=tmp.second;
			rt->up();
			return {tmp.first,rt}; 
		}
	}
	tuple<treap *,treap *,treap *> split2(treap *rt,int key){
		if(rt==nullptr)return {nullptr,nullptr,nullptr};
		int x=(rt->son[0]==nullptr?0:rt->son[0]->siz);
		if(key<=x){
			treap *l,*r,*mid;
			tie(l,mid,r)=split2(rt->son[0],key);
			rt->son[0]=r;
			rt->up();
			return {l,mid,rt}; 
		}else if(key<=x+rt->cnt){
			treap *lson=rt->son[0],*rson=rt->son[1];
			rt->son[0]=nullptr;
			rt->son[1]=nullptr;
			rt->up();
			return {lson,rt,rson};
		}else{
			treap *l,*r,*mid;
			tie(l,mid,r)=split2(rt->son[1],key-x-rt->cnt);
			rt->son[1]=l;
			rt->up();
			return {rt,mid,r};
		}
	}
	treap *merge(treap *u,treap *v){
		if(u==nullptr&&v==nullptr)return nullptr;
		if(u==nullptr&&v!=nullptr)return v;
		if(u!=nullptr&&v==nullptr)return u;
		if(u->pri<v->pri){
			u->son[1]=merge(u->son[1],v);
			u->up();
			return u;
		}else{
			v->son[0]=merge(u,v->son[0]);
			v->up();
			return v;
		}
	}
	void insert(int x){
		auto tmp=split(root,x);
		auto l=split(tmp.first,x-1);
		treap *id;
		if(l.second==nullptr){
			id=new treap(x);
		}else{
			l.second->cnt++;
			l.second->up();
		}
		treap *mid=merge(l.first,(l.second==nullptr?id:l.second));
		root=merge(mid,tmp.second);
	}
	void del(int x){
		auto temp=split(root,x);
		auto l=split(temp.first,x-1);
		if(l.second->cnt>1){
			l.second->cnt--;
			l.second->up();
			l.first=merge(l.first,l.second);
		}else{
			if(temp.first==l.second){
				temp.first=nullptr;
			}
			delete l.second;
			l.second=nullptr;
		}
		root=merge(l.first,temp.second);
	}
	int findth(treap* rt,int x){
		auto temp=split(rt,x-1);
		int ans=(temp.first==nullptr?0:temp.first->siz)+1;
		root=merge(temp.first,temp.second);
		return ans;
	}
	int findval(treap *rt,int key){
		treap *l,*mid,*r;
		tie(l,mid,r)=split2(rt,key);
		int ans=mid->val;
		root=merge(merge(l,mid),r);
		return ans;
	}
	int pre(int x){
		auto temp=split(root,x-1);
		int ans=findval(temp.first,temp.first->siz);
		root=merge(temp.first,temp.second);
		return ans;
	}
	int nxt(int x){
		auto temp=split(root,x);
		int ans=findval(temp.second,1);
		root=merge(temp.first,temp.second);
		return ans;
	}
}tr;	
signed main(){
	srand(time(0));
	cin>>m;
	while(m--){
		int op,x;
		scanf("%d%d",&op,&x);
		if(op==1){
			tr.insert(x);
		}if(op==2){
			tr.del(x);
		}if(op==3){
			printf("%d\n",tr.findth(tr.root,x));
		}if(op==4){
			printf("%d\n",tr.findval(tr.root,x));
		}if(op==5){
			printf("%d\n",tr.pre(x));
		}if(op==6){
			printf("%d\n",tr.nxt(x));
		}
	}
	return 0;
}

无旋Treap(区间)

区间操作速度:507ms

Code
C++
#include<bits/stdc++.h>
#define size(_) (_==nullptr?0:_->siz)
using namespace std;
int n,m;
struct treap{
	int fa,val,siz,cnt,pri,rev=0;
	treap *son[2];
	treap(int x):val(x),cnt(1),siz(1){
		son[0]=son[1]=nullptr;
		pri=rand();
	}
	void up(){
		siz=cnt;
		if(son[0]!=nullptr)siz+=son[0]->siz;
		if(son[1]!=nullptr)siz+=son[1]->siz;
	} 
	void down(){
		if(!rev)return;
		swap(son[0],son[1]);
		if(son[0]!=nullptr)son[0]->rev^=1;
		if(son[1]!=nullptr)son[1]->rev^=1;
		rev=0;
	}
};
struct unturn_treap{
	treap *root=nullptr;
	pair<treap *,treap *> split(treap *rt,int key){
		if(rt==nullptr)return {nullptr,nullptr};
		rt->down();
		if(size(rt->son[0])>=key){
			auto tmp=split(rt->son[0],key);
			rt->son[0]=tmp.second;
			rt->up();
			return {tmp.first,rt};
		}else{
			auto tmp=split(rt->son[1],key-size(rt->son[0])-1);
			rt->son[1]=tmp.first;
			rt->up();
			return {rt,tmp.second};
		}
	}
	treap *merge(treap *u,treap *v){
		if(u==nullptr&&v==nullptr)return nullptr;
		if(u==nullptr&&v!=nullptr)return v;
		if(u!=nullptr&&v==nullptr)return u;
		u->down();
		v->down();
		if(u->pri<v->pri){
			u->son[1]=merge(u->son[1],v);
			u->up();
			return u;
		}else{
			v->son[0]=merge(u,v->son[0]);
			v->up();
			return v;
		}
	}
	void insert(int x){
		auto tmp=split(root,x);
		auto l=split(tmp.first,x-1);
		treap *id;
		if(l.second==nullptr)id=new treap(x);
		treap *mid=merge(l.first,(l.second==nullptr?id:l.second));
		root=merge(mid,tmp.second);
	}
	void revs(int l,int r){
		auto less=split(root,l-1);
		auto more=split(less.second,r-l+1);
		more.first->rev=1;
		root=merge(less.first,merge(more.first,more.second));
	}
	void print(treap* rt){
		if(rt==nullptr)return ;
		rt->down();
		print(rt->son[0]);
		printf("%d ",(rt->val));
		print(rt->son[1]);
	}
}tr;	
signed main(){
	srand(time(0));
	cin>>n>>m;
	for(int i=1;i<=n;i++)tr.insert(i);
	while(m--){
		int l,r;
		scanf("%d%d",&l,&r);
		tr.revs(l,r);
	}
	tr.print(tr.root);
	return 0;
}
原文