跳到正文

个人记录

pbds 背诵

最後の祈りを约 3 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

namespace\text{namespace} 是 __gnu_pbds,头文件可以直接 #include<bits/extc++.h> ,不过 devcpp 用不了,只能 #include<ext/pb_ds/assoc_container.hpp> 以及对应的头文件,迭代器都是 point_iterator\text{point\_iterator}。

哈希表

根据别人以及自己的 benchmark\text{benchmark} 来说 hash_table\text{hash\_table} 表现很优秀,非构造数据卡点常还是要写的。

  1. 头文件: #include<ext/pb_ds/hash_policy.h>
  2. 声明:gp_hash_table<int,int>,当 unordered_set\text{unordered\_set} 用的话是 gp_hash_table<int,nulltype>
  3. 使用:同 unordered_map\text{unordered\_map} ,直接遍历也是 O(n)O(n) 乱序的。

堆

用配对堆,支持 O(1) push joinO(1)\ \text{push join},O(log⁡) erase modifyO(\log)\ \text{erase modify} 。

  1. 头文件: #include<ext/pb_ds/priority_queue.h>
  2. 声明:__gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag>
  3. 使用:push\text{push} 会返回迭代器,join\text{join} 为 a.join(b)\text{a.join(b)} 且清空 b\text b ,erase\text{erase} 为 q.erase(it)\text{q.erase(it)},modify\text{modify} 为 q.modify(it,x)\text{q.modify(it,x)} ,当 it\text{it} 为 NULL\text{NULL} 时就不在堆中,自定义比较同标准库要用 struct\text{struct} 。

平衡树

用红黑树,相比 set\text{set} 支持 kth rank join split\text{kth rank join split} ,和 fhq_treap\text{fhq\_treap} 的常数差不多,常数较大。

  1. 头文件: #include<ext/pb_ds/tree_policy.h>
  2. 声明:tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
  3. 使用:同 set\text{set} ,实现 multiset\text{multiset} 要用 pair\text{pair} 通过第二关键字区分,下标从 00 开始,kth\text{kth} 是 *t.find_by_order(k-1)\text{*t.find\_by\_order(k-1)} ,求排名是 t.order_of_key(x)+1\text{t.order\_of\_key(x)+1} ,合并是 a.join(b)\text{a.join(b)} 且清空 b\text b ,要保证两树值域不交,分裂是 a.split(x,b)\text{a.split(x,b)} 把 b\text b 清空再把 >x>x 的元素分给 b\text b 。

reference:pbds库学习笔记 - 知乎

原文
存档评论1 条抓取于