Advertisement
Guest User

Untitled

a guest
Jul 5th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. class treap{
  2. private:
  3. struct node{
  4. int val, prior, sz;
  5.  
  6. int l;
  7. int r;
  8.  
  9. node(int val){
  10. this -> val = val;
  11. this -> prior = gen() % (int)1e9;
  12. this -> l = -1;
  13. this -> r = -1;
  14. this -> sz = 1;
  15. }
  16. };
  17.  
  18. int root = -1;
  19.  
  20. vector < node > t;
  21.  
  22. void update(int pos){
  23. if (pos == -1)return;
  24. t[pos].sz = 1;
  25. if (t[pos].l != -1)t[pos].sz += t[t[pos].l].sz;
  26. if (t[pos].r != -1)t[pos].sz += t[t[pos].r].sz;
  27. }
  28.  
  29. pair < int, int > split_val(int pos, int val){
  30. if (pos == -1)return {-1, -1};
  31. if (t[pos].val <= val){
  32. pair < int, int > p = split_val(t[pos].r, val);
  33. t[pos].r = p.F;
  34. update(pos);
  35. return {pos, p.S};
  36. }else{
  37. pair < int, int > p = split_val(t[pos].l, val);
  38. t[pos].l = p.S;
  39. update(pos);
  40. return {p.F, pos};
  41. }
  42. }
  43.  
  44. int merg(int pos_1, int pos_2){
  45. if (pos_1 == -1)return pos_2;
  46. if (pos_2 == -1)return pos_1;
  47. if (t[pos_1].prior > t[pos_2].prior){
  48. t[pos_1].r = merg(t[pos_1].r, pos_2);
  49. update(pos_1);
  50. return pos_1;
  51. }else{
  52. t[pos_2].l = merg(pos_1, t[pos_2].l);
  53. update(pos_2);
  54. return pos_2;
  55. }
  56. }
  57.  
  58. bool find_val(int pos, int val){
  59. if (pos == -1)return 0;
  60. if (t[pos].val == val)return 1;
  61. if (t[pos].val > val)return find_val(t[pos].l, val);
  62. else return find_val(t[pos].r, val);
  63. }
  64.  
  65. int insert_val(int pos, int new_pos){
  66. if (pos == -1)return new_pos;
  67. if (t[pos].prior > t[new_pos].prior){
  68. if (t[pos].val > t[new_pos].val) t[pos].l = insert_val(t[pos].l, new_pos);
  69. else t[pos].r = insert_val(t[pos].r, new_pos);
  70. update(pos);
  71. return pos;
  72. }else{
  73. pair < int, int > p = split_val(pos, t[new_pos].val);
  74. t[new_pos].l = p.F;
  75. t[new_pos].r = p.S;
  76. update(new_pos);
  77. return new_pos;
  78. }
  79. }
  80.  
  81. int eras(int pos, int val){
  82. if (t[pos].val == val) pos = merg(t[pos].l, t[pos].r);
  83. else if (t[pos].val < val) t[pos].r = eras(t[pos].r, val);
  84. else t[pos].l = eras(t[pos].l, val);
  85. update(pos);
  86. return pos;
  87. }
  88.  
  89. int get(int pos, int indx){
  90. if (pos == -1)return -1;
  91. int lft_sz = (t[pos].l == -1 ? 0 : t[t[pos].l].sz);
  92. if (lft_sz == indx)return t[pos].val;
  93. if (indx >= lft_sz) return get(t[pos].r, indx - lft_sz - 1);
  94. else return get(t[pos].l, indx);
  95. }
  96.  
  97. public:
  98.  
  99. void add(int val){
  100. if (find_val(root, val))return;
  101. t.emplace_back(node(val));
  102. if (root == -1){
  103. root = t.size() - 1;
  104. return;
  105. }
  106. root = insert_val(root, t.size() - 1);
  107. }
  108.  
  109. void remov(int val){
  110. (t[root].sz == 1 ? root = -1 : root = eras(root, val));
  111. }
  112.  
  113. int get(int k){
  114. return get(root, k);
  115. }
  116. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement