跳到正文

算法·理论

数据结构总结——FHQ Treap

program_xwl约 15 分钟
原文
文章信息与版本

文章信息

发布
2025年3月6日
收录
2025年12月12日
更新
2026年11月20日
篇幅
约 15 分钟

版本

  1. v1当前

平衡树应该是现在最强大的数据结构。

T1 P3850 [TJOI2007] 书架

思路

我们可以将书架映射为一棵平衡树,再使用 FHQ Treap 的排名分裂与合并完成序列的插入操作,最后把树拍平完成询问。

代码

C++
#include <bits/stdc++.h>
using namespace std;

struct node {int key,lson,rson,siz;string val;} treap[100205];
int n,T,a_root,idx = 0;
string ans[100205];

void push_up(int x)
{
	treap[x].siz = treap[treap[x].lson].siz+1+treap[treap[x].rson].siz;
	return;
}

void rank_split_treap(int root,int k,int &rootl,int &rootr)
{
	if(root == 0)
	{
		rootl = rootr = 0;
		return;
	}
	if(k <= treap[treap[root].lson].siz)
	{
		rootr = root;
		rank_split_treap(treap[root].lson,k,rootl,treap[root].lson);
	}else
	{
		rootl = root;
		rank_split_treap(treap[root].rson,k-treap[treap[root].lson].siz-1,treap[root].rson,rootr);
	}
	push_up(root);
	return;
}

int merge_treap(int rootl,int rootr)
{
	if(rootl == 0 || rootr == 0) return rootl^rootr;
	if(treap[rootl].key <= treap[rootr].key)
	{
		treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
		push_up(rootl);
		return rootl;
	}else
	{
		treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
		push_up(rootr);
		return rootr;
	}
	return 0;
}

void add_node(int x,string val)
{
	int rootl,rootr;
	rank_split_treap(a_root,x-1,rootl,rootr);
	treap[++idx] = {rand(),0,0,1,val};
	a_root = merge_treap(merge_treap(rootl,idx),rootr);
	return;
}

void output(int root)
{
	if(root == 0) return;
	output(treap[root].lson);
	ans[++idx] = treap[root].val;
	output(treap[root].rson);
	return;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	srand(time(0));
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		string x;
		cin >> x;
		add_node(i,x);
	}
	cin >> T;
	while(T--)
	{
		string val;
		int x;
		cin >> val >> x;
		add_node(x+1,val);
	}
	idx = 0;
	output(a_root);
	cin >> T;
	while(T--)
	{
		int x;
		cin >> x;
		cout << ans[x+1] << '\n';
	}
	return 0;
}

T2 P5217 贫穷

思路

首先将字符串映射为平衡树,序列问题就变成了平衡树问题。I,D,T\text{I,D,T} 操作都可以用排名分裂与普通合并实现,R\text{R} 操作可以用懒标记将优化为 O(log⁡n)O(\log n)。重点是 P,Q\text{P,Q} 操作。

P\text{P} 操作

我们可以特别为初始文本特别开 nn 个不会被回收的空间,如果删除操作指向了它们,就打上标记。询问元素 xx 时,若元素 xx 有标记,输出 00。否则从下往上求出排名,代码如下:

C++
void update(int x)//处理x到根节点中的所有翻转懒标记
{
	if(x == 0) return;//递归边界
	update(treap[x].fa);//注意这里要先处理上面的,因为翻转懒标记是由上往下传递的
	push_down(x);//传递
	return;
}

int get_rank(int x)//从下往上求出排名
{
	update(x);//处理x到根节点中的所有翻转懒标记,否则会出错
	int ans = treap[treap[x].lson].siz+1,pos = x;//先加上自己的左子树大小
	while(treap[pos].fa)
	{
		if(treap[treap[pos].fa].rson == pos) ans += treap[treap[treap[pos].fa].lson].siz+1;//如果该节点是他父亲的右儿子,则加上他兄弟子树的大小,注意父亲也要加上
		pos = treap[pos].fa;//上走一层
	}
	return ans;
}

QQ 操作

注意到字符集只包含小写字母,也就是只会有 2626 种字符。我们可以将它们状压为 int\text{int},区间种类数就变成了数二进制表示中有几个 11。代码如下:

C++
void push_up(int x)//上传子树数据
{
	treap[x].siz = treap[treap[x].lson].siz+1+treap[treap[x].rson].siz;//合并子树大小
	treap[x].kind = treap[treap[x].lson].kind | (1<<(treap[x].val-'a')) | treap[treap[x].rson].kind;//合并状态
	treap[treap[x].lson].fa = treap[treap[x].rson].fa = x;//更新父亲
	treap[a_root].fa = 0;//注意根节点没有父亲,不更新会在从下往上求排名时出问题
	return;
}

int get_kinds(int root,int l,int r)
{
	int rootl,middle,rootr;
	rank_split_treap(root,r,rootl,rootr);//把目标区间剥离出来
	rank_split_treap(rootl,l-1,rootl,middle);
	int kind = treap[middle].kind,ans = 0;
	while(kind)
	{
		ans++;
		kind -= kind&-kind;//去掉kind的最后一个1
	}
	merge_treap(merge_treap(rootl,middle),rootr);//别忘了合回去
	return ans;
}

代码

代码细节特别多,调了好久。

C++
#include <bits/stdc++.h>
using namespace std;

struct node {char val;int key,fa,lson,rson,siz,kind;bool rotate;} treap[200005];
int n,T,a_root;
stack<int>memory;

void push_up(int x)
{
	treap[x].siz = treap[treap[x].lson].siz+1+treap[treap[x].rson].siz;
	treap[x].kind = treap[treap[x].lson].kind | (1<<(treap[x].val-'a')) | treap[treap[x].rson].kind;
	treap[treap[x].lson].fa = treap[treap[x].rson].fa = x;
	treap[a_root].fa = 0;
	return;
}

void push_down(int x)
{
	if(treap[x].rotate)
	{
		swap(treap[treap[x].lson].lson,treap[treap[x].lson].rson);
		swap(treap[treap[x].rson].lson,treap[treap[x].rson].rson);
		treap[treap[x].lson].rotate ^= 1;
		treap[treap[x].rson].rotate ^= 1;
		treap[x].rotate = 0;
	}
	return;
}

void rank_split_treap(int root,int k,int &rootl,int &rootr)
{
	if(root == 0)
	{
		rootl = rootr = 0;
		return;
	}
	push_down(root);
	if(k <= treap[treap[root].lson].siz)
	{
		rootr = root;
		rank_split_treap(treap[root].lson,k,rootl,treap[root].lson);
	}else
	{
		rootl = root;
		rank_split_treap(treap[root].rson,k-treap[treap[root].lson].siz-1,treap[root].rson,rootr);
	}
	push_up(root);
	return;
}

int merge_treap(int rootl,int rootr)
{
	if(rootl == 0 || rootr == 0) return rootl^rootr;
	push_down(rootl);
	push_down(rootr);
	if(treap[rootl].key <= treap[rootr].key)
	{
		treap[rootl].rson = merge_treap(treap[rootl].rson,rootr);
		push_up(rootl);
		return rootl;
	}else
	{
		treap[rootr].lson = merge_treap(rootl,treap[rootr].lson);
		push_up(rootr);
		return rootr;
	}
	return 0;
}

void reverse(int l,int r)
{
	int rootl,middle,rootr;
	rank_split_treap(a_root,r,rootl,rootr);
	rank_split_treap(rootl,l-1,rootl,middle);
	swap(treap[middle].lson,treap[middle].rson);
	treap[middle].rotate ^= 1;
	a_root = merge_treap(merge_treap(rootl,middle),rootr);
	return;
}

void init(void)
{
	srand(time(0));
	for(int i = n+T;i > n;i--) memory.push(i);
	return;
}

void insert(int &root,int x,char val)
{
	int rootl,rootr,new_node = memory.top();
	memory.pop();
	treap[new_node] = {val,rand(),0,0,0,1,1<<(val-'a'),0};
	rank_split_treap(root,x,rootl,rootr);
	root = merge_treap(merge_treap(rootl,new_node),rootr);
	return;
}

void delate(int &root,int x)
{
	int rootl,rootr,idx;
	rank_split_treap(root,x-1,rootl,rootr);
	rank_split_treap(rootr,1,idx,rootr);
	if(idx > n) memory.push(idx);
	else treap[idx].val = '#';
	root = merge_treap(rootl,rootr);
	return;
}

char kth(int root,int k)
{
	if(root == 0) return 0;
	push_down(root);
	if(k <= treap[treap[root].lson].siz) return kth(treap[root].lson,k);
	else if(k == treap[treap[root].lson].siz+1) return treap[root].val;
	return kth(treap[root].rson,k-treap[treap[root].lson].siz-1);
}

void update(int x)
{
	if(x == 0) return;
	update(treap[x].fa);
	push_down(x);
	return;
}

int get_rank(int x)
{
	update(x);
	int ans = treap[treap[x].lson].siz+1,pos = x;
	while(treap[pos].fa)
	{
		if(treap[treap[pos].fa].rson == pos) ans += treap[treap[treap[pos].fa].lson].siz+1;
		pos = treap[pos].fa;
	}
	return ans;
}

int get_kinds(int root,int l,int r)
{
	int rootl,middle,rootr;
	rank_split_treap(root,r,rootl,rootr);
	rank_split_treap(rootl,l-1,rootl,middle);
	int kind = treap[middle].kind,ans = 0;
	while(kind)
	{
		ans++;
		kind -= kind&-kind;
	}
	merge_treap(merge_treap(rootl,middle),rootr);
	return ans;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> T;
	init();
	for(int i = 1;i <= n;i++)
	{
		char c;
		cin >> c;
		treap[i] = {c,rand(),0,0,0,1,1<<(c-'a'),0};
		a_root = merge_treap(a_root,i);
	}
	while(T--)
	{
		int x,y;
		char op,c;
		cin >> op >> x;
		if(op == 'I')
		{
			cin >> c;
			insert(a_root,x,c);
		}else if(op == 'D') delate(a_root,x);
		else if(op == 'R')
		{
			cin >> y;
			reverse(x,y);
		}else if(op == 'P')
		{
			if(treap[x].val == '#') cout << "0\n";
			else cout << get_rank(x) << '\n';
		}else if(op == 'T') cout << kth(a_root,x) << '\n';
		else if(op == 'Q')
		{
			cin >> y;
			cout << get_kinds(a_root,x,y) << '\n';
		}
	}
	return 0;
}
原文