IzhanVarsky

Untitled

Apr 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <ctime>
  4. #include <fstream>
  5. #include <vector>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. vector<int> res;
  11.  
  12. struct Node {
  13. int Y, size, data, empty;
  14. Node *Left, *Right;
  15.  
  16. Node(int y, int d, int emp = 0) {
  17. empty = emp;
  18. Y = y;
  19. data = d;
  20. size = 1;
  21. Left = Right = nullptr;
  22. }
  23. };
  24.  
  25. int getSize(Node *treap) {
  26. return treap ? treap->size : 0;
  27. }
  28.  
  29. void updSize(Node *treap) {
  30. if (treap) treap->size = getSize(treap->Left) + getSize(treap->Right) + 1;
  31. }
  32.  
  33. int getEmptyCnt(Node *treap) {
  34. return treap ? treap->empty : 0;
  35. }
  36.  
  37. void updEmptyCnt(Node *treap) {
  38. if (treap)
  39. treap->empty = getEmptyCnt(treap->Left) + getEmptyCnt(treap->Right) + (treap->data == 0 ? 1 : 0);
  40. }
  41.  
  42. void bigUpd(Node *treap) {
  43. updSize(treap);
  44. updEmptyCnt(treap);
  45. }
  46.  
  47. void veryBigUpd(Node *&treap) {
  48. if (treap == nullptr) return;
  49. veryBigUpd(treap->Left);
  50. veryBigUpd(treap->Right);
  51. bigUpd(treap);
  52. }
  53.  
  54. void split(Node *treap, int pos, Node *&lRes, Node *&rRes) {
  55. if (treap == nullptr) {
  56. lRes = rRes = nullptr;
  57. return;
  58. }
  59. int XX = getSize(treap->Left) + 1;
  60. if (pos <= XX) {
  61. split(treap->Left, pos, lRes, treap->Left);
  62. rRes = treap;
  63. } else {
  64. split(treap->Right, pos - XX, treap->Right, rRes);
  65. lRes = treap;
  66. }
  67. bigUpd(treap);
  68. }
  69.  
  70. void merge(Node *&treap, Node *l, Node *r) {
  71. if (!l || !r) {
  72. treap = l ? l : r;
  73. } else if (l->Y > r->Y) {
  74. merge(l->Right, l->Right, r);
  75. treap = l;
  76. } else {
  77. merge(r->Left, l, r->Left);
  78. treap = r;
  79. }
  80. bigUpd(treap);
  81. }
  82.  
  83. void delNextZero(Node *&treap) {
  84. if (treap == nullptr) return;
  85. if (getEmptyCnt(treap->Left) != 0) {
  86. delNextZero(treap->Left);
  87. } else if (treap->data == 0) {
  88. merge(treap, treap->Left, treap->Right);
  89. } else if (getEmptyCnt(treap->Right) != 0) {
  90. delNextZero(treap->Right);
  91. }
  92. bigUpd(treap);
  93. }
  94.  
  95. void printTreap(Node *&treap) {
  96. if (treap == nullptr) return;
  97. printTreap(treap->Left);
  98. // cout << treap->data << " ";
  99. res.push_back(treap->data);
  100. printTreap(treap->Right);
  101. }
  102.  
  103. void insertWithDelNextZero(Node *&treap, int pos, Node *toIns) {
  104. Node *t1, *t2;
  105. split(treap, pos, t1, t2);
  106. // veryBigUpd(t2);
  107. delNextZero(t2);
  108. merge(treap, t1, toIns);
  109. merge(treap, treap, t2);
  110. }
  111.  
  112. int main() {
  113. srand(time(nullptr));
  114. ios_base::sync_with_stdio(false);
  115. cin.tie(nullptr);
  116. cout.tie(nullptr);
  117. Node *treap = nullptr;
  118.  
  119. // for (int j = 0; j < 100; ++j) {
  120. // treap = nullptr;
  121. // int n, m;
  122. // ifstream cin("input.txt");
  123. // cin >> n >> m;
  124. // for (int i = 0; i < m; ++i) {
  125. // merge(treap, treap, new Node(rand(), 0, 1));
  126. // bigUpd(treap);
  127. // }
  128. // for (int i = 1; i <= n; ++i) {
  129. // int x;
  130. // cin >> x;
  131. // insertWithDelNextZero(treap, x, new Node(rand(), i, 0));
  132. //// printTreap(treap);
  133. //// cout << endl;
  134. // }
  135. // printTreap(treap);
  136. // cout << endl;
  137. // }
  138. // cout << endl;
  139.  
  140. int n, m;
  141. // ifstream cin("input.txt");
  142. cin >> n >> m;
  143. for (int i = 0; i < m + 1; ++i) {
  144. merge(treap, treap, new Node(rand(), 0, 1));
  145. bigUpd(treap);
  146. }
  147.  
  148. for (int i = 1; i <= n; ++i) {
  149. int x;
  150. cin >> x;
  151. insertWithDelNextZero(treap, x, new Node(rand(), i, 0));
  152. // veryBigUpd(treap);
  153. // printTreap(treap);
  154. // cout << endl;
  155. }
  156. printTreap(treap);
  157. int j;
  158. for (j = res.size() - 1; j >= 0; j--) {
  159. if (res[j] != 0) break;
  160. }
  161. cout << j + 1 << endl;
  162. for (int i = 0; i < j + 1; ++i) {
  163. cout << res[i] << " ";
  164. }
  165. // cout << getSize(treap) << endl;
  166. // printTreap(treap);
  167. // cout << endl;
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment