主要综合借鉴了yyb和马前卒两位大佬的。

  1. //普通平衡树
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <algorithm>
  6. #define R(x) scanf("%d", &x)
  7. #define ri readint()
  8. #define gc getchar()
  9. #define wi(x) printf("%d\n", x)
  10. #define ls(x) T[x].ch[0]
  11. #define rs(x) T[x].ch[1]
  12. using namespace std;
  13.  
  14. int readint() {
  15. int x = , s = , c = gc;
  16. while (c <= ) c = gc;
  17. if (c == '-') s = -, c = gc;
  18. for (; isdigit(c); c = gc)
  19. x = x * + c - ;
  20. return x * s;
  21. }
  22.  
  23. struct node {
  24. int fa;
  25. int ch[];
  26. int val, cnt;
  27. int size;
  28. };
  29. node T[];
  30. int tot, root;
  31.  
  32. int lr(int x) {
  33. return x == rs(T[x].fa);
  34. }
  35.  
  36. void Connect(int son, int fa, int way) {
  37. T[fa].ch[way] = son;
  38. T[son].fa = fa;
  39. }
  40.  
  41. void push_up(int pos) {
  42. T[pos].size = T[ls(pos)].size + T[rs(pos)].size + T[pos].cnt;
  43. }
  44.  
  45. void Rotate(int x) {
  46. int f = T[x].fa, gf = T[f].fa;
  47. int x_f = lr(x), f_gf = lr(f);
  48. Connect(T[x].ch[x_f^], f, x_f);
  49. Connect(f, x, x_f^);
  50. Connect(x, gf, f_gf);
  51. push_up(f);
  52. push_up(x);
  53. }
  54.  
  55. void splay(int pos, int goal) {
  56. for (; T[pos].fa != goal; Rotate(pos)) {
  57. int f = T[pos].fa, gf = T[f].fa;
  58. if (gf != goal) {
  59. lr(f) ^ lr(pos) ? Rotate(pos) : Rotate(f);
  60. }
  61. }
  62. if (goal == ) root = pos;
  63. }
  64.  
  65. void Insert(int x) {
  66. int pos = root, fa = ;
  67. while (pos && T[pos].val != x) {
  68. fa = pos;
  69. pos = T[pos].ch[x > T[pos].val];
  70. }
  71. if (pos) {
  72. T[pos].cnt++;
  73. } else {
  74. pos = ++tot;
  75. if (fa) T[fa].ch[x > T[fa].val] = pos;
  76. T[pos].ch[] = T[pos].ch[] = ;
  77. T[pos].fa = fa;
  78. T[pos].val = x;
  79. T[pos].cnt = T[pos].size = ;
  80. }
  81. splay(pos, );
  82. }
  83.  
  84. int find(int x) {//数字val为x的位置
  85. int pos = root;
  86. if (!pos) return ;
  87. while (T[pos].ch[x > T[pos].val] && x != T[pos].val)
  88. pos = T[pos].ch[x > T[pos].val];
  89. splay(pos, );
  90. return pos;
  91. }
  92.  
  93. int Next(int flag, int x) {//0为前驱,1为后继
  94. find(x);
  95. int pos = root;
  96. if (T[pos].val > x && flag) return pos;
  97. if (T[pos].val < x && !flag) return pos;
  98. pos = T[pos].ch[flag];
  99. while (T[pos].ch[flag^]) {
  100. pos = T[pos].ch[flag^];
  101. }
  102. return pos;
  103. }
  104.  
  105. void Delet(int x) {//删除数字x
  106. int pos = find(x);
  107. if (!pos) return;
  108. if (T[pos].cnt > ) {
  109. T[pos].cnt--;
  110. T[pos].size--;
  111. return;
  112. } else {
  113. if (!ls(pos) && !rs(pos)) {
  114. root = ;
  115. return;
  116. } else if (ls(pos) && rs(pos)) {
  117. int u = ls(pos);
  118. while (rs(u)) u = rs(u);
  119. splay(u, );
  120. Connect(rs(pos), u, );
  121. push_up(u);
  122. } else {
  123. if (ls(pos)) root = ls(pos);
  124. else root = rs(pos);
  125. T[root].fa = ;
  126. }
  127. }
  128. }
  129.  
  130. int Rank(int x) {//数字x的排名
  131. find(x);
  132. return T[ls(root)].size + ;
  133. }
  134.  
  135. int Kth(int x) {//第x大的数
  136. int pos = root;
  137. if (T[pos].size < x) return -;
  138. while (true) {
  139. int lson = ls(pos);
  140. if (x > T[lson].size + T[pos].cnt) {
  141. x -= T[lson].size + T[pos].cnt;
  142. pos = rs(pos);
  143. } else {
  144. if (x > T[lson].size) return T[pos].val;//这个题返回的是值
  145. pos = lson;
  146. }
  147. }
  148. }
  149.  
  150. int main() {
  151. int n;
  152. for (n = ri; n; n--) {
  153. int op = ri, x = ri;
  154. if (op == ) Insert(x);
  155. else if (op == ) Delet(x);
  156. else if (op == ) wi(Rank(x));
  157. else if (op == ) wi(Kth(x));
  158. else wi(T[Next(op - , x)].val);
  159. }
  160. return ;
  161. }
  1. //文艺平衡树
  2. #include <cstdio>
  3. #include <cctype>
  4. #include <iostream>
  5. #include <algorithm>
  6. #define ri readint()
  7. #define gc getchar()
  8. #define init(a, b) memset(a, b, sizeof(a))
  9. #define rep(i, a, b) for (int i = a; i <= b; i++)
  10. #define irep(i, a, b) for (int i = a; i >= b; i--)
  11. #define ls(x) T[x].ch[0]
  12. #define rs(x) T[x].ch[1]
  13. using namespace std;
  14.  
  15. typedef double db;
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. typedef pair<int, int> P;
  19. const int inf = 0x3f3f3f3f;
  20. const ll INF = 1e18;
  21.  
  22. inline int readint() {
  23. int x = , s = , c = gc;
  24. while (!isdigit(c)) c = gc;
  25. if (c == '-') s = -, c = gc;
  26. for (; isdigit(c); c = gc)
  27. x = x * + c - ;
  28. return x * s;
  29. }
  30.  
  31. const int maxn = 1e5 + ;
  32. int n, m, root, tot;
  33. struct node {
  34. int fa, ch[];
  35. int val, size, tag;
  36. };
  37. node T[maxn];
  38.  
  39. int lr(int x) {
  40. return x == T[T[x].fa].ch[];
  41. }
  42.  
  43. void connect(int son, int fa, int way) {
  44. T[son].fa = fa;
  45. T[fa].ch[way] = son;
  46. }
  47.  
  48. void push_down(int pos) {
  49. if (T[pos].tag) {
  50. T[ls(pos)].tag ^= ;
  51. T[rs(pos)].tag ^= ;
  52. T[pos].tag = ;
  53. swap(ls(pos), rs(pos));
  54. }
  55. }
  56.  
  57. void push_up(int pos) {
  58. T[pos].size = T[ls(pos)].size + T[rs(pos)].size + ;
  59. }
  60.  
  61. void rotate(int pos) {
  62. int f = T[pos].fa, gf = T[f].fa;
  63. int x_f = lr(pos), f_gf = lr(f);
  64. connect(T[pos].ch[x_f^], f, x_f);
  65. connect(f, pos, x_f^);
  66. connect(pos, gf, f_gf);
  67. push_up(f);
  68. push_up(pos);
  69. }
  70.  
  71. void splay(int pos, int goal) {
  72. for (; T[pos].fa != goal; rotate(pos)) {
  73. int f = T[pos].fa, gf = T[f].fa;
  74. if (gf != goal)
  75. lr(pos) ^ lr(f) ? rotate(pos) : rotate(f);
  76. }
  77. if (!goal) root = pos;
  78. }
  79.  
  80. void insert(int x) {
  81. int pos = root, fa = ;
  82. while (pos && T[pos].val != x) {
  83. fa = pos;
  84. pos = T[pos].ch[x > T[pos].val];
  85. }
  86. pos = ++tot;
  87. if (fa) T[fa].ch[x > T[fa].val] = pos;
  88. T[pos].fa = fa;
  89. T[pos].ch[] = T[pos].ch[] = ;
  90. T[pos].val = x;
  91. T[pos].size = ;
  92. T[pos].tag = ;
  93. splay(pos, );
  94. }
  95.  
  96. int Kth(int x) {
  97. int pos = root;
  98. while (true) {
  99. push_down(pos);
  100. int lson = ls(pos);
  101. if (x > T[lson].size + ) {
  102. x -= T[lson].size + ;
  103. pos = rs(pos);
  104. } else {
  105. if (x > T[lson].size) return pos;
  106. pos = lson;
  107. }
  108. }
  109. }
  110.  
  111. void work(int l, int r) {
  112. l = Kth(l), r = Kth(r + );
  113. splay(l, ), splay(r, l);
  114. int pos = rs(root);
  115. pos = ls(pos);
  116. T[pos].tag ^= ;
  117. }
  118.  
  119. void dfs(int cur) {
  120. push_down(cur);
  121. if (T[cur].ch[]) dfs(T[cur].ch[]);
  122. if (T[cur].val != -inf && T[cur].val != inf)
  123. printf("%d ", T[cur].val);
  124. if (T[cur].ch[]) dfs(T[cur].ch[]);
  125. }
  126.  
  127. int main() {
  128. n = ri, m = ri;
  129. insert(inf), insert(-inf);
  130. rep(i, , n) insert(i);
  131. while (m--) {
  132. int l = ri, r = ri;
  133. work(l, r);
  134. }
  135. dfs(root);
  136. return ;
  137. }

普通平衡树与文艺平衡树的splay代码的更多相关文章

  1. [bzoj3224]普通平衡树/3223文艺平衡树

    这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...

  2. 【Splay】bzoj3223-Tyvj1729文艺平衡树

    一.题目 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 ...

  3. bzoj3223 文艺平衡树 (treap or splay分裂+合并)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 3313  Solved: 1883 [Submit][S ...

  4. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

  5. bzoj 3223: Tyvj 1729 文艺平衡树 (splay)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...

  6. 洛谷 P3391 【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  7. BZOJ3223 文艺平衡树(splay)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  8. 洛谷 P3391【模板】文艺平衡树(Splay)

    题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  9. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

随机推荐

  1. 人生苦短之Python迭代器

     迭代 在Python中,如果给定一个list或者touple,我们可以通过for循环来遍历,将值依次取出,这种遍历称为迭代. 在Python中是通过for...in..来进行遍历的,在Java中则是 ...

  2. IPFS 到底是怎么工作的?

    简介 我们知道,一个存储服务,最基本的功能就是存和取.IPFS 中提供了这两种语义,那就是 add 和 get 操作. 在 IPFS 系统中执行 add 操作,就是执行了一次存操作,放在网络的概念里, ...

  3. 简单封装微信小程序

    一.不同环境配置封装 新建config文件夹,根据自己有不同环境设置不同的js文件 具体js文件内容: exports.config = { requestHost: 'https://******. ...

  4. reactjs的一些笔记

    1.使用虚拟DOM作为其不同的实现.同时可以由服务器node.js渲染,从而不需要过重的浏览器DOM支持.   2.虚拟DOM:在浏览器端用javascript实现了一套DOM API.用react开 ...

  5. 字面量(literal)与 C 语言复合字面量(compound literals)

    在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(notation)(字面量是相对变量常量等定义的,无论是常量还是变量,其值在某一时刻总是确定的,只是变量可以反复赋值.刷新 ...

  6. Learning Android ActionBar

    PART1:Make Android2.1 Support ActionBar Last evening I learnt how to make android2.1 support actionb ...

  7. 使用 @RequestMapping 映射请求

  8. bzoj 2655 calc —— 拉格朗日插值

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2655 先设 f[i][j] 表示长度为 i 的序列,范围是 1~j 的答案: 则 f[i][ ...

  9. bzoj1013高斯消元

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1013 似乎是很明显的高斯消元: 第一次写高斯消元. 代码如下: #include<io ...

  10. 蓝桥杯 2014本科C++ B组 李白打酒 三种实现方法 枚举/递归

    标题:李白打酒 话说大诗人李白,一生好饮.幸好他从不开车. 一天,他提着酒壶,从家里出来,酒壶中有酒2斗.他边走边唱: 无事街上走,提壶去打酒. 逢店加一倍,遇花喝一斗. 这一路上,他一共遇到店5次, ...