Advertisement
ke_timofeeva7

сенечке

Aug 3rd, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <memory.h>
  7. #include <stdio.h>
  8. #include <vector>
  9. #include <stack>
  10. #include <deque>
  11. #include <queue>
  12. #include <vector>
  13. #include <set>
  14. #include <iterator>
  15. #include <map>
  16. #include <iomanip>
  17. #include <unordered_set>
  18. #define int long long
  19. #define sp system("pause")
  20. #define pb push_back
  21. #define double long double
  22. #define endl "\n"
  23. #define un unsigned
  24. #define INF 1000000009
  25. #define pii pair<int, int>
  26. #define all(v) v.begin(), v.end()
  27. using namespace std;
  28.  
  29. const int MOD = 1000000000;
  30.  
  31. int rnd()
  32. {
  33. return (rand() << 16) | rand();
  34. }
  35.  
  36. struct Node
  37. {
  38. int x, y, size;
  39.  
  40. Node* left = nullptr;
  41. Node* right = nullptr;
  42.  
  43. Node(int x) : x(x), y(rnd()), size(1) {}
  44. Node() : x(0), y(rnd()), size(1) {}
  45. };
  46.  
  47. int get_size(Node* root)
  48. {
  49. if (root == nullptr)
  50. {
  51. return 0;
  52. }
  53.  
  54. return root->size;
  55. }
  56.  
  57. Node* update(Node* root)
  58. {
  59. root->size = get_size(root->left) + 1 + get_size(root->right);
  60.  
  61. return root;
  62. }
  63.  
  64. pair <Node*, Node*> split(Node* root, int pos)
  65. {
  66. if (root == nullptr)
  67. {
  68. return { nullptr, nullptr };
  69. }
  70.  
  71. if (get_size(root->left) + 1 <= pos)
  72. {
  73. auto splitted = split(root->right, pos - get_size(root->left) - 1);
  74. root->right = splitted.first;
  75. root = update(root);
  76. return { root, splitted.second };
  77. }
  78. else
  79. {
  80. auto splitted = split(root->left, pos);
  81. root->left = splitted.second;
  82. root = update(root);
  83. return { splitted.first, root };
  84. }
  85. }
  86.  
  87. Node* merge(Node* a, Node* b)
  88. {
  89. if (a == nullptr)
  90. {
  91. return b;
  92. }
  93. if (b == nullptr)
  94. {
  95. return a;
  96. }
  97.  
  98. if (a->y < b->y)
  99. {
  100. a->right = merge(a->right, b);
  101. a = update(a);
  102. return a;
  103. }
  104. else
  105. {
  106. b->left = merge(a, b->left);
  107. b = update(b);
  108. return b;
  109. }
  110. }
  111.  
  112. Node* find(Node* root, int pos)
  113. {
  114. if (root == nullptr)
  115. {
  116. return nullptr;
  117. }
  118. if (get_size(root->left) + 1 == pos)
  119. {
  120. return root;
  121. }
  122. if (get_size(root->left) + 1 < pos)
  123. {
  124. return find(root->right, pos - get_size(root->left) - 1);
  125. }
  126. else
  127. {
  128. return find(root->left, pos);
  129. }
  130. }
  131.  
  132. Node* insert(Node* root, Node* new_node, int pos)
  133. {
  134. auto splitted = split(root, pos);
  135. root = merge(splitted.first, merge(new_node, splitted.second));
  136. root = update(root);
  137.  
  138. return root;
  139. }
  140.  
  141. void answer(Node* tree, int n)
  142. {
  143. for (int i = 1; i <= n; i++)
  144. {
  145. auto ans = find(tree, i);
  146.  
  147. if (ans != nullptr)
  148. {
  149. cout << ans->x << " ";
  150. }
  151. }
  152.  
  153. cout << endl;
  154. }
  155.  
  156. signed main()
  157. {
  158. ios_base::sync_with_stdio();
  159. cin.tie(0);
  160. cout.tie(0);
  161. cout.precision(20);
  162.  
  163. int n, m;
  164. cin >> n >> m;
  165.  
  166. Node* tree = nullptr;
  167.  
  168. for (int i = 1; i <= n; i++)
  169. {
  170. Node* new_node = new Node(i);
  171. tree = insert(tree, new_node, i - 1);
  172. }
  173.  
  174. //answer(tree, n);
  175.  
  176. for (int i = 0; i < m; i++)
  177. {
  178. int l, r;
  179. cin >> l >> r;
  180.  
  181. auto splitted = split(tree, l - 1);
  182. auto splitted2 = split(splitted.second, r - l + 1);
  183.  
  184. tree = merge(splitted2.first, merge(splitted.first, splitted2.second));
  185.  
  186. //answer(tree, n);
  187. }
  188.  
  189. answer(tree, n);
  190.  
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement