跳到正文

个人记录

平衡树第三章 -------- 最灵活的平衡树 之 Splay

MoCaRabbit约 12 分钟
原文

这是 2025年9月19日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2024年1月29日
收录
2025年12月12日
更新
2026年9月14日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

平衡树第二章 -------- 大获全胜的暴力 之 替罪羊树.

前置知识

treap.

正文

注,此文内容均以 P3369 展开讲解。

替罪羊树固然好,但是我们需要更灵活一点与更省空间的树。

以下是节点定义。

C++
struct node{
	node *s[2],*fa;
	int val,cnt,siz;
};

基本函数与基本操作。

C++
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];
}

这里需要注意,这里 insert 了 -INT_MAX 与 INT_MAX。

因为 Splay 的操作很容易导致溢出。

插入操作,注意维护父节点。

这里用传址维护父节点。

C++
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);
}

Splay操作

诶,这里怎么有一个 splay 函数呢。

这个是伸展函数,splay(p,q) 是将 p 旋到 q 的下面。

如果 q 为空,相当于把 p 旋到根。

先来看单旋转操作,目的是将 p 上旋。

C++
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);
}

模拟一下过程。

C++
    gf
   /
  fa
  / \
  p  c
 / \ 
a  b

将 p 上旋。

C++
    gf
   /
  p
 / \
a  fa
  /  \
  b  c

旋转操作与 treap 差不多就对了,所以不多讲。

接下来,灵魂操作来了。

C++
void splay(node *p,node *q){
	while(p->fa!=q){//只要p的父节点不是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;
}

问号处是什么,为什么要两个两个旋。

其实模拟一下就可以了。

如果 qson(p)=qson(fa)。

设最长路径是 a->b->p->B。

C++
      a
     / \
    b   D
   / \
  p  C
 / \
A   B

如果两次都旋 p 旋。

就还是一条链。

C++
   p
  / \
  A  a
    / \
    b  D
   / \
  B   C

但如果先旋 b,再旋 p。

C++
    p
   / \
  A   b
     / \
    B   a
       / \
      C  D

最长路径 p->b->B 竟然变短了。

如果 qson(p)!=qson(fa)。

旋两次 p 是较优的。

可以自己画图。

find操作

寻找最大的 ≤x\le x 的数的节点,并把它旋到根节点。

很简单是吧!

C++
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);
}

其余操作

和 treap 的思路差不多。

列一下吧!

C++
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;
}

删除操作

我们可以将 xx 的前驱旋到根节点,后继旋到根节点的下面。

xx 的后继的左子节点就是没有儿子节点的 xx,可以直接删掉。

最后再 update 一下。

C++
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);
}

代码。

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;
} 

附录

注,附录内容均以 P3391 展开讲解。