跳到正文

个人记录

算法笔记(2)——__gnu_pbds库

幻想繁星约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月1日
篇幅
约 9 分钟
获赞
3

版本

  1. v1当前

一、简介

所谓 pbds 就是 Policy based data structures,即基于策略的数据结构。

__gnu_pbds库里包含了:

  • 哈希表

  • 可并堆

  • 平衡树

  • trie

四种数据结构的封装,在特定的情况下可以帮助我们完成很多基础的操作,省去非常多的码量。

使用前需要先调用头文件 #include<bits/extc++.h>[1]^{[1]} 和命名空间 using namespace __gnu_pbds。

二、哈希表

在 pb_ds 中封装了两种哈希表分别是拉链法的和探测法的。

C++
cc_hash_table<int,int>m;//拉链法
gp_hash_table<int,int>m;//探测法

其使用方法是和 map 几乎完全一样的,支持访问 [] 以及 find(),但并不支持 count() 函数。

其内部是和 unordered_map 类似的哈希表,所以其时间复杂度也是常数较大的 O(1)O(1),但是其并不会像 unordered_map 一样容易被卡,性能上也比 unordered_map 略优。

例题:P3370 【模板】字符串哈希

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
int n,ans;
string s;
gp_hash_table<string,bool>vis;
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>s;
		if(vis[s]==0)
		{
			vis[s]=1;
			ans++;
		}
	}
	cout<<ans;
	return 0;
}

三、可并堆

在 pb_ds 中封装了五种可并堆分别是:

  1. 配对堆(pairing_heap_tag)

  2. 二叉堆(binary_heap_tag)

  3. 二项堆(binomial_heap_tag)

  4. 冗余计数二项堆(rc_binomial_heap_tag)

  5. Fibonacci 堆[2]^{[2]}(thin_heap_tag)

其定义模板为 __gnu_pbds::priority_queue<T,Compare,Tag>

其中 T 为存储的数据类型,Compare 为严格的弱序比较类型,Tag 为所使用的堆的类型(见上)。

例如 __gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag> 就是一个存储 int 类型的小根堆。

pbds 中所有可并堆支持的操作是一样的,不同的是其时间复杂度,在 OI 中一般配对堆的时间复杂度是比较容易被接受的。

  • push() 向堆中压入一个元素。

  • pop() 将堆顶元素弹出。

  • top() 返回堆顶元素。

  • size() 返回元素个数。

  • empty() 返回是否非空。

  • modify(point_iterator,const key)把迭代器位置的 key 修改为传入的 key,并对底层储存结构进行排序。

  • erase(point_iterator) 把迭代器位置的键值从堆中擦除。

  • join(__gnu_pbds::priority_queue &other) 把 other 合并到当前堆,并把 other 清空。

其中 top() 函数、size()函数、empty()函数时间复杂度都是 O(1)O(1) 的。

其他操作每种堆时间复杂度并不相同,具体如下:

例题:P3377 【模板】左偏树(可并堆)

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
int n,m;
__gnu_pbds::priority_queue<pair<int,int>,greater<pair<int,int> >,pairing_heap_tag>q[100005];
vector<int>f(100005);
vector<bool>vis(100005);
int opt,x,y;
int find(int x)
{
	return f[x]==x?x:f[x]=find(f[x]);
}
void add(int x,int y)
{
	f[find(y)]=find(x);
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		f[i]=i;
	for(int i=1;i<=n;i++)
	{
	    int a;
	    cin>>a;
	    q[i].push(make_pair(a,i));
	}
	while(m--)
	{
		cin>>opt>>x;
		if(opt==1)
		{
			cin>>y;
			if(vis[x]==0&&vis[y]==0&&find(x)!=find(y))
			{
				q[find(x)].join(q[find(y)]);
				add(x,y);
			}
		}
		else
		{
			if(vis[x]==1)
			{
				puts("-1");
				continue;
			}
			cout<<q[find(x)].top().first<<endl;
			vis[q[find(x)].top().second]=1;
			q[find(x)].pop();
		}
	}
    return 0;
}

四、平衡树

在 pb_ds 中封装了三种平衡树分别是:

  1. 红黑树(rb_tree_tag)

  2. Splay树(splay_tree_tag)

  3. 有序向量树(ov_tree_tag)[3]^{[3]}

其定义模板为 _gnu_pbds::tree<Key,Mapped,Cmp_Fn,Tag,Node_Update>

其中 Key 为存储的数据类型,Mapped 为映射规则类型(如果要指示关联容器是集合,此处填入 null_type;如果要指示关联容器是带值的集合,此处填入类似于 <Key, Value> 的 Value 类型),Cmp_Fn 为关键字比较函数,Tag 为所使用的平衡树的类型(见上),Node_Update 是用于更新节点的策略,默认使用 null_node_update,若要使用 order_of_key 和 find_by_order 函数,需要使用 tree_order_statistics_node_update。

例如 tree<int,null_type,less<int>,rb_tree_tag> 就是一个存储 int 类型的红黑树。

pbds 中平衡树支持的操作有:

  • insert(x) 向树中插入一个元素 x。

  • erase(x) 从树中删除一个元素/迭代器 x。

  • order_of_key(x) 返回 x 以 Cmp_Fn 比较的排名。

  • find_by_order(x) 返回 Cmp_Fn 比较的排名所对应元素的迭代器。

  • lower_bound(x) 以 Cmp_Fn 比较做 lower_bound 返回迭代器。

  • upper_bound(x) 以 Cmp_Fn 比较做 upper_bound 返回迭代器。

  • join(x) 将 x 树并入当前树,前提是两棵树的类型一样,x 树被删除。

  • split(x,b) 以 Cmp_Fn 比较,小于等于 x 的属于当前树,其余的属于 b 树。

  • empty() 返回是否为空。

  • size() 返回大小。

其时间复杂度与手写的平衡树是一样的,只不过常数较大[4]^{[4]}。

例题:P3369 【模板】普通平衡树

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
int n,cnt;
int opt,x;
tree<pair<int,int>,null_type,less<pair<int,int> >,rb_tree_tag,tree_order_statistics_node_update>t;
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>opt>>x;
		if(opt==1)
			t.insert(make_pair(x,cnt++));
		if(opt==2)
			t.erase(t.lower_bound(make_pair(x,0)));
		if(opt==3)
			cout<<t.order_of_key(make_pair(x,0))+1<<endl;
		if(opt==4)
			cout<<(*t.find_by_order(x-1)).first<<endl;
		if(opt==5)
			cout<<(*t.find_by_order(t.order_of_key(make_pair(x,0))-1)).first<<endl;
		if(opt==6)
			cout<<(*t.find_by_order(t.order_of_key(make_pair(x,2147483647)))).first<<endl;
	}
	return 0;
}

五、trie

这个东西干嘛不手写?


[1]:这个头文件 DEV-C++ 编译不过去。

[2]:其实是除了合并的复杂度都和 Fibonacci 堆一样的一个堆。

[3]:只是一个由 vector 实现的有序结构,类似于排序的 vector 来实现平衡树,性能取决于数据想不想卡你。

[4]:但是即使是封装的红黑树也肯定比你手写的平衡树性能要优。

原文