跳到正文

个人记录

平衡树

nb_jzy约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 13 分钟

版本

  1. v1当前

Splay

思想

主要基于左旋和右旋操作。

考虑对于平衡树上的节点 uu,将其旋转到他的父亲上面(交换父子关系),并且让整棵树依然保持平衡树的性质(中序遍历后构成有序的数列)。

这就不得不提到服务器了。

其以 splay 操作为核心,将平衡树上的某个点通过左旋和右旋旋转到根节点。可以证明的是 splay 操作的均摊复杂度是 O(log⁡n)\mathcal O(\log n),所以 splay 的复杂度是 O(m×log⁡n)\mathcal O(m\times \log n) 的。但是常数比较大。

注意点

但是如何左旋和右旋让 splay 保证复杂度呢?

一个简单的思路是一直旋转 uu 节点就行了。但是这样均摊出来是错误的,具体证明参考 OI-Wiki。

我们需要分两种情况进行讨论:

  • uu,pp 构成一条链

这个时候先旋转 pp,再旋转 uu。

  • uu,pp 不是一条链

这个时候 uu 自己旋转两次就行了。

然后就可以保证复杂度了!

运用

一些单纯考察平衡树的题目近几年都很少见,主要还是发现可以用平衡树优化或者是维护某些东西时使用。当然,也是 LCT 的主要结构。

代码

维护多重集

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e6+5;
int n,m;
struct splay_tree{
	int t[maxn],son[maxn][2],siz[maxn],f[maxn],num[maxn],cnt,root;
	void clearing(){
		memset(t,0,sizeof t);
		memset(son,0,sizeof son);
		memset(siz,0,sizeof siz);
		memset(f,0,sizeof f);
		memset(num,0,sizeof num);
		cnt=0;root=0;
	}
	int sonid(int x){
		return (son[f[x]][1]==x);
	}
	void benew(int x){
		siz[x]=siz[son[x][0]]+siz[son[x][1]]+num[x];
	}
	void rot(int x){
		int y=f[x],z=f[y];
		int k=sonid(x);
		f[x]=z;
		if(z){
			son[z][sonid(y)]=x;
		}
		else{
			root=z;
		}
		son[y][k]=son[x][k^1];f[son[y][k]]=y;
		son[x][k^1]=y;f[y]=x;
		benew(y),benew(x);
	}
	void splay(int x,int p){
		for(int i=f[x];i=f[x],i!=p;rot(x)){
			if(f[i]!=p){
				rot(sonid(x)==sonid(i)?i:x);
			}
		}
		if(!f[x]){
			root=x;
		}
	}
	int find(int x){
		int now=root,noww=0;
		while(now!=0&&t[now]!=x){
			if(now!=0){
				noww=now;
			}
			now=son[now][x>t[now]];
		}
		if(noww){
			splay(noww,0);
		}
		if(!now){
			now=-1;
		}
		return now;
	}
	void Insert(int x){
		int loc=find(x);
		if(loc==-1){
			if(!root){
				cnt++;root=cnt;
				siz[cnt]=num[cnt]=1;t[cnt]=x;
			}
			else{
				int now=root;
				while(now>0){
					int id=(x>=t[now]);
					if(!son[now][id]){
						son[now][id]=++cnt;
						num[cnt]=siz[cnt]=1;t[cnt]=x;f[cnt]=now;
						break;
					}
					now=son[now][id];
				}
				splay(cnt,0);
			}
		}
		else{
			num[loc]++,siz[loc]++;splay(loc,0);
		}
	}
	int getmaxid(int x){
		int now=x;
		while(son[now][1]){
			now=son[now][1];
		}
		return now;
	}
	int getminid(int x){
		int now=x;
		while(son[now][0]){
			now=son[now][0];
		}
		return now;
	}
	void merge(int x,int y){
		if(x){
			int now=getmaxid(x);
			splay(now,0);
			if(y>0){
				son[now][1]=y,f[y]=now;
			}
			benew(now);
		}
		else{
			root=y;
		}
	}
	void del(int x){
		int loc=find(x);
		splay(loc,0);
		if(num[loc]==1){
			num[loc]=siz[loc]=0;
			int ls=son[loc][0],rs=son[loc][1];
			son[loc][0]=son[loc][1]=0;t[loc]=0;
			f[ls]=0,f[rs]=0;
			merge(ls,rs);
		}
		else{
			num[loc]--,siz[loc]--;
		}
	}
	int query3(int x){
		Insert(x);
		int now=find(x);
		splay(now,0);
		int res=siz[son[now][0]]+1;
		del(x);
		return res;
	}
	int query4(int x){
		int now=root;
		while(now>0){
			if(x>siz[son[now][0]]&&x<=siz[son[now][0]]+num[now]){
				break;
			}
			if(x>siz[son[now][0]]+num[now]){
				x-=siz[son[now][0]]+num[now];now=son[now][1];
			}
			else{
				now=son[now][0];
			}
		}
		splay(now,0);
		return t[now];
	}
	int query5(int x){
		Insert(x);
		int loc=find(x);
		splay(loc,0);
		int res=t[getmaxid(son[loc][0])];
		del(x);
		return res;
	}
	int query6(int x){
		Insert(x);
		int loc=find(x);
		splay(loc,0);
		int res=t[getminid(son[loc][1])];
		del(x);
		return res;
	}
}t;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	t.clearing();
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int x;cin>>x;
		t.Insert(x);
	}
	int lastans=0,ans=0;
	for(int i=1;i<=m;i++){
		int op,x;
		cin>>op>>x;
		x^=lastans;
		if(op==1){
			t.Insert(x);
		}
		else if(op==2){
			t.del(x);
		}
		else if(op==3){
			lastans=t.query3(x);
			ans^=lastans;
		}
		else if(op==4){
			lastans=t.query4(x);
			ans^=lastans;
		}
		else if(op==5){
			lastans=t.query5(x);
			ans^=lastans;
		}
		else{
			lastans=t.query6(x);
			ans^=lastans;
		}
	}
	cout<<ans;
	return 0;
}

维护序列信息

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,m,a[maxn];
struct splay_tree{
	int t[maxn],t1[maxn],t2[maxn],son[maxn][2],siz[maxn],f[maxn],la[maxn],cnt,root;
	int sonid(int x){
		return (son[f[x]][1]==x);
	}
	void pushup(int x){
		if(!x){
			return;
		}
		int ls=son[x][0],rs=son[x][1];
		siz[x]=siz[ls]+siz[rs]+1;
		t1[x]=t1[ls]+t1[rs]+t[x];
		t2[x]=max(t2[ls],max(t2[rs],t[x]));
	}
	void pushdown(int x){
		int ls=son[x][0],rs=son[x][1];
		if(la[x]){
			if(ls){
				la[ls]^=1;
				swap(son[ls][0],son[ls][1]);
			}
			if(rs){
				la[rs]^=1;
				swap(son[rs][0],son[rs][1]);
			}
			la[x]=0;
		}
	}
	void build(int l,int r,int fa,int id){
		if(l>r){
			return;
		}
		int mid=(l+r)>>1;
		int p=++cnt;
		t[p]=t1[p]=t2[p]=a[mid];siz[p]=1;
		if(fa){
			son[fa][id]=p;f[p]=fa;
		}
		else{
			root=p;
		}
		build(l,mid-1,p,0),build(mid+1,r,p,1);
		pushup(p);
	}
	void rot(int x){
		int y=f[x],z=f[y],k=sonid(x);
		f[x]=z;
		if(z){
			son[z][sonid(y)]=x;
		}
		else{
			root=x;
		}
		son[y][k]=son[x][k^1];f[son[y][k]]=y;
		son[x][k^1]=y;f[y]=x;
		pushup(y),pushup(x);
	}
	void splay(int x,int p){
		for(int i=f[x];i=f[x],i!=p;rot(x)){
			if(f[i]!=p){
				rot(sonid(i)==sonid(x)?i:x);
			}
		}
		if(!f[x]){
			root=x;
		}
	}
	int find(int x){
		int now=root;
		while(now>0){
			pushdown(now);
			if(siz[son[now][0]]+1==x){
				break;
			}
			if(siz[son[now][0]]+1>x){
				now=son[now][0];
			}
			else{
				x-=siz[son[now][0]]+1;
				now=son[now][1];
			}
		}
		return now;
	}
	void change(int x,int d){
		int loc=find(x+1);
		splay(loc,0);
		t[loc]+=d;
		pushup(loc);
	}
	int query1(int l,int r){
		int ll=find(l);splay(ll,0);
		int rr=find(r+2);splay(rr,ll);
		return t1[son[rr][0]];
	}
	int query2(int l,int r){
		int ll=find(l);splay(ll,0);
		int rr=find(r+2);splay(rr,ll);
		return t2[son[rr][0]];
	}
	void rev(int l,int r){
		int ll=find(l);splay(ll,0);
		int rr=find(r+2);splay(rr,ll);
		int now=son[rr][0];
		la[now]^=1;swap(son[now][0],son[now][1]);
	}
}t;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	t.build(0,n+1,0,0);
	for(int i=1;i<=m;i++){
		int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			cout<<t.query1(x,x)<<'\n';
		}
		else if(op==2){
			int x,d;
			cin>>x>>d;
			t.change(x,d);
		}
		else if(op==3){
			int l,r;
			cin>>l>>r;
			cout<<t.query1(l,r)<<'\n';
		}
		else if(op==4){
			int l,r;
			cin>>l>>r;
			cout<<t.query2(l,r)<<'\n';
		}
		else{
			int l,r;
			cin>>l>>r;
			t.rev(l,r);
		}
	}
	return 0;
}
原文