sort(v.first(),v.end(),cmp())
unique(v.first(),v.end(),cmp()) 第三个参数可以传入一个bool型,用来判断是不是相等,返回unique后的超尾
max_element(v.first(),v.end(),cmp()) 返回一个迭代器
max_element(v.first(),v.end(),cmp()) 返回一个迭代器
nth_elemtn(v.first(),v.first()+nth,v.end(),cmp()) 对整个容器部分排序后,返回nth迭代器。其中 $[first,nth)$ 都比nth小, $[nth,last)$ 都不小于nth。

reverse(v.first(),v.end())反转
rotate(v.first(),v.middle(),v.end()) 左右交换位置,用处不大还慢(因为随机访问对CPU缓存机制的不友好,太差劲了)
random_shuffle(first, last, rand)产生随机序列

pb_ds

//首先需要以下头文件以及命名空间 #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;

优先队列
#include <ext/pb_ds/priority_queue.hpp>

可合并优先队列pairing_heap_tag
            此外,pb_ds库的优先队列支持合并操作,pairing_heap的合并时间复杂度是O(logn)的,可以说基本上完美代替了左偏树。合并的调用方式是:

支持迭代器,可以记录push的返回迭代器来对优先队列中的元素进行修改

join(priority_queue &other)  //合并两个堆,other会被清空
            split(Pred prd,priority_queue &other)  //分离出两个堆
            modify(point_iterator it,const key)  //修改一个节点的值

因为重名的原因一定要加上 __gnu_pbds::
            __gnu_pbds::priority_queue<int,greater<int>,pairing_heap_tag> pq;
// 对两优先队列进行一些操作
            a.join(b);(O(1)合并)
  此时优先队列b内所有元素就被合并进优先队列a中,且优先队列b被清空。

名次树/红黑树

  1. #include <ext/pb_ds/tree_policy.hpp>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <bits/stdc++.h>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6.  
  7. typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> rbtree;

// int类型

// null_type为映射类型, 低版本g++为 null_mapped_type// less<int>, greater<int> 比较器// rb_tree_tag 和 splay_tree_tag 选择树的类型// tree_order_statistics_node_update 结点更新// insert, erase// order_of_key rank// find_by_order() kth// lower_bound() 前继, >=x 最小的迭代器// upper_bound() 后继 >x 最小的迭代器// a.join(b) b并入a,前提是两颗树的取值范围不相交// a.split(v, b) key <= v的属于a,其他属于// 注意,插入的元素会去重,如set

迭代器支持++和--
计数从0开始而不是从1开始,例如查询第k+1小的数,使用find_by_order()函数,返回的为迭代器。t.find_by_order(2),查询(常说的第3小的数)
查询比x小的数的个数,注意,是比x小的个数,不是x的排名。t1.order_of_key(2),查询比2小的数的个数,相当于2的排名-1。

正常的splay

  1. #include <ext/pb_ds/tree_policy.hpp>#include <ext/pb_ds/assoc_container.hpp>#include <bits/stdc++.h>
  2. using namespace __gnu_pbds;
  3. using namespace std;
  4. const int MAXN = + ;
  5. int price, menu[MAXN], cnt[MAXN];
  6. template<class T>inline bool nextInt(T &n) {
  7. T x = , tmp = ;
  8. char c = getchar();
  9. while((c < '' || c > '') && c != '-' && c != EOF)
  10. c = getchar();
  11. if(c == EOF)
  12. return false;
  13. if(c == '-')
  14. c = getchar(), tmp = -;
  15. while(c >= '' && c <= '')
  16. x *= , x += (c - ''), c = getchar();
  17. n = x*tmp;
  18. return true;
  19. }
  20. template<class T>inline void Out(T n) {
  21. if(n < ) {
  22. putchar('-');
  23. n = -n;
  24. }
  25. int len = , data[];
  26. while(n) {
  27. data[len++] = n%;
  28. n /= ;
  29. }
  30. if(!len)
  31. data[len++] = ;
  32. while(len--)
  33. putchar(data[len]+);
  34. }
  35.  
  36. struct Splay_Tree {
  37. struct Node {
  38. int father, childs[], key, cnt, _size;
  39. inline void init() {
  40. father = childs[] = childs[] = key = cnt = _size = ;
  41. } inline void init(int father, int lchild, int rchild, int key, int cnt, int sz) {
  42. this -> father = father, childs[] = lchild, childs[] = rchild;
  43. this -> key = key, this -> cnt = cnt, _size = sz;
  44. }
  45. } tre[MAXN];
  46. int sign, root;
  47. inline void init() {
  48. sign = root = ;
  49. }
  50. inline bool judge(int x) {
  51. return tre[ tre[x].father ].childs[] == x;
  52. }
  53. inline void update(int x) {
  54. if(x) {
  55. tre[x]._size = tre[x].cnt;
  56. if(tre[x].childs[]) {
  57. tre[x]._size += tre[ tre[x].childs[] ]._size;
  58. }
  59. if(tre[x].childs[]) {
  60. tre[x]._size += tre[ tre[x].childs[] ]._size;
  61. }
  62. }
  63. }
  64. inline void rotate(int x) {
  65. int y = tre[x].father, z = tre[y].father, k = judge(x);
  66. //tre[y].childs[k] = tre[x].childs[!k], tre[ tre[x].childs[!k] ].father = y; //tre[x].childs[!k] = y, tre[y].father = x; //tre[z].childs[ tre[z].childs[1] == y ] = x, tre[x].father = z;
  67. if(k == ) { ///zig tre[y].childs[0] = tre[x].childs[1], tre[ tre[x].childs[1] ].father = y; tre[x].childs[1] = y, tre[y].father = x; } else { ///zag tre[y].childs[1] = tre[x].childs[0], tre[ tre[x].childs[0] ].father = y; tre[x].childs[0] = y, tre[y].father = x; } tre[z].childs[ tre[z].childs[1] == y ] = x, tre[x].father = z;
  68. update(y);
  69. }
  70. inline void splay(int x,int goal) {
  71. for(int father; (father = tre[x].father) != goal; rotate(x) ) {
  72. if(tre[father].father != goal) {
  73. rotate(judge(x) == judge(father) ? father : x);
  74. }
  75. }
  76. root = x;
  77. }
  78. inline void insert_node(int x) {
  79. if(root == ) {
  80. tre[++sign].init(, , , x, , );
  81. root = sign;
  82. return ;
  83. }
  84. int now = root, father = ;
  85. while() {
  86. if(tre[now].key == x) {
  87. tre[now].cnt ++;
  88. update(now), update(father);
  89. splay(now, );
  90. break;
  91. }
  92. father = now;
  93. if(x > tre[now].key) {
  94. now = tre[now].childs[];
  95. } else {
  96. now = tre[now].childs[];
  97. }
  98. if(now == ) {
  99. tre[++sign].init(father, , , x, , );
  100. if(x > tre[father].key) {
  101. tre[father].childs[] = sign;
  102. } else {
  103. tre[father].childs[] = sign;
  104. }
  105. update(father);
  106. splay(sign, );
  107. break;
  108. }
  109. }
  110. }
  111. inline int pre() {
  112. int now = tre[root].childs[];
  113. while(tre[now].childs[]) {
  114. now = tre[now].childs[];
  115. }
  116. return now;
  117. }
  118. inline int next() {
  119. int now = tre[root].childs[];
  120. while(tre[now].childs[]) {
  121. now = tre[now].childs[];
  122. }
  123. return now;
  124. }
  125. inline int find_rank(int x) { /// 找x的排名 int now = root, ans = 0; while(1) { if(x < tre[now].key) { now = tre[now].childs[0]; } else { if(tre[now].childs[0]) { ans += tre[ tre[now].childs[0] ]._size; } if(x == tre[now].key) { splay(now, 0); return ans + 1; } ans += tre[now].cnt; now = tre[now].childs[1]; } } }
  126. inline int find_by_order(int x) {
  127. int now = root;
  128. while() {
  129. if(tre[now].childs[] && x <= tre[ tre[now].childs[] ]._size ) {
  130. now = tre[now].childs[];
  131. } else {
  132. int rchild = tre[now].childs[], sum = tre[now].cnt;
  133. if(rchild) {
  134. sum += tre[rchild]._size;
  135. }
  136. if(x <= sum) {
  137. int ans = tre[now].key;
  138. splay(now, );
  139. return ans;
  140. }
  141. x -= sum;
  142. now = tre[now].childs[];
  143. }
  144. }
  145. }
  146. inline int find_rankx(int x) { /// 找排名为x的数字 int now = root; while(1) { if(tre[now].childs[0] && x <= tre[ tre[now].childs[0] ]._size ) { now = tre[now].childs[0]; } else { int lchild = tre[now].childs[0], sum = tre[now].cnt; if(lchild) { sum += tre[lchild]._size; } if(x <= sum) { return tre[now].key; } x -= sum; now = tre[now].childs[1]; } } }
  147. inline void del(int x) {
  148. find_rank(x);
  149. if(tre[root].cnt > ) {
  150. tre[root].cnt --;
  151. update(root);
  152. return ;
  153. }
  154. if(!tre[root].childs[] && !tre[root].childs[]) {
  155. tre[root].init();
  156. root = ;
  157. return ;
  158. }
  159. if(!tre[root].childs[]) {
  160. int old_root = root;
  161. root = tre[root].childs[], tre[root].father = , tre[old_root].init();
  162. return ;
  163. }
  164. if(!tre[root].childs[]) {
  165. int old_root = root;
  166. root = tre[root].childs[], tre[root].father = , tre[old_root].init();
  167. return ;
  168. }
  169. int pre_node = pre(), old_root = root;
  170. splay(pre_node, );
  171. tre[root].childs[] = tre[old_root].childs[];
  172. tre[ tre[old_root].childs[] ].father = root;
  173. tre[old_root].init();
  174. update(root);
  175. }
  176. inline bool find(int x) {
  177. int now = root;
  178. while() {
  179. if(now == ) {
  180. return ;
  181. }
  182. if(x == tre[now].key) {
  183. splay(now, );
  184. return ;
  185. }
  186. if(x > tre[now].key) {
  187. now = tre[now].childs[];
  188. } else {
  189. now = tre[now].childs[];
  190. }
  191. }
  192. }
  193. }
  194. tre;
  195. int n, opt, x;
  196. int main() {
  197. scanf("%d", &price);
  198. int id = ;
  199. while(nextInt(opt) && opt) { //scanf("%d", &x); nextInt(x); if(opt == 1) { /// add price of x tre.insert_node(x); cnt[x]++; /// 价格x的菜的数量 menu[id++] = x; /// 第id道菜价格x continue; } if(opt == 2) { int p = menu[x]; tre.find(p); if(cnt[p]) { cnt[p]--; } continue; } if(opt == 3) { int res = tre.find_by_order(x); if(res > price) { puts("Dui bu qi,Mei you."); } else if(cnt[res] == 0) { puts("Mei you. Zhe ge ke yi you. Zhe ge zhen mei you!"); } else { printf("You. %d Yuan.\n", res); } } }
  200.  
  201. return ;
  202. }

平衡树
pb_ds库这次内置了红黑树(red-black tree)、伸展树(splay tree)和排序向量树(ordered-vector tree,没找到通用译名,故自行翻译)。这些封装好的树都支持插入(insert)、删除(erase)、求kth(find_by_order)、求rank(order_of_key)操作

封装好的红黑树可以达到手写Treap的速度。

求kth(find_by_order)返回的是迭代器,求rank返回的是值,两者都是从0开始计算的。

此外,它们也支持合并(join)和分离(split)操作。用法如下。

tree<int,null_type> a,b;
// 对两平衡二叉树进行一些操作
a.join(b);
// 对两平衡二叉树进行一些操作
a.split(v,b);
  这里需要进行一些解释。join操作的前提是两棵树的key的取值范围不相交,否则会抛出一个异常;合并后平衡二叉树b被清空。split操作中,v是一个与key类型相同的值,表示key小于等于v的元素属于平衡二叉树a,其余的属于平衡二叉树b,注意此时后者已经存有的元素将被清空。

rope

  1. #include<ext/rope>
  2. using namespace __gnu_cxx;
  3.  
  4. append()
  5. string &append(const string &s,int pos,int n);
  6.  
  7. //把字符串s中从pos开始的n个字符连接到当前字符串的结尾或
  8.  
  9. a.append(b);
  10.  
  11. substr()
  12. s.substr(,);
  13. //获得字符串s中从第零位开始长度为5的字符串(默认时长度为刚好开始位置到结尾)
  14.  
  15. push_back(x);//在末尾添加x
  16. insert(pos,x);//在pos插入x,自然支持整个char数组的一次插入
  17. erase(pos,x);//从pos开始删除x个
  18. copy(pos,len,x);//从pos开始到pos+len为止用x代替
  19. replace(pos,x);//从pos开始换成x
  20. substr(pos,x);//提取pos开始x个
  21. at(x)/[x];//访问第x个元素

支持用数组下标访问,不需要使用迭代器(迭代器会超时)

技巧:有时翻转操作可以维护一个反方向的rope

声明
rope<char> str;

哈希表

  1. #include<ext/pb_ds/assoc_container.hpp>
  2. #include<ext/pb_ds/hash_policy.hpp>
  3. usingnamespace __gnu_pbds;
  4.  
  5. cc_hash_table<string,int>mp1;//拉链法
  6. gp_hash_table<string,int>mp2;//查探法(快一些)

不使用C++11的时候比map的效率大大提高

ACM-ICPC 中可能会使用到的库的更多相关文章

  1. Java in ACM/ICPC

    目录 Java在ACM/ICPC中的特点 在ACM/ICPC中使用Java需要注意的问题 Java与高精度计算 1.Java在ACM/ICPC中的特点 Java的语法和C++几乎相同 Java在执行计 ...

  2. 【转】lonekight@xmu·ACM/ICPC 回忆录

    转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...

  3. 【转】ACM/ICPC生涯总结暨退役宣言—alpc55

    转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...

  4. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  5. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  6. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. ACM - ICPC World Finals 2013 C Surely You Congest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...

  8. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  9. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

随机推荐

  1. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1

    Despite the fact that Cypress is an application that runs natively on your machine, you can install ...

  2. weex 项目开发(五)自定义 过滤函数 和 混合 及 自定义 Header 组件

    1.自定义  过滤函数 src / filters / index.js /** * 自定义 过滤函数 */ export function host (url) { if (!url) return ...

  3. XMLHttpRequest是什么、如何完整地运行一次GET请求、如何检測错误。

    var xmlhttp; function LoadXmlDoc(url){ xmlhttp = null; if(window.XMLHttpRequest){ //code for all new ...

  4. Mac 下配置 Cocos2d-x 3-x android 的环境

    本人初学Cocos2d 3-x,环境配置,搭建android环境弄了好长时间,走了不少弯路,翻阅了好多人的博客和文档,包括官方文档讲的似乎有些似懂非懂,好多依然是旧的版本,所以把我的整个过程梳理一下. ...

  5. UIAutomation使用測试入门

    自己主动化測试的优点: 1.自己主动化能够自己主动測试,不须要人的干预.同一时候还能够不断地反复某一个动作. 2.自己主动化測试在添加了新的功能之后.还能够回归到原理的功能,使其原来的功能不会受到影响 ...

  6. Mmseg中文分词算法解析

    Mmseg中文分词算法解析 @author linjiexing 开发中文搜索和中文词库语义自己主动识别的时候,我採用都是基于mmseg中文分词算法开发的Jcseg开源project.使用场景涉及搜索 ...

  7. HDU 1398 Square Coins(母函数或dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. Win8下怎样安装Win7 or Win7下怎样安装win8?

    预计非常多人可能会用U盘安装工具去去做双系统的安装(Win8下安装Win7, Win7下安装Win8).可是在安装过程中你 会发现一个问题:win7下安装win8,提示你mbr硬盘格式不能安装win8 ...

  9. ajax的异步操作及页面重定向跳转

    今天主要分享一个简单的ajax的异步操作数据,用javascript也有一段时间了,刚开始看到一些页面在没有页面刷新的情况下就可以实现数据的保存或者获取,觉得挺不可思议的,感觉速度很快,做了几个项目之 ...

  10. MFC 的 Picture Control 加载 BMP/PNG 图片的方法

    1. 加载 BMP CStatic* pWnd = (CStatic*)GetDlgItem(IDC_PIC); // 得到 Picture Control 句柄 pWnd->ModifySty ...