Advertisement
999ms

Untitled

Mar 25th, 2020
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define forn(i, n) for(int i = 0; i < int(n); ++i)
  4.  
  5. using namespace std;
  6.  
  7. using ll = long long;
  8.  
  9. mt19937 rng(111111);
  10.  
  11. struct treap {
  12.   treap* l = nullptr;
  13.   treap* r = nullptr;
  14.   treap* p = nullptr;
  15.     int key;
  16.     ll pr;
  17.     int sz;
  18.     treap(int key) : key(key), pr(rng()), sz(1) {}
  19. };
  20.  
  21.  
  22. treap* arr[300300];
  23.  
  24. int gs(treap* v) {
  25.     return v ? v->sz : 0;
  26. }
  27.  
  28. int getKey(treap* v) {
  29.   return v ? v->key : 0;
  30. }
  31.  
  32. bool isLeft(treap* v, int le) {
  33.   return getKey(v->l) == le;
  34. }
  35.  
  36. void rs(treap* v) {
  37.   if (!v) return;
  38.   v->sz = 1 + gs(v->l) + gs(v->r);
  39.   v->p = nullptr;
  40.   if (v->l) v->l->p = v;
  41.   if (v->r) v->r->p = v;
  42. }
  43.  
  44. treap* merge(treap* l, treap* r) {
  45.     if (!l || !r) {
  46.     rs(l), rs(r);
  47.     return l ? l : r;
  48.     }
  49.   if (l->pr > r->pr) {  
  50.         l->r = merge(l->r, r);
  51.     rs(l);
  52.     return l;
  53.     } else {
  54.     r->l = merge(l, r->l);
  55.     rs(r);
  56.     return r;
  57.   }
  58. }
  59.  
  60. pair<treap*, treap*> split(treap* v, int key) {
  61.   if (!v) return {v, v};
  62.   if (gs(v->l) + 1 <= key) {
  63.     auto [l, r] = split(v->r, key - 1 - gs(v->l));
  64.     v->r = l;
  65.     rs(v);
  66.     rs(r);
  67.     return {v, r};
  68.   } else {
  69.     auto [l, r] = split(v->l, key);
  70.     v->l = r;
  71.     rs(v);
  72.     rs(l);
  73.     return {l, v};
  74.   }
  75. }
  76.  
  77. int ind(int cur) {
  78.   int ans = gs(arr[cur]->l) + 1;  
  79.   while (arr[cur]->p) {
  80.     int p = getKey(arr[cur]->p);
  81.     if (!isLeft(arr[p], cur)) {
  82.       ans += gs(arr[p]->l) + 1;
  83.     }
  84.     cur = p;
  85.   }
  86.   return ans;
  87. }
  88.  
  89. tuple<treap*, treap*, treap*> fff(treap* v, int k) {
  90.   auto [l, x] = split(v, k - 1);
  91.   auto [w, r] = split(x, 1);    
  92.   return {l, w, r};
  93. }
  94.  
  95. int main() {
  96.   ios_base::sync_with_stdio(false);
  97.   cin.tie(nullptr);
  98.   cout.tie(nullptr);
  99.   int n, m, t;
  100.   cin >> n >> m >> t;
  101.   if (t == 2) {
  102.     arr[0] = nullptr;
  103.     treap* root = arr[0];
  104.     for (int i = 1; i <= m; i++) {
  105.       arr[i] = new treap(i);
  106.       root = merge(root, arr[i]);
  107.     }
  108.     for (int i = 0; i < n; i++) {
  109.       int k;
  110.       cin >> k;
  111.       auto [l, v, r] = fff(root, k);
  112.       cout << v->key << ' ';
  113.       root = merge(v, merge(l, r));
  114.     }
  115.     return 0;
  116.   } else {
  117.     arr[0] = nullptr;
  118.     treap* root = arr[0];
  119.     for (int i = 1; i <= m; i++) {
  120.       arr[i] = new treap(i);
  121.       root = merge(root, arr[i]);
  122.     }
  123.     for (int i = 0; i < n; i++) {
  124.       int x;
  125.       cin >> x;
  126.       int index = ind(x);
  127.       int jndex = ind(index);
  128.       if (index == jndex) {
  129.         auto [L, V, R] = fff(root, index);
  130.         root = merge(V, merge(L, R));
  131.         rs(root);
  132.       } else if (index < jndex) {
  133.         auto [P, V, R] = fff(root, jndex);
  134.         auto [L, X, MID] = fff(P, index);
  135.         root = merge(X, merge(L, merge(V, merge(MID, R))));
  136.         rs(root);
  137.       } else {
  138.         auto [P, X, R] = fff(root, index);
  139.         auto [L, V, MID] = fff(P, jndex);
  140.         root = merge(X, merge(L, merge(V, merge(MID, R))));
  141.         rs(root);
  142.       }
  143.       cout << index << ' ';
  144.     }
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement