Advertisement
GerONSo

Untitled

Jun 9th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. //#define pragma
  2.  
  3. #ifdef pragma
  4. #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
  5. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6. #endif // pragma
  7.  
  8. #include<bits/stdc++.h>
  9. #include <ext/pb_ds/assoc_container.hpp>
  10. #include <ext/pb_ds/tree_policy.hpp>
  11.  
  12. #define ll long long
  13. #define all(x) begin(x), end(x)
  14. #define pb push_back
  15. #define x first
  16. #define y second
  17. //#define int long long
  18. #define zero(two) memset(two, 0, sizeof(two))
  19.  
  20. using namespace std;
  21. using namespace __gnu_pbds;
  22.  
  23.  
  24. typedef vector<int> vi;
  25. typedef vector<bool> vb;
  26. typedef pair<int, int> pii;
  27. typedef long double ld;
  28. typedef vector<vi> matrix;
  29. template<typename T>
  30. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. const ld PI = atan2(0, -1);
  33.  
  34. void seriy() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. // cout << fixed << setprecision(10);
  39. #if _offline
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42. #else
  43. freopen("next.in", "r", stdin);
  44. freopen("next.out", "w", stdout);
  45. #endif
  46. }
  47.  
  48. const int MAXN = 2e5 + 10;
  49. const int INF = 2e9 + 7;
  50. const int MAXLOG = 20;
  51. const int MOD = 1e9;
  52. const int BASE = 47;
  53. const int MAX = (1ll << 32) - 1;
  54.  
  55. struct treap {
  56. int x, y, sz = 1, num;
  57. treap *left = nullptr;
  58. treap *right = nullptr;
  59.  
  60. treap(int x1, int y1, int num1) {
  61. x = x1;
  62. y = y1;
  63. num = num1;
  64. }
  65. };
  66.  
  67. treap *root;
  68. vi parent;
  69. vector<treap*> nodes;
  70. mt19937 rr(47);
  71.  
  72. void add(treap *cur, treap *v) {
  73. if(cur->y < v->y) {
  74. if(cur->right != nullptr) {
  75. v->left = cur->right;
  76. parent[v->left->num] = v->num;
  77. }
  78. cur->right = v;
  79. parent[v->num] = cur->num;
  80. return;
  81. }
  82. else {
  83. if(parent[cur->num] == -1) {
  84. v->left = cur;
  85. parent[cur->num] = v->num;
  86. return;
  87. }
  88. else {
  89. add(nodes[parent[cur->num]], v);
  90. }
  91. }
  92. }
  93.  
  94. void update(treap *a) {
  95. int sz1 = ((a->left == nullptr) ? 0 : a->left->sz);
  96. int sz2 = ((a->right == nullptr) ? 0 : a->right->sz);
  97. a->sz = sz1 + sz2 + 1;
  98. }
  99.  
  100. treap *unite(treap *a, treap *b) {
  101. if(a == nullptr) return b;
  102. if(b == nullptr) return a;
  103. if(a->y > b->y) {
  104. a->right = unite(a->right, b);
  105. update(a);
  106. return a;
  107. }
  108. else {
  109. b->left = unite(a, b->left);
  110. update(b);
  111. return b;
  112. }
  113. }
  114.  
  115. pair<treap*, treap*> split(treap *a, int k) {
  116. if(a == nullptr) return {nullptr, nullptr};
  117. int l = ((a->left == nullptr) ? -1 : a->left->sz);
  118. if(k <= l) {
  119. pair<treap*, treap*> kek = split(a->left, k);
  120. a->left = kek.y;
  121. update(a);
  122. return {kek.x, a};
  123. }
  124. else {
  125. pair<treap*, treap*> kek = split(a->right, k - l - 1);
  126. a->right = kek.x;
  127. update(a);
  128. return {a, kek.y};
  129. }
  130. }
  131.  
  132. treap *get_parent(treap *a) {
  133. if(parent[a->num] == -1) {
  134. return a;
  135. }
  136. else {
  137. return get_parent(nodes[parent[a->num]]);
  138. }
  139. }
  140.  
  141. void build(int n) {
  142. parent.resize(n, -1);
  143. root = new treap(1, rr(), 0);
  144. nodes.pb(root);
  145. for(int i = 2; i <= n; i++) {
  146. nodes.pb(new treap(i, rr(), i - 1));
  147. }
  148. treap *now = nodes[0];
  149. for(int i = 1; i < n; i++) {
  150. add(now, nodes[i]);
  151. now = nodes[i];
  152. }
  153. root = get_parent(root);
  154. }
  155.  
  156. void change(int l, int r) {
  157. pair<treap*, treap*> kek = split(root, l);
  158. pair<treap*, treap*> lol = split(kek.y, r - l + 1);
  159. lol.x = unite(lol.x, kek.x);
  160. root = unite(kek.x, lol.y);
  161. }
  162.  
  163. void print(treap *a) {
  164. // cerr << 1 << '\n';
  165. if(a == nullptr) return;
  166. print(a->left);
  167. cout << a->x << " ";
  168. print(a->right);
  169. }
  170.  
  171. signed main() {
  172. seriy();
  173. int n, m;
  174. cin >> n >> m;
  175. build(n);
  176. while(m--) {
  177. int l, r;
  178. cin >> l >> r;
  179. // change(l, r);
  180. }
  181. pair<treap*, treap*> kek = split(root, 3);
  182. print(kek.y);
  183. // print(root);
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement