跳到正文

算法·理论

pb_ds入门使用指南

MaxLKY约 8 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

前置知识

map 和 set(注意,都是小写)

C++
map<int,int> ma;
set<int> st;

我们用 mp 表示 map,用 st 表示 set。

详细看oi-wiki上的讲解。

代码头文件

pb_ds 不包含在万能头文件内,需要自己引用。

C++
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template<class Key,class Val=__gnu_pbds::null_type>
using Map=  __gnu_pbds::tree<Key, Val, less<Key>,
            __gnu_pbds::rb_tree_tag,
            __gnu_pbds::tree_order_statistics_node_update>;

把这些写在主函数前即可。

注意:此时不能 #define int long long

pb_ds使用

成员函数

  • insert(x):插入一个元素 x,返回 std::pair<point_iterator, bool>,其中第一个元素代表插入位置的迭代器,第二个元素代表是否插入成功。
  • erase(x):删除一个元素/迭代器 x。如果 x 是迭代器,则返回指向 x 下一个的迭代器(如果 x 是 end() 则返回 end());如果 x 是 Key,则返回是否删除成功(如果不存在则删除失败)。
  • order_of_key(x):返回严格小于 x 的元素个数(以 Cmp_Fn 作为比较逻辑),即从 0 开始的排名。可以在文件头重写比较函数并替换 pb_ds 头文件的 less<Key> 比较函数
  • find_by_order(x):返回 Cmp_Fn 比较的排名所对应元素的迭代器。
  • lower_bound(x):返回第一个不小于 x 的元素所对应的迭代器(以 Cmp_Fn 作为比较逻辑)。
  • upper_bound(x):返回第一个严格大于 x 的元素所对应的迭代器(以 Cmp_Fn 作为比较逻辑)。
  • join(x):将 x pb_ds并入当前pb_ds,x pb_ds被清空(必须确保两树的 比较函数 和 元素类型 相同)。
  • split(x,b):以 Cmp_Fn 比较,小于等于 x 的属于当前pb_ds,其余的属于 b pb_ds。
  • empty():返回是否为空。
  • size():返回大小。

注意!!!

join(x) 函数需要保证并入树的键的值域与被并入树的键的值域 不相交(也就是说并入树内所有值必须全部大于/小于当前树内的所有值),否则会抛出join_error 异常。

如果要合并两棵值域有交集的树,需要将一棵树的元素一一插入到另一棵树中。

重定义比较函数

先了解一下什么是伪函数。

伪函数

伪函数写法上是一个结构体,但是可以当作函数来调用。

例如我们用普通函数实现对一个 int 类型数组写比较函数代码如下:

C++
bool cmp(int a,int b){
    return a>b;
}

不多做解释。

上面的代码用伪函数可以这么写:

C++
struct cmp//函数名
{
    //函数的类型为size_t,返回值必须是bool,重定义()
    size_t operator()(const int &a,const int &b)const//每一个const和&都是必要的
    {
        //函数内容
        return a>b;
    }
};

重定义

先写一个伪函数,把头文件中

C++
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template<class Key,class Val=__gnu_pbds::null_type>
using Map=  __gnu_pbds::tree<Key, Val, less<Key>,
            __gnu_pbds::rb_tree_tag,
            __gnu_pbds::tree_order_statistics_node_update>;

less<Key> 换成自己的函数名即可。

注意!!!

自定义的比较函数所比较的类型必须和自己接下来定义的pb_ds类型相同。

这些已经足够在算法竞赛中作为平衡树使用了。

实例

用作平衡树

C++
#include<bits/stdc++.h>
using namespace std;
struct cmp{
	size_t operator()(const pair<int,int> &a,const pair<int,int> &b)const{
		return a<b;
	}
};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template<class Key,class Val=__gnu_pbds::null_type>
using Map=  __gnu_pbds::tree<Key, Val, cmp,
            __gnu_pbds::rb_tree_tag,
            __gnu_pbds::tree_order_statistics_node_update>;
inline int in()
{
    int k=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9')
    {
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')k=k*10+c-'0',c=getchar();
    return k*f;
}
inline void out(int x)
{
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else out(x/10),putchar(x%10+'0');
}
const int N=1.02e9;
Map<pair<int,int>> st;
int cnt=0;
inline void ins(int x){
    st.insert(make_pair(x,++cnt));
}
inline void del(int x){
	auto it=st.lower_bound(make_pair(x,0));
	st.erase(it);
}
inline int rk(int x){
	return st.order_of_key(make_pair(x,0))+1;
}
inline int kth(int k){
	return st.find_by_order(k-1)->first;
}
inline int pre(int x){
	auto it=st.lower_bound(make_pair(x,0));
	return prev(it)->first;
}
inline int suc(int x){
	auto it=st.upper_bound(make_pair(x,N));
	return it->first;
}
int main(){
    int n,m,lst=0;
    cin>>n>>m;

	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		ins(x);
	}
	int ans=0;
	for(int i=1;i<=m;i++){
		int op,x;
		scanf("%d%d",&op,&x);
		x^=lst;
		if(op==1){
            ins(x);
		}
		if(op==2){
            del(x);
		}
		if(op==3){
            ans^=(lst=rk(x));
		}
		if(op==4){
            ans^=(lst=kth(x));
		}
		if(op==5){
           ans^=(lst=pre(x));
		}
        if(op==6){
            ans^=(lst=suc(x));
        }
	}
	cout<<ans;
    return 0;
}

附上记录

原文