pbds学习笔记

pbds 是 GNU 扩展库的一部分,在 g++ 环境下可以直接使用,clang 下不能使用。

#include <bits/extc++.h> // pbds万能头
using namespace __gnu_cxx;
using namespace __gnu_pbds;

#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;

using heap = __gnu_pbds::priority_queue<int>; // 默认大根堆
heap q;
using small_heap = __gnu_pbds::priority_queue<int, less<int>>; // 小根堆
struct myCmp { bool operator()(node x, node y) { return x.b < y.b; } }; // 也可以在自定义类型里重载运算符
using my_heap = __gnu_pbds::priority_queue<node, myCmp>;

q.top(); q.pop(); q.size(); q.empty(); q.clear(); // 和 STL 完全一样
id = q.push(10); // 效果和 STL 一样,会多返回一个指向插入的元素的迭代器(可以不接收)复杂度O(1)
q.modify(id, 5); // 直接修改迭代器位置的元素,复杂度均摊O(log n)
q.erase(id); // 直接删除迭代器位置的元素,均摊O(log n)
q.join(p); // 把堆p合并到堆q,p清空,复杂度O(1)
*/

modify 的一个应用是在 Dijkstra 中可以直接修改堆内元素,不需要重复插入+ vis 数组记录。

哈希表

#include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
gp_hash_table<int, int> gp_table;
cc_hash_tabel<int, int> cc_table; // 目前似乎cc更快?总之都比unordered_map快

// 用时间设置哈希值防卡,cf常用
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
    // 如果要用string之类的类型作为 key,可以调用std::hash
    hash<string> hasher;
    int operator()(string s) const { return hasher(s) ^ RANDOM; }
    // std::pair没有默认的哈希,需要自定义
    int operator()(pair<int, int> x) const { return x.first* 31 + x.second; }
};
cc_hash_table<int, int, chash> table1;
cc_hash_table<string, int, chash> table2;

平衡树

不如 FhqTreap,贴个OI Wiki 链接

posted @   XYukari  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示