[关闭]
@wsndy-xx 2018-05-08T22:02:48.000000Z 字数 2410 阅读 1024

Splay 完整代码

纯代码

  1. #include <bits/stdc++.h>
  2. const int N = 1e6 + 10;
  3. int ch[N][3], fa[N], size[N], cnt[N], key[N];
  4. int Size, Root;
  5. #define gc getchar()
  6. inline int read() {
  7. int x = 0, f = 1; char c = gc;
  8. while(c < '0' || c > '9') {if(c == '-') f = -1; c = gc;}
  9. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
  10. return x * f;
  11. }
  12. void Clear(int x) {
  13. ch[x][1] = ch[x][0] = cnt[x] = size[x] = fa[x] = key[x] = 0;
  14. }
  15. void Update(int x) {
  16. if (x) {
  17. size[x]=cnt[x];
  18. if (ch[x][0]) size[x]+=size[ch[x][0]];
  19. if (ch[x][1]) size[x]+=size[ch[x][1]];
  20. }
  21. }
  22. bool Get(int x) {
  23. return ch[fa[x]][1] == x;
  24. }
  25. void Rotate(int x) {
  26. int fax = fa[x], grfa = fa[fax], w = Get(x);
  27. ch[fax][w] = ch[x][w ^ 1];
  28. fa[ch[x][w ^ 1]] = fax;
  29. ch[x][w ^ 1] = fax;
  30. fa[fax] = x;
  31. fa[x] = grfa;
  32. ch[grfa][ch[grfa][1] == fax] = x;
  33. Update(fax);
  34. Update(x);
  35. }
  36. void Splay(int x) {
  37. for(int fax; fax = fa[x]; Rotate(x))
  38. if(fa[fax]) Rotate((Get(x) == Get(fax)) ? fax : x);
  39. Root = x;
  40. }
  41. void Insert(int x) {
  42. if(! Root) {
  43. Size ++;
  44. Root = Size;
  45. ch[Root][1] = ch[Root][0] = fa[Root] = 0;
  46. size[Root] = cnt[Size] = 1;
  47. key[Root] = x;
  48. return ;
  49. }
  50. int now = Root, fanow = 0;
  51. while(1) {
  52. if(x == key[now]) {
  53. cnt[now] ++;
  54. Update(now), Update(fanow);
  55. Splay(now);
  56. return ;
  57. }
  58. fanow = now;
  59. now = ch[now][key[now] < x];
  60. if(!now) {
  61. Size ++;
  62. ch[Size][0] = ch[Size][1] = 0;
  63. fa[Size] = fanow;
  64. ch[fanow][x > key[fanow]] = Size;
  65. size[Size] = cnt[Size] = 1;
  66. key[Size] = x;
  67. Update(fanow);
  68. Splay(Size);
  69. break;
  70. }
  71. }
  72. }
  73. int Pre() {
  74. int now = ch[Root][0];
  75. while(ch[now][1]) now = ch[now][1];
  76. return now;
  77. }
  78. int Nxt() {
  79. int now = ch[Root][1];
  80. while(ch[now][0]) now = ch[now][0];
  81. return now;
  82. }
  83. inline int Find(int x) {
  84. int now = Root, Ans = 0;
  85. while(1) {
  86. if(x < key[now]) now = ch[now][0];
  87. else {
  88. Ans += (ch[now][0] ? size[ch[now][0]] : 0);
  89. if(x == key[now]) {
  90. Splay(now);
  91. return Ans + 1;
  92. }
  93. Ans += cnt[now];
  94. now = ch[now][1];
  95. }
  96. }
  97. }
  98. inline int Findx(int x) {
  99. int now = Root;
  100. while(1) {
  101. if(ch[now][0] && x <= size[ch[now][0]]) now = ch[now][0];
  102. else {
  103. int imp = (ch[now][0] ? size[ch[now][0]] : 0) + cnt[now];
  104. if(x <= imp) return key[now];
  105. x -= imp;
  106. now = ch[now][1];
  107. }
  108. }
  109. }
  110. void Delete(int x) {
  111. int my = Find(x);
  112. if(cnt[Root] > 1) cnt[Root] --, Update(Root);
  113. else if(!ch[Root][1] && !ch[Root][0]) Clear(Root), Root = 0;
  114. else if(!ch[Root][0]) {
  115. int befroot = Root;
  116. Root = ch[Root][1];
  117. fa[Root] = 0;
  118. Clear(befroot);
  119. }
  120. else if(!ch[Root][1]) {
  121. int befroot = Root;
  122. Root = ch[Root][0];
  123. fa[Root] = 0;
  124. Clear(befroot);
  125. }
  126. else {
  127. int left = Pre(), befroot = Root;
  128. Splay(left);
  129. ch[Root][1] = ch[befroot][1];
  130. fa[ch[befroot][1]] = Root;
  131. Clear(befroot);
  132. Update(Root);
  133. }
  134. return ;
  135. }
  136. int main() {
  137. int T = read();
  138. for(; T; T --) {
  139. int opt = read(), x = read();
  140. if(opt == 1) Insert(x);
  141. else if(opt == 2) Delete(x);
  142. else if(opt == 3) std:: cout << Find(x) << "\n";
  143. else if(opt == 4) std:: cout << Findx(x) << "\n";
  144. else if(opt == 5) {
  145. Insert(x);
  146. std:: cout << key[Pre()] << "\n";
  147. Delete(x);
  148. } else {
  149. Insert(x);
  150. std:: cout << key[Nxt()] << "\n";
  151. Delete(x);
  152. }
  153. }
  154. return 0;
  155. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注