Advertisement
IanisBelu

A. Shandom Ruffle

Dec 10th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | Source Code | 0 0
  1. #ifdef LOCAL
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <random>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <set>
  10. #include <map>
  11. #else
  12. #pragma GCC optimize("Ofast,unroll-loops")
  13. #include <bits/stdc++.h>
  14. #define cerr if (false) cerr
  15. #define endl '\n'
  16. #endif
  17.  
  18. #define fi first
  19. #define se second
  20.  
  21. #define sz(a) ((int)(a).size())
  22. #define all(a) (a).begin(), (a).end()
  23.  
  24. #define lsb(x) (x & (-x))
  25.  
  26. using namespace std;
  27.  
  28. template <typename T>
  29. bool ckmax(T &a, T b) { return a < b ? a = b, true : false; }
  30. template <typename T>
  31. bool ckmin(T &a, T b) { return a > b ? a = b, true : false; }
  32.  
  33. using ll = long long;
  34. using pii = pair<int, int>;
  35.  
  36. struct Treap {
  37.    int val, pr, size;
  38.    Treap *l, *r;
  39.  
  40.    Treap(): val(0), pr(0), size(0), l(nullptr), r(nullptr) { }
  41.    Treap(int _val): val(_val), size(1), l(nullptr), r(nullptr) {
  42.       static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  43.       pr = int(rng());
  44.    }
  45. };
  46.  
  47. ostream &operator<<(ostream &out, Treap *t) {
  48.    function<void(Treap*)> dfs = [&](Treap *t) {
  49.       if (!t) return;
  50.       dfs(t->l);
  51.       out << t->val << ' ';
  52.       dfs(t->r);
  53.    };
  54.    dfs(t);
  55.    return out;
  56. }
  57.  
  58. int size(Treap *a) { return a ? a->size : 0; }
  59.  
  60. Treap *treap_update(Treap *a) {
  61.    if (!a) return nullptr;
  62.    a->size = size(a->l) + size(a->r) + 1;
  63.    return a;
  64. }
  65.  
  66. Treap *join(Treap *a, Treap *b) {
  67.    if (!a || !b) return a ? a : b;
  68.    if (a->pr > b->pr) return a->r = join(a->r, b), treap_update(a);
  69.    return b->l = join(a, b->l), treap_update(b);
  70. }
  71.  
  72. Treap *join(vector<Treap*> treaps) {
  73.    if (treaps.empty()) return nullptr;
  74.    Treap *ret = treaps[0];
  75.    for (int i = 1; i < sz(treaps); i++)
  76.       ret = join(ret, treaps[i]);
  77.    return ret;
  78. }
  79.  
  80. pair<Treap*, Treap*> split(Treap *a, int pos) {
  81.    if (!a) return { nullptr, nullptr };
  82.    if (size(a->l) + 1 <= pos) {
  83.       auto [ l, r ] = split(a->r, pos - size(a->l) - 1);
  84.       a->r = l;
  85.       return { treap_update(a), r };
  86.    }
  87.    auto [ l, r ] = split(a->l, pos);
  88.    a->l = r;
  89.    return { l, treap_update(a) };
  90. }
  91.  
  92. void swap_segments(Treap *treap, int l1, int r1, int l2, int r2) {
  93.    auto [ left1, suff1 ] = split(treap, l1 - 1);
  94.    auto [ mid1, right1 ] = split(suff1, r1 - l1 + 1);
  95.    auto [ left2, suff2 ] = split(right1, l2 - r1 - 1);
  96.    auto [ mid2, right2 ] = split(suff2, r2 - l2 + 1);
  97.  
  98.    join({ left1, mid2, left2, mid1, right2 });
  99. }
  100.  
  101. void solve() {
  102.    int n;
  103.    cin >> n;
  104.  
  105.    Treap *treap = new Treap(1);
  106.    for (int i = 2; i <= n; i++)
  107.       treap = join(treap, new Treap(i));
  108.  
  109.    for (int i = 1, a, b; i <= n; i++) {
  110.       cin >> a >> b;
  111.       if (a >= b) continue;
  112.       int len = min(b - a, n - b + 1);
  113.       swap_segments(treap, a, a + len - 1, b, b + len - 1);
  114.    }
  115.  
  116.    cout << treap << endl;
  117. }
  118.  
  119. signed main() {
  120. #ifdef LOCAL
  121.    freopen("input.txt", "r", stdin);
  122. #endif
  123.    ios_base::sync_with_stdio(false);
  124.    cin.tie(0);
  125.    cout.tie(0);
  126.    
  127.    solve();
  128.  
  129.    return 0;
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement