版本比较
板子汇总
正文
增加 33 行删除 33 行
294 行未改动全部展开295296297298299300301302303408 行未改动全部展开712713714715716717718719720465 行未改动全部展开11861187118811891190119111921193119420 行未改动12151216121712181219122012211222122331 行未改动125512561257125812591260126112621263210 行未改动全部展开147414751476147714781479148014811482148314841485148614871488148914901491149214931494149518 行未改动1514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154845 行未改动15941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162634 行未改动1661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168630 行未改动17171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174911 行未改动294 行未改动全部展开int v = edge.first, w = edge.second;if(vis[v]) continue;binsiz = 0; dis[v] = w;get_dis(v, u);for(int i = 1; i <= binsiz; i++){for(int j = 1; j <= m; j++){ // qryif(bin[i] <= qry[j]) ans[j] |= flg[qry[j] - bin[i]];}408 行未改动全部展开for(int i = 1; i <= n; i++){cout << (st::t[rt[i]].sum ? st::t[rt[i]].cnt : 0) << '\n';}return 0;}```## dsu on tree465 行未改动全部展开## trie```cppstruct trie{int N;//字符集总大小int son[N][26], siz[N], tot;int calc(char c){return c - 'a';}20 行未改动int calc(char c){return c - 'a';}void insert(char s[]){int len = strlen(s);int now = 0;for(int i = 0; i < len; i++){int digit = calc(s[i]);if(!son[now][digit]){31 行未改动string s;//s:1-indexint n, m;//n:串长度//m:字符集大小char s[maxn];int cnt[maxn];int x[maxn], y[maxn];int prex[maxn];210 行未改动全部展开Cmp_Fn, // 比较函数(默认less<Key>)Tag, // 树类型(默认rb_tree_tag)Node_Update, // 节点策略(必须指定)Alloc // 分配器(默认allocator<char>)>```#### 2. 核心参数说明- **Tag**:`rb_tree_tag`(红黑树,推荐)或 `splay_tree_tag`- **Node_Update**:`tree_order_statistics_node_update`(支持排名操作)#### 3. 关键成员函数```cpp// 插入元素(返回pair<iterator, bool>)iterator insert(const Key& key)// 删除元素(参数可以是键或迭代器)void erase(iterator it)void erase(const Key& key)// 查询key的排名(小于key的元素数量)size_type order_of_key(const Key& key) const18 行未改动using namespace __gnu_pbds;int main() {// 定义类似set的结构tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> tr;// 插入元素tr.insert(10);tr.insert(5);tr.insert(20);tr.insert(15);// 查询操作cout << "Size: " << tr.size() << endl; // 4cout << "Rank of 15: " << tr.order_of_key(15) << endl; // 2(0-indexed排名)// 查询第2小的元素(0-indexed)auto it = tr.find_by_order(2);if (it != tr.end())cout << "3rd element: " << *it << endl; // 15// 范围查询cout << "Elements in [8,18]: ";for (auto it = tr.lower_bound(8); it != tr.upper_bound(18); it++)cout << *it << " "; // 输出 10 15// 删除元素tr.erase(10);cout << "\nAfter erase(10), size: " << tr.size(); // 3return 0;}```45 行未改动int main() {// 定义配对堆(大根堆)typedef priority_queue<int, less<int>, pairing_heap_tag> Heap;Heap pq1, pq2;// 插入元素并保存迭代器auto it1 = pq1.push(30);pq1.push(10);pq1.push(50);auto it2 = pq2.push(40);// 修改元素值pq1.modify(it1, 35); // 30→35// 合并堆pq1.join(pq2); // pq2并入pq1// 堆操作cout << "Top: " << pq1.top() << endl; // 50pq1.pop();cout << "After pop: " << pq1.top() << endl; // 40// 遍历所有元素(非标准操作)cout << "All elements: ";while (!pq1.empty()) {cout << pq1.top() << " "; // 40 35 10pq1.pop();}return 0;}```34 行未改动int main() {// 定义高性能哈希表gp_hash_table<string, int> ht;// 插入数据ht["apple"] = 5;ht["banana"] = 3;ht.insert({"orange", 7});// 访问元素cout << "apple: " << ht["apple"] << endl; // 5// 遍历元素cout << "All items:\n";for (auto& p : ht)cout << p.first << ": " << p.second << endl;// 删除元素ht.erase("banana");cout << "After erase, size: " << ht.size(); // 2return 0;}```30 行未改动using namespace __gnu_pbds;int main() {// 定义字典树typedef trie<string, null_type, trie_string_access_traits<>,pat_trie_tag, trie_prefix_search_node_update> Trie;Trie t;// 插入数据t.insert("apple");t.insert("app");t.insert("banana");t.insert("application");// 前缀搜索 "app"auto range = t.prefix_range("app");cout << "Prefix 'app': ";for (auto it = range.first; it != range.second; ++it)cout << *it << " "; // 输出 app, apple, application// 删除元素t.erase("app");// 再次搜索cout << "\nAfter erase: ";range = t.prefix_range("app");for (auto it = range.first; it != range.second; ++it)cout << *it << " "; // 输出 apple, applicationreturn 0;}```11 行未改动294 行未改动全部展开295296297298299300301302303408 行未改动全部展开712713714715716717718719720465 行未改动全部展开11861187118811891190119111921193119420 行未改动12151216121712181219122012211222122331 行未改动125512561257125812591260126112621263210 行未改动全部展开147414751476147714781479148014811482148314841485148614871488148914901491149214931494149518 行未改动1514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154845 行未改动15941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162634 行未改动1661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168630 行未改动17171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174911 行未改动294 行未改动全部展开int v = edge.first, w = edge.second;if(vis[v]) continue;binsiz = 0; dis[v] = w;get_dis(v, u);for(int i = 1; i <= binsiz; i++){for(int j = 1; j <= m; j++){ // qryif(bin[i] <= qry[j]) ans[j] |= flg[qry[j] - bin[i]];}408 行未改动全部展开for(int i = 1; i <= n; i++){cout << (st::t[rt[i]].sum ? st::t[rt[i]].cnt : 0) << '\n';}return 0;}```## dsu on tree465 行未改动全部展开## trie```cppstruct trie{int N;//字符集总大小int son[N][26], siz[N], tot;int calc(char c){return c - 'a';}20 行未改动int calc(char c){return c - 'a';}void insert(char s[]){int len = strlen(s);int now = 0;for(int i = 0; i < len; i++){int digit = calc(s[i]);if(!son[now][digit]){31 行未改动string s;//s:1-indexint n, m;//n:串长度//m:字符集大小char s[maxn];int cnt[maxn];int x[maxn], y[maxn];int prex[maxn];210 行未改动全部展开Cmp_Fn, // 比较函数(默认less<Key>)Tag, // 树类型(默认rb_tree_tag)Node_Update, // 节点策略(必须指定)Alloc // 分配器(默认allocator<char>)>```#### 2. 核心参数说明- **Tag**:`rb_tree_tag`(红黑树,推荐)或 `splay_tree_tag`- **Node_Update**:`tree_order_statistics_node_update`(支持排名操作)#### 3. 关键成员函数```cpp// 插入元素(返回pair<iterator, bool>)iterator insert(const Key& key)// 删除元素(参数可以是键或迭代器)void erase(iterator it)void erase(const Key& key)// 查询key的排名(小于key的元素数量)size_type order_of_key(const Key& key) const18 行未改动using namespace __gnu_pbds;int main() {// 定义类似set的结构tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> tr;// 插入元素tr.insert(10);tr.insert(5);tr.insert(20);tr.insert(15);// 查询操作cout << "Size: " << tr.size() << endl; // 4cout << "Rank of 15: " << tr.order_of_key(15) << endl; // 2(0-indexed排名)// 查询第2小的元素(0-indexed)auto it = tr.find_by_order(2);if (it != tr.end())cout << "3rd element: " << *it << endl; // 15// 范围查询cout << "Elements in [8,18]: ";for (auto it = tr.lower_bound(8); it != tr.upper_bound(18); it++)cout << *it << " "; // 输出 10 15// 删除元素tr.erase(10);cout << "\nAfter erase(10), size: " << tr.size(); // 3return 0;}```45 行未改动int main() {// 定义配对堆(大根堆)typedef priority_queue<int, less<int>, pairing_heap_tag> Heap;Heap pq1, pq2;// 插入元素并保存迭代器auto it1 = pq1.push(30);pq1.push(10);pq1.push(50);auto it2 = pq2.push(40);// 修改元素值pq1.modify(it1, 35); // 30→35// 合并堆pq1.join(pq2); // pq2并入pq1// 堆操作cout << "Top: " << pq1.top() << endl; // 50pq1.pop();cout << "After pop: " << pq1.top() << endl; // 40// 遍历所有元素(非标准操作)cout << "All elements: ";while (!pq1.empty()) {cout << pq1.top() << " "; // 40 35 10pq1.pop();}return 0;}```34 行未改动int main() {// 定义高性能哈希表gp_hash_table<string, int> ht;// 插入数据ht["apple"] = 5;ht["banana"] = 3;ht.insert({"orange", 7});// 访问元素cout << "apple: " << ht["apple"] << endl; // 5// 遍历元素cout << "All items:\n";for (auto& p : ht)cout << p.first << ": " << p.second << endl;// 删除元素ht.erase("banana");cout << "After erase, size: " << ht.size(); // 2return 0;}```30 行未改动using namespace __gnu_pbds;int main() {// 定义字典树typedef trie<string, null_type, trie_string_access_traits<>,pat_trie_tag, trie_prefix_search_node_update> Trie;Trie t;// 插入数据t.insert("apple");t.insert("app");t.insert("banana");t.insert("application");// 前缀搜索 "app"auto range = t.prefix_range("app");cout << "Prefix 'app': ";for (auto it = range.first; it != range.second; ++it)cout << *it << " "; // 输出 app, apple, application// 删除元素t.erase("app");// 再次搜索cout << "\nAfter erase: ";range = t.prefix_range("app");for (auto it = range.first; it != range.second; ++it)cout << *it << " "; // 输出 apple, applicationreturn 0;}```11 行未改动