Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. // randomized cartesian tree
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. mt19937 rng (12345);
  6.  
  7. struct Node;
  8. typedef Node * PNode;
  9. struct Node
  10. {
  11. int x;
  12. int size;
  13. PNode left;
  14. PNode right;
  15.  
  16. Node (int x_)
  17. {
  18. x = x_;
  19. size = 1;
  20. left = right = nullptr;
  21. }
  22. };
  23.  
  24. void recalc (PNode t)
  25. {
  26. if (t == nullptr) return;
  27. t -> size = 1;
  28. if (t -> left != nullptr)
  29. t -> size += t -> left -> size;
  30. if (t -> right != nullptr)
  31. t -> size += t -> right -> size;
  32. }
  33.  
  34. pair <PNode, PNode> tsplit (PNode t, int xs)
  35. {
  36. if (t == nullptr) return {nullptr, nullptr};
  37. if (t -> x < xs)
  38. { // t->left t->x | t->right, xs
  39. // t->left | t->x | temp.first | temp.second
  40. auto temp = tsplit (t -> right, xs);
  41. t -> right = temp.first;
  42. recalc (t);
  43. return {t, temp.second};
  44. }
  45. else
  46. { // t->left, xs | t->x | t->right
  47. // temp.first | temp.second / t->x \ t->right
  48. auto temp = tsplit (t -> left, xs);
  49. t -> left = temp.second;
  50. recalc (t);
  51. return {temp.first, t};
  52. }
  53. }
  54.  
  55. PNode tmerge (PNode l, PNode r)
  56. {
  57. if (l == nullptr) return r;
  58. if (r == nullptr) return l;
  59. // (100) 100/110 (10) 10/110
  60. if (rng () % (l -> size + r -> size) < (unsigned) l -> size)
  61. { // l
  62. // / \ .
  63. // l->left l->right r
  64. l -> right = tmerge (l -> right, r);
  65. recalc (l);
  66. return l;
  67. }
  68. else
  69. { // r
  70. // / \ .
  71. // l r->left r->right
  72. r -> left = tmerge (l, r -> left);
  73. recalc (r);
  74. return r;
  75. }
  76. }
  77.  
  78. PNode tadd (PNode t, int x)
  79. {
  80. auto temp = tsplit (t, x);
  81. auto cur = new Node (x);
  82. // temp.first | cur | temp.second
  83. auto m = tmerge (temp.first, cur);
  84. return tmerge (m, temp.second);
  85. }
  86.  
  87. PNode trem (PNode t, int x)
  88. {
  89. auto temp = tsplit (t, x);
  90. auto temp2 = tsplit (temp.second, x + 1);
  91. // temp.first (< x) | temp2.first (== x) | temp2.second (> x)
  92. return tmerge (temp.first, temp2.second);
  93. }
  94.  
  95. void toutput (PNode t, string history = "")
  96. {
  97. if (t == nullptr) return;
  98. toutput (t -> left, history + " ");
  99. cout << history << "" << t -> x << endl;
  100. toutput (t -> right, history + " ");
  101. }
  102.  
  103. int tfind(PNode t, int k, int cu){
  104. int tmpll = 0, tmprr = 0, tmplr = 0, tmprl = 0;
  105. if(t -> left != nullptr){
  106. if(t -> left -> left != nullptr) tmpll = t -> left -> left -> size;
  107. if(t -> left -> right != nullptr) tmplr = t -> left -> right -> size;
  108. }
  109. if(t -> right != nullptr){
  110. if(t -> right -> left != nullptr) tmprl = t -> right -> left -> size;
  111. if(t -> right -> right != nullptr) tmprr = t -> right -> right -> size;
  112. }
  113. //cout << t -> x << " size " << t->size << " cur " << cu << " tmprl " << tmprl << " tmprr" << tmprr << endl;
  114. if(k - 1 == cu){
  115. return t -> x;
  116. }
  117. if(t -> right != nullptr){
  118. if(cu < k - 1){
  119. tfind(t -> left, k, cu + tmplr + 1);
  120. }
  121. else tfind(t -> right, k, cu - tmprl - 1);
  122. }
  123. else{
  124. tfind(t -> left, k, cu + tmplr + 1);
  125. }
  126. }
  127.  
  128. PNode root;
  129.  
  130. int main ()
  131. {
  132. ios_base::sync_with_stdio (false);
  133. cin.tie (0);
  134. cout.tie (0);
  135.  
  136. root = nullptr;
  137. int n;
  138. cin >> n;
  139. for(int i = 0; i < n; ++i){
  140. int a, b;
  141. cin >> a >> b;
  142. if(a == +1){
  143. root = tadd(root, b);
  144. //toutput(root);
  145. }
  146. if(a == -1){
  147. root = trem(root, b);
  148. }
  149. if(a == 0){
  150. int tmp = 0;
  151. if(root -> right != nullptr){
  152. tmp = root -> right -> size;
  153. }
  154. cout << tfind(root, b, tmp) << endl;
  155. }
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement