[笔记] stl和pbds的好东西

前言:模拟赛写这个题,用的最麻烦的离散化+树状数组,直接调炸了。赛后发现pbds的tree可以极其简便的解决此题,于是有了这篇文章。

STL容器

set/multiset

两个用红黑树实现的集合,区别在于 set 不能重复,而 multiset 可以。

定义

非常简单:

#include<set>// 必备头文件
using namespace std;
set<int> st;// 默认升序
multiset<int> st;// 可重集
set<int, greater<int> > st;// 降序
struct Node{
    ...;
    friend bool operator<(const Node &x, const Node &y){
        ...
    }
};
set<Node> st;// 可以是任何具有比较规则的类型

插入/删除

set 中的 insert 和 erase 函数可以支持插入和删除,时间复杂度基本都是 \(O(\log n)\),有下面几种用法:

set<int> a{3}, b{7,8,9};// 初始化构造
a.insert(1);
a.insert(2);
a.insert(1);// 没有效果,因为1已经在a中了
// a:{1, 2, 3}
a.insert({4, 5, 6});// 一次性插入多个元素
a.insert(b.begin(), b.end());// 按迭代器区间插入,O(n)
// a:{1, 2, 3, 4, 5, 6, 7, 8, 9}
set<string> c;
string s; cin>>s;
c.emplace(s);// 原地构造插入,相比 insert 少一次拷贝,对 string/struct/class 更快

a.erase(9);// 按值删除
auto it=a.find(5);// 找5所在迭代器,后面会讲
it=a.erase(it);// 删迭代器,均摊O(1),返回下一个有效的迭代器(非常有用)
// a:{1, 2, 3, 4, 6, 7, 8}
a.erase(a.begin(), a.end());// 按迭代器区间删除,O(n)
// a为空

multiset 和 set 没啥区别,只不过元素可以相等。但有一点需要注意:

multiset<int> a, b;
for(int i=1; i<=10; i++) a.insert(1), b.insert(1);
a.erase(1);// 会把a中所有1都删掉
auto it=b.find(1);
b.erase(it);// 这样可以做到只删一个

查找

set 底层是平衡树,因此支持各种各样的查找操作:

set<int> a;
multiset<int> b;
// 省略初始化过程。设x为一个int类型的值
a.count(x);// 查询x的出现次数(set里只有0,1)
b.count(x);// 查询x的出现次数
auto it=a.find(x);// 查询x的迭代器,查不到会返回 a.end()
if(it!=a.end()){...}// 查到了,干一些事情
it=a.lower_bound(x);// 查>=x的最小的值,返回迭代器,it==a.end()表示没有大于等于x的数
it=a.upper_bound(x);// 查>x的最小的值
// 注意不要写成 lower_bound(a.begin(), a.end(), x); 这是O(n)的

迭代器和遍历

迭代器就相当于 set 的指针,用法和指针也区别不大:

set<int> a;
multiset<int> b;
auto it=a.find(x);// it就是迭代器
a.begin();// 第一个元素的迭代器
a.end();// 最后一个元素后一个位置的迭代器,注意,这个迭代器指向无效位置,不能直接访问其指向的值,(--a.end())才是最后一个元素的迭代器
a.rbegin();// 反向迭代器的第一个位置,即a的最后一个位置。
a.rend();// 反向迭代器的最后一个位置的后一个位置,同样指向无效位置。
// 注意:正向迭代器和反向迭代器不一样,不能将它们混用。
for(int x:a) cout<<x<<" ";// 正序遍历
for(auto it=a.begin(); it!=a.end(); it++) cout<<(*it)<<" ";// 正序遍历,(*it)表示取it指向的值,和指针用法一样
for(auto it=a.rbegin(); it!=a.rend(); it++) cout<<(*it)<<" ";// 倒序遍历

pb_ds容器

tree

set 虽然非常好用,但缺少了平衡树最强的操作:查询排名和第 \(k\) 小,而这正是平衡树最常见的用法。

所以pbds的tree就出现了,它在支持所有stl的平衡树操作的基础上,又多了查询排名和第 \(k\) 小的功能。缺点就是常数比较大,和FHQ差不多,并且比较难背。

定义

pb_ds 所有东西都有非常长的头文件。

#include<ext/pb_ds/assoc_container.hpp>// 所有常用的pbds都要用的头文件
#include<ext/pb_ds/tree_policy.hpp>// tree要用的头文件
using namespace __gnu_pbds;// 需要使用的命名空间

然后就是 tree 极为复杂的定义:

tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> a;

是不是已经眼花了?别着急,我们一个个看这些参数的作用:

  • int:数据类型。
  • null_type:表示这个平衡树实现的是set功能。如果写入一个具体的数据类型(如int),则实现的是map的映射功能。
  • less<int>:排序规则。
  • rb_tree_tag:底层实现规则,rb_tree_tag 表示用红黑树实现,还有splay和vector两种实现方式,不过这两种都非常慢,一般没有啥用。
  • tree_order_statistics_node_update:表示是否需要用到查询排名的功能。

功能

绝大部分功能和stl没有区别,额外多了几个新函数:

  • a.find_by_order(k):查排名为 \(k\) 的数。注意:排名从0开始,也就是说最小值的排名是0,查不到返回 st.end()。
  • a.order_of_key(x):查 \(x\) 的排名,同样的,排名从0开始。
  • a.split(k, b):平衡树分裂,把 \(<k\) 的分到 a 里,\(\ge k\) 的分到 b 里。
  • a.join(b):平衡树合并,把 b 并入 a,之后 b 变为空。

不过我们注意到,tree 是不可重集,也就是不能保存重复元素。如果要存重复元素,我们就得用 pair<int,int> 给值绑上一个时间戳,查询的时候就给所查值绑上一个正/负无穷大即可。

附上 普通平衡树 和 P1486 [NOI2004] 郁闷的出纳员 的代码,便于理解:

普通平衡树
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define rbt tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;

const int N=1e5+10, INF=0x3f3f3f3f;
rbt tr;
int n, m, ans, ord;

inline int getpre(const rbt &st, int x){
    auto it=st.lower_bound({x, -INF});
    if(it==st.begin()) return ~INF;
    return (*(--it)).first;
}
inline int getnxt(const rbt &st, int x){
    auto it=st.upper_bound({x, INF});
    if(it==st.end()) return ~INF;
    return (*it).first;
}
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1; i<=n; i++){
		int x; cin>>x;
		tr.insert({x, ++ord});
	}
	int lst=0;
	while(m--){
		int ch, x; cin>>ch>>x; x^=lst;
		if(ch==1){
			tr.insert({x, ++ord});
		}
		if(ch==2){
			auto it=tr.lower_bound({x, -1});
			tr.erase(it);
		}
		if(ch==3) ans^=lst=tr.order_of_key({x, -INF})+1;
		if(ch==4) ans^=lst=tr.find_by_order(x-1)->first;
		if(ch==5)
			ans^=lst=getpre(tr, x);
		if(ch==6)
			ans^=lst=getnxt(tr, x);
	}
	cout<<ans;
	return 0;
}
P1486 [NOI2004] 郁闷的出纳员
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
using namespace __gnu_pbds;
using namespace std;
#define rbt tree<pair<int,int>, null_type, greater<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update>

const int N=3e5+10, INF=0x3f3f3f3f;
rbt tr;
int n, m, ans, Min, K;

void fastio(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}
inline int Rank(const rbt &tr, int x){
	return tr.order_of_key({x, ~INF})+1;
}
inline int kth(const rbt &tr, int k){
	auto it=tr.find_by_order(k-1);
	if(it==tr.end()) return ~INF;
	return (*it).first;
}
signed main(){
	fastio();
	cin>>m>>Min;
	int add=0;
	for(int i=1; i<=m; i++){
		char ch; int x;
		cin>>ch>>x;
		if(ch=='I'){
			x-=add;
			if(x>=Min) tr.insert({x, i});
		}
		else if(ch=='A') Min-=x, add+=x;
		else if(ch=='S'){
			Min+=x; add-=x;
			rbt y;
			tr.split({Min, -INF}, y);
			ans+=y.size();
		}
		else{
			int res=kth(tr, x);
			if(res!=~INF) cout<<res+add<<"\n";
			else cout<<"-1\n";
		}
	}
	cout<<ans;
	return 0;
}
posted @ 2026-10-21 22:33  vector_int  阅读(18)  评论(0)    收藏  举报