首先给出一论文讲的很好:

http://www.docin.com/p-63165342.html

http://www.docin.com/p-62465596.html

然后给出模板胡浩大神的模板:http://www.notonlysuccess.com/index.php/splay-tree/

好像胡浩大神的没有给注释,然后给出cxlove的,给出了详细的注释:

http://blog.csdn.net/acm_cxlove/article/details/7790895

然后给出模板题目:

营业额统计

题意:中文..

思路:
每输入一个值,我们就更新splay树,然后找他的左子树中最靠右的,右子树中最靠左的那肯定和他的距离差值是最小的。然后求和即可。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <functional>
  10. #include <numeric>
  11. #include <sstream>
  12. #include <stack>
  13. #include <map>
  14. #include <queue>
  15.  
  16. #define CL(arr, val) memset(arr, val, sizeof(arr))
  17.  
  18. #define lc l,m,rt<<1
  19. #define rc m + 1,r,rt<<1|1
  20. #define pi acos(-1.0)
  21. #define ll long long
  22. #define L(x) (x) << 1
  23. #define R(x) (x) << 1 | 1
  24. #define MID(l, r) (l + r) >> 1
  25. #define Min(x, y) (x) < (y) ? (x) : (y)
  26. #define Max(x, y) (x) < (y) ? (y) : (x)
  27. #define E(x) (1 << (x))
  28. #define iabs(x) (x) < 0 ? -(x) : (x)
  29. #define OUT(x) printf("%I64d\n", x)
  30. #define lowbit(x) (x)&(-x)
  31. #define Read() freopen("din.txt", "r", stdin)
  32. #define Write() freopen("dout.txt", "w", stdout);
  33.  
  34. #define M 137
  35. #define N 32777
  36.  
  37. using namespace std;
  38.  
  39. const int inf = 0x7f7f7f7f;
  40. const ll mod = ;
  41.  
  42. struct SplayTree
  43. {
  44. int chd[N][],pre[N],key[N];
  45. int root,tot;
  46. //生成新节点
  47. void newNode(int &r,int fa,int k)
  48. {
  49. r = ++tot;
  50. pre[r] = fa;
  51. key[r] = k;
  52. chd[r][] = chd[r][] = ;
  53. }
  54. //旋转
  55. void Rotate(int x,int kd)
  56. {
  57. int y = pre[x];
  58. chd[y][!kd] = chd[x][kd];
  59. pre[chd[x][kd]] = y;
  60.  
  61. if (pre[y] != ) chd[pre[y]][chd[pre[y]][] == y] = x;
  62. pre[x] = pre[y];
  63. pre[y] = x;
  64. chd[x][kd] = y;
  65. }
  66. //
  67. void Splay(int r,int g)
  68. {
  69. while (pre[r] != g)
  70. {
  71. if (pre[pre[r]] == g) Rotate(r,chd[pre[r]][] == r);
  72. else
  73. {
  74. int y = pre[r];
  75. int kd = chd[pre[y]][] == y;
  76. if (chd[pre[r]][kd] == r)
  77. {
  78. Rotate(r,!kd);
  79. Rotate(r,kd);
  80. }
  81. else
  82. {
  83. Rotate(y,kd);
  84. Rotate(r,kd);
  85. }
  86. }
  87. }
  88. if (g == ) root = r;
  89. }
  90. //插入
  91. int insert(int k)
  92. {
  93. int r = root;
  94. while (chd[r][key[r] < k])
  95. {
  96. if (key[r] == k)
  97. {
  98. Splay(r,);
  99. return ;
  100. }
  101. r = chd[r][key[r] < k];
  102. }
  103. newNode(chd[r][key[r]< k],r,k);
  104. Splay(chd[r][key[r] < k],);
  105. return ;
  106. }
  107. int get_next(int x)
  108. {
  109. int rg = chd[x][];
  110. if (rg == ) return inf;
  111. while (chd[rg][]) rg = chd[rg][];
  112. return key[rg] - key[x];
  113. }
  114. int get_pre(int x)
  115. {
  116. int lt = chd[x][];
  117. if (lt == ) return inf;
  118. while (chd[lt][]) lt = chd[lt][];
  119. return key[x] - key[lt];
  120. }
  121. }spt;
  122.  
  123. int n;
  124. int main()
  125. {
  126. // Read();
  127. int x;
  128. while (~scanf("%d",&n))
  129. {
  130. spt.tot = spt.root = ;
  131. int ans = ;
  132. for (int i = ; i <= n; ++i)
  133. {
  134. scanf("%d",&x);
  135. // cout<< ">>>>" << x << endl;
  136. if (i == )
  137. {
  138. ans += x;
  139. spt.newNode(spt.root,,x);
  140. }
  141. else
  142. {
  143. if (spt.insert(x) == ) continue;
  144. int a = spt.get_next(spt.root);
  145. int b = spt.get_pre(spt.root);
  146. ans += min(a,b);
  147. }
  148. // cout << ans << endl;
  149. }
  150. printf("%d\n",ans);
  151. }
  152. return ;
  153. }

线段树可解决的问题,Splay解决:

pku 3468 A Simple Problem with Integers

成段更新,区间询问

第二个论文里面有提到如何通过Splay的操作来实现。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <functional>
  10. #include <numeric>
  11. #include <sstream>
  12. #include <stack>
  13. #include <map>
  14. #include <queue>
  15.  
  16. #define CL(arr, val) memset(arr, val, sizeof(arr))
  17.  
  18. #define lc l,m,rt<<1
  19. #define rc m + 1,r,rt<<1|1
  20. #define pi acos(-1.0)
  21. #define ll __int64
  22. #define L(x) (x) << 1
  23. #define R(x) (x) << 1 | 1
  24. #define MID(l, r) (l + r) >> 1
  25. #define Min(x, y) (x) < (y) ? (x) : (y)
  26. #define Max(x, y) (x) < (y) ? (y) : (x)
  27. #define E(x) (1 << (x))
  28. #define iabs(x) (x) < 0 ? -(x) : (x)
  29. #define OUT(x) printf("%I64d\n", x)
  30. #define lowbit(x) (x)&(-x)
  31. #define keyTree (chd[chd[root][1]][0])
  32. #define Read() freopen("din.txt", "r", stdin)
  33. #define Write() freopen("dout.txt", "w", stdout);
  34.  
  35. #define M 137
  36. #define N 200007
  37.  
  38. using namespace std;
  39.  
  40. const int inf = 0x7f7f7f7f;
  41. const int mod = ;
  42.  
  43. struct SpayTree
  44. {
  45. public:
  46. int sz[N];
  47. int ss[N],que[N];
  48. int chd[N][],pre[N];
  49. int top1,top2,root;
  50.  
  51. inline void Rotate(int x,int kd)
  52. {
  53. int y = pre[x];
  54. pushdown(y);
  55. pushdown(x);
  56. chd[y][!kd] = chd[x][kd];
  57. pre[chd[x][kd]] = y;
  58. pre[x] = pre[y];
  59.  
  60. if (pre[x]) chd[pre[y]][chd[pre[y]][] == y] = x;
  61. chd[x][kd] = y;
  62. pre[y] = x;
  63. pushup(y);
  64. }
  65. inline void Splay(int x,int goal)
  66. {
  67. pushdown(x);
  68. while (pre[x] != goal)
  69. {
  70. if (pre[pre[x]] == goal)
  71. {
  72. Rotate(x,chd[pre[x]][] == x);
  73. }
  74. else
  75. {
  76. int y = pre[x],z = pre[y];
  77. int kd = (chd[z][] == y);
  78. if (chd[y][kd] == x)
  79. {
  80. Rotate(x,!kd); Rotate(x,kd);
  81. }
  82. else
  83. {
  84. Rotate(y,kd); Rotate(x,kd);
  85. }
  86. }
  87. }
  88. pushup(x);
  89. if (goal == ) root = x;
  90. }
  91. inline void RotateTo(int k,int goal)
  92. {
  93. int x = root;
  94. pushdown(x);
  95. while (sz[chd[x][]] != k)
  96. {
  97. if (k < sz[chd[x][]])
  98. {
  99. x = chd[x][];
  100. }
  101. else
  102. {
  103. k -= (sz[chd[x][]] + );
  104. x = chd[x][];
  105. }
  106. pushdown(x);
  107. }
  108. Splay(x,goal);
  109. }
  110. inline void erase(int x)
  111. {
  112. int fa = pre[x];
  113. int head = , tail = ;
  114. for (que[tail++] = x; head < tail; ++head)
  115. {
  116. ss[top2++] = que[head];
  117. if (chd[que[head]][]) que[tail++] = chd[que[head]][];
  118. if (chd[que[head]][]) que[tail++] = chd[que[head]][];
  119. }
  120. chd[fa][chd[fa][] == x] = ;
  121. pushup(fa);
  122. }
  123.  
  124. /* 以上基本为固定模板 */
  125.  
  126. inline void pushup(int rt)
  127. {
  128. sz[rt] = sz[chd[rt][]] + sz[chd[rt][]] + ;
  129. sum[rt] = add[rt] + val[rt] + sum[chd[rt][]] + sum[chd[rt][]];
  130. }
  131. inline void pushdown(int rt)
  132. {
  133. if (add[rt])
  134. {
  135. val[rt] += add[rt];
  136. add[chd[rt][]] += add[rt];
  137. add[chd[rt][]] += add[rt];
  138. sum[chd[rt][]] += (ll)sz[chd[rt][]]*add[rt];
  139. sum[chd[rt][]] += (ll)sz[chd[rt][]]*add[rt];
  140. add[rt] = ;
  141. }
  142. }
  143. inline void newNode(int &x,int c)
  144. {
  145. if (top2) x = ss[--top2];
  146. else x = ++top1;
  147. chd[x][] = chd[x][] = pre[x] = ;
  148. sz[x] = ;
  149. val[x] = sum[x] = c;
  150. add[x] = ;
  151. }
  152. inline void makeTree(int &x,int l,int r, int f)
  153. {
  154. if (l > r) return ;
  155. int m = (l + r)>>;
  156. newNode(x,num[m]);
  157. makeTree(chd[x][],l,m - ,x);
  158. makeTree(chd[x][],m + ,r,x);
  159. pre[x] = f;
  160. pushup(x);
  161. }
  162. inline void init(int n)
  163. {
  164. chd[][] = chd[][] = pre[] = ;
  165. add[] = sum[] = sz[] = ;
  166.  
  167. root = top1 = ;
  168. newNode(root,-);
  169. // pre[root] = 0;
  170. newNode(chd[root][],-);
  171. pre[top1] = root;
  172. sz[root] = ;
  173.  
  174. for (int i = ; i < n; ++i) scanf("%d",&num[i]);
  175. makeTree(keyTree,,n - ,chd[root][]);
  176. pushup(chd[root][]); pushup(root);
  177. }
  178.  
  179. inline void update()
  180. {
  181. int l,r,c;
  182. scanf("%d%d%d",&l,&r,&c);
  183. RotateTo(l - ,);
  184. RotateTo(r + ,root);
  185. add[keyTree] += c;
  186. sum[keyTree] += (ll)c*sz[keyTree];
  187. }
  188. inline void query()
  189. {
  190. int l,r;
  191. scanf("%d%d",&l,&r);
  192. RotateTo(l - ,);
  193. RotateTo(r + ,root);
  194. printf("%I64d\n",sum[keyTree]);
  195. }
  196.  
  197. ll sum[N];
  198. int add[N];
  199. int val[N];
  200. int num[N];
  201.  
  202. }spt;
  203.  
  204. int main()
  205. {
  206. // Read();
  207. int n,m;
  208. scanf("%d%d",&n,&m);
  209. spt.init(n);
  210. while (m--)
  211. {
  212. char op[];
  213. scanf("%s",op);
  214. if (op[] == 'Q') spt.query();
  215. else spt.update();
  216. }
  217. return ;
  218. }

hdu 1890 Robotic Sort

题意:
给你n个数,然后每次找到第i大的数,放到第i个位置,然后将第i大的数的位置 - 1到i这个区间逆置,求第i大的数的位置

思路:
直接Splay模拟,然后构造两个边界点,旋转达到求逆的效果。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <functional>
  10. #include <numeric>
  11. #include <sstream>
  12. #include <stack>
  13. #include <map>
  14. #include <queue>
  15.  
  16. #define CL(arr, val) memset(arr, val, sizeof(arr))
  17.  
  18. #define lc l,m,rt<<1
  19. #define rc m + 1,r,rt<<1|1
  20. #define pi acos(-1.0)
  21. #define L(x) (x) << 1
  22. #define R(x) (x) << 1 | 1
  23. #define MID(l, r) (l + r) >> 1
  24. #define Min(x, y) (x) < (y) ? (x) : (y)
  25. #define Max(x, y) (x) < (y) ? (y) : (x)
  26. #define E(x) (1 << (x))
  27. #define iabs(x) (x) < 0 ? -(x) : (x)
  28. #define OUT(x) printf("%I64d\n", x)
  29. #define lowbit(x) (x)&(-x)
  30. #define Read() freopen("data.in", "r", stdin)
  31. #define Write() freopen("d.out", "w", stdout)
  32. #define ll unsigned long long
  33. #define keyTree (chd[chd[root][1]][0])
  34.  
  35. #define M 100007
  36. #define N 100017
  37.  
  38. using namespace std;
  39.  
  40. const int inf = 0x7f7f7f7f;
  41. const int mod = ;
  42.  
  43. int n;
  44. struct node
  45. {
  46. int val;
  47. int idx;
  48. }nd[N];
  49.  
  50. int cmp(node a,node b)
  51. {
  52. return a.val < b.val;
  53. }
  54. class SplayTree
  55. {
  56. public:
  57.  
  58. int son[N][],pre[N];
  59. int rt,top;
  60. int sz[N],rev[N];
  61.  
  62. void Link(int x,int y,int c)
  63. {
  64. pre[x] = y; son[y][c] = x;
  65. }
  66. void Rotate(int x,int c)
  67. {
  68. int y = pre[x];
  69. pushdown(y); pushdown(x);
  70. Link(x , pre[y], son[pre[y]][] == y);
  71. Link(son[x][!c],y,c);
  72. Link(y,x,!c);
  73. pushup(y);
  74. }
  75. void Splay(int x,int g)
  76. {
  77. for (pushdown(x); pre[x] != g;)
  78. {
  79. int y = pre[x],cx = son[y][] == x,cy = son[pre[y]][] == y;
  80. if (pre[y] == g) Rotate(x,cx);
  81. else
  82. {
  83. if (cx == cy) Rotate(y,cy);
  84. else Rotate(x,cx);
  85. Rotate(x,cy);
  86. }
  87. }
  88. pushup(x);
  89. if (g == ) rt = x;
  90. }
  91. int RotateTo(int k,int g)
  92. {
  93. int x = rt;
  94. pushdown(x);
  95. while (sz[son[x][]] != k)
  96. {
  97. if (sz[son[x][]] > k) x = son[x][];
  98. else k -= sz[son[x][]] + , x = son[x][];
  99. pushdown(x);
  100. }
  101. Splay(x,g);
  102. return x;
  103. }
  104. void NewNode(int y ,int &x,int k)
  105. {
  106. x = ++top;
  107. pre[x] = y; sz[x] = ; nd[k].idx = x;
  108. rev[x] = son[x][] = son[x][] = ;
  109. }
  110. void pushup(int x)
  111. {
  112. sz[x] = sz[son[x][]] + sz[son[x][]] + ;
  113. }
  114. void Reverse(int x)
  115. {
  116. rev[x] ^= ;
  117. swap(son[x][],son[x][]);
  118. }
  119. void pushdown(int x)
  120. {
  121. if (rev[x])
  122. {
  123. Reverse(son[x][]);
  124. Reverse(son[x][]);
  125. rev[x] = ;
  126. }
  127. }
  128. void makeTree(int l,int r,int &x,int y)
  129. {
  130. if (l > r) return;
  131. int m = (l + r)>>;
  132. NewNode(y,x,m);
  133. makeTree(l,m - ,son[x][],x);
  134. makeTree(m + ,r,son[x][],x);
  135. pushup(x);
  136. }
  137. void init()
  138. {
  139. for (int i = ; i <= n; ++i)
  140. {
  141. scanf("%d",&nd[i].val);
  142. }
  143. NewNode(top = ,rt,);
  144. NewNode(rt,son[rt][],);
  145. makeTree(,n,son[][],);
  146. Splay(,);
  147. }
  148. void solve()
  149. {
  150. for (int i = ; i <= n; ++i)
  151. {
  152. int idx = nd[i].idx;
  153. Splay(idx, );
  154. int ans = sz[son[rt][]];
  155. RotateTo(i - ,);
  156. int y = RotateTo(ans + ,rt);
  157. Reverse(son[y][]);
  158. printf("%d%c",ans,i < n ? ' ': '\n');
  159. }
  160. }
  161. }spt;
  162.  
  163. int main()
  164. {
  165. // Read();
  166. while (~scanf("%d",&n))
  167. {
  168. if (!n) break;
  169. spt.init();
  170. stable_sort(nd + ,nd + + n, cmp);
  171. // for (int i = 1; i <= n; ++i) printf("%d %d\n",nd[i].val,nd[i].idx);
  172. spt.solve();
  173. }
  174. return ;
  175. }

hdu 3436 Queue-jumpers

题意:

给你一个序列1...n 有三种操作:

1.  Top x :Take person x to the front of the queue
2.  Query x: calculate the current position of person x
3.  Rank x: calculate the current person at position x

输出每次的询问

思路:

首先我们关键是点的离散化,然后建立树上的边到数组的点的映射,然后利用Splay模拟这个过程就行。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <string>
  8. #include <set>
  9. #include <functional>
  10. #include <numeric>
  11. #include <sstream>
  12. #include <stack>
  13. #include <map>
  14. #include <queue>
  15.  
  16. #define CL(arr, val) memset(arr, val, sizeof(arr))
  17.  
  18. #define lc l,m,rt<<1
  19. #define rc m + 1,r,rt<<1|1
  20. #define pi acos(-1.0)
  21. #define L(x) (x) << 1
  22. #define R(x) (x) << 1 | 1
  23. #define MID(l, r) (l + r) >> 1
  24. #define Min(x, y) (x) < (y) ? (x) : (y)
  25. #define Max(x, y) (x) < (y) ? (y) : (x)
  26. #define E(x) (1 << (x))
  27. #define iabs(x) (x) < 0 ? -(x) : (x)
  28. #define OUT(x) printf("%I64d\n", x)
  29. #define lowbit(x) (x)&(-x)
  30. #define Read() freopen("data.in", "r", stdin)
  31. #define Write() freopen("d.out", "w", stdout)
  32. #define ll unsigned long long
  33. #define keyTree (chd[chd[root][1]][0])
  34.  
  35. #define M 100007
  36. #define N 300017
  37.  
  38. using namespace std;
  39.  
  40. const int inf = 0x7f7f7f7f;
  41. const int mod = ;
  42.  
  43. int p[N],a[N];
  44. char op[N][];
  45. int s[N],e[N];
  46. int tot;
  47. int n,m;
  48.  
  49. class SplayTree
  50. {
  51. public:
  52. int sz[N];
  53. int key[N],nd[N];
  54. int son[N][],pre[N];
  55. int num[N];
  56. int rt,top;
  57.  
  58. inline void Link(int x,int y,int c)
  59. {
  60. pre[x] = y; son[y][c] = x;
  61. }
  62. inline void Rotate(int x,int c)
  63. {
  64. int y = pre[x];
  65. Link(x,pre[y],son[pre[y]][] == y);
  66. Link(son[x][!c],y,c);
  67. Link(y,x,!c);
  68. pushup(y);
  69. }
  70. inline void Splay(int x,int g)
  71. {
  72. for (; pre[x] != g;)
  73. {
  74. int y = pre[x], cx = son[y][] == x, cy = son[pre[y]][] == y;
  75. if (pre[y] == g) Rotate(x,cx);
  76. else
  77. {
  78. if (cx == cy) Rotate(y,cy);
  79. else Rotate(x,cx);
  80. Rotate(x,cy);
  81. }
  82. }
  83. pushup(x);
  84. if (g == ) rt = x;
  85. }
  86. inline void pushup(int x)
  87. {
  88. sz[x] = sz[son[x][]] + sz[son[x][]] + num[x];
  89. }
  90. inline void NewNode(int &x,int y,int c)
  91. {
  92. x = ++top; pre[x] = y;
  93. son[x][] = son[x][] = ;
  94. sz[x] = num[x] = e[c] - s[c] + ;
  95. key[x] = c; nd[c] = x;//将树上的点与数组的中的建立映射
  96. }
  97. inline void makeTree(int l,int r,int &x,int f)
  98. {
  99. if (l > r) return ;
  100. int m = (l + r)>>;
  101. NewNode(x,f,m);
  102. makeTree(l,m - ,son[x][],x);
  103. makeTree(m + ,r,son[x][],x);
  104. pushup(x);
  105. }
  106. inline void init()
  107. {
  108. son[][] = son[][] = pre[] = ;
  109. sz[] = ; num[] = ; top = ;
  110. key[] = nd[] = ;
  111. makeTree(,tot - ,rt,);
  112. }
  113. inline void Query(int k)
  114. {
  115. int tx = lower_bound(s, s + tot, k) - s;
  116. int x = nd[tx];
  117. Splay(x,);
  118. printf("%d\n",sz[son[rt][]] + );
  119. }
  120. inline int Find(int k,int rt)//查找第k大
  121. {
  122. int t = sz[son[rt][]];
  123. if (k <= t) return Find(k,son[rt][]);
  124. else if (k <= t + num[rt]) return s[key[rt]] + (k - t) - ;
  125. else return Find(k - (t + num[rt]),son[rt][]);
  126. }
  127. inline int get_min(int x)
  128. {
  129. while (son[x][]) x = son[x][];
  130. return x;
  131. }
  132. inline void Del_R()
  133. {
  134. if (!son[rt][] || !son[rt][])
  135. {
  136. rt = son[rt][] + son[rt][];
  137. pre[rt] = ;
  138. }
  139. else
  140. {
  141. int k = get_min(son[rt][]);//找到右子树中最小的点
  142. Splay(k,rt);//旋转到根节点下边这样保证根节点达到右子树的左子树为空
  143. //合并将根节点删除
  144. son[son[rt][]][] = son[rt][];
  145. rt = son[rt][];
  146. pre[son[rt][]] = rt;
  147. pre[rt] = ;
  148. pushup(rt);
  149. }
  150. }
  151. //不断的往左插入
  152. inline void Insert(int &x,int k,int f)
  153. {
  154. if (x == )
  155. {
  156. NewNode(x,f,k);
  157. return ;
  158. }
  159. Insert(son[x][],k,x);
  160. pushup(x);
  161. }
  162. inline void Top(int k)
  163. {
  164. int tx = lower_bound(s, s + tot, k) - s;
  165. int x = nd[tx];
  166. Splay(x,);
  167. Del_R();
  168. Insert(rt,tx,);
  169. Splay(top,);//这里如果不加会TLE致死。。
  170. }
  171. inline void solve()
  172. {
  173. for (int i = ; i < m; ++i)
  174. {
  175. if (op[i][] == 'T') Top(a[i]);
  176. else if (op[i][] == 'Q') Query(a[i]);
  177. else printf("%d\n",Find(a[i],rt));
  178. }
  179. }
  180. }spt;
  181.  
  182. int main()
  183. {
  184. // Read();
  185. int T,cas = ;
  186. scanf("%d",&T);
  187. while (T--)
  188. {
  189. scanf("%d%d",&n,&m);
  190. //对我们需要询问或者处理的点进行离散化,将不变的区间进行缩点离散
  191. int k = ;
  192. p[k++] = ;
  193. for (int i = ; i < m; ++i)
  194. {
  195. scanf("%s%d",op[i],&a[i]);
  196. if (op[i][] == 'Q' || op[i][] == 'T')
  197. {
  198. p[k++] = a[i];
  199. }
  200. }
  201. p[k++] = n;
  202.  
  203. sort(p,p + k);
  204. tot = ;
  205. s[tot] = p[];
  206. e[tot++] = p[];
  207.  
  208. for (int i = ; i < k; ++i)
  209. {
  210. if (p[i] != p[i - ])
  211. {
  212. if (p[i - ] + < p[i])//将区间缩点
  213. {
  214. s[tot] = p[i - ] + ;
  215. e[tot++] = p[i] - ;
  216. }
  217. s[tot] = p[i];
  218. e[tot++] = p[i];
  219. }
  220. }
  221. printf("Case %d:\n",cas++);
  222. spt.init();
  223. spt.solve();
  224. }
  225. return ;
  226. }

Splay树学习的更多相关文章

  1. 文艺平衡Splay树学习笔记(2)

    本blog会讲一些简单的Splay的应用,包括但不局限于 1. Splay 维护数组下标,支持区间reserve操作,解决区间问题 2. Splay 的启发式合并(按元素多少合并) 3. 线段树+Sp ...

  2. 暑假学习日记:Splay树

    从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...

  3. Splay树再学习

    队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...

  4. BST,Splay平衡树学习笔记

    BST,Splay平衡树学习笔记 1.二叉查找树BST BST是一种二叉树形结构,其特点就在于:每一个非叶子结点的值都大于他的左子树中的任意一个值,并都小于他的右子树中的任意一个值. 2.BST的用处 ...

  5. Splay树-Codevs 1296 营业额统计

    Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...

  6. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

  7. 1439. Battle with You-Know-Who(splay树)

    1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解   网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...

  8. 伸展树(Splay树)的简要操作

    伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...

  9. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

随机推荐

  1. 【BZOJ1720】[Usaco2006 Jan]Corral the Cows 奶牛围栏 双指针法

    [BZOJ1720][Usaco2006 Jan]Corral the Cows 奶牛围栏 Description Farmer John wishes to build a corral for h ...

  2. oracle的分页查询,mabatis的sql配置

    <select id="getCardcaseByPage" resultType="Cardcase" > select * from ( sel ...

  3. rest_framework之解析器详解 05

    解析器就是服务端写api,对于前端用户发来的数据进行解析.解析完之后拿到自己能用数据. 本质就是对请求体中的数据进行解析. django的解析器 post请求过来之后,django 的request. ...

  4. 09.Curator临时节点

        使用Curator也可以简化Ephemeral Node (临时节点)的操作.临时节点驻存在ZooKeeper中,当连接和session断掉时被删除.比如通过ZooKeeper发布服务,服务启 ...

  5. RPM命令详解(安装、升级、卸载)

    rpm 常用命令1.安装一个包 # rpm -ivh 2.升级一个包 # rpm -Uvh 3.卸载一个包 # rpm -e 4.安装参数 --force 即使覆盖属于其它包的文件也强迫安装 --no ...

  6. Python爬虫框架Scrapy实例(一)

    目标任务:爬取腾讯社招信息,需要爬取的内容为:职位名称,职位的详情链接,职位类别,招聘人数,工作地点,发布时间. 一.创建Scrapy项目 scrapy startproject Tencent 命令 ...

  7. uchome 缓存生成

    一.uchome的缓存目录 ---------data此目录要有777权限 (1)模板文件缓存机制 1:在要显示的页面通过include template($name) 语句来包含被编译后的模板文件 ...

  8. python __init__ 构造函数

    实例化过程 会执行__init__ 的函数方法 class SQLHelper: def __init__(self): # self = s1 print("helo") def ...

  9. 在python中使用c语言编写的库

    本文使用的 cffi 官网网址:https://cffi.readthedocs.io/en/latest/overview.html cffi 自己本身使用了pycparser 这个库,是用pyth ...

  10. openSession()与getCurrentSession()的区别

    getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...