Advertisement
ivnikkk

Untitled

Nov 5th, 2022
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("unroll-loops")
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include "bits/stdc++.h"
  5. using namespace std;
  6. #define all(a) a.begin(), a.end()
  7. #define int long long
  8. using namespace std;
  9. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  10. struct Node {
  11.     int x, y;
  12.     int siz = 0;
  13.     Node* l, * r;
  14.     Node(int _x) : x(_x) {
  15.         y = rnd();
  16.         siz = 1;
  17.         l = nullptr;
  18.         r = nullptr;
  19.     }
  20. };
  21. int siz(Node* t) {
  22.     if (t == nullptr) {
  23.         return 0;
  24.     }
  25.     return t->siz;
  26. }
  27. int calc(Node* a) {
  28.     int res = 0;
  29.     if (a == nullptr)return res;
  30.     if (a->l != nullptr) {
  31.         res += (a->l)->siz;
  32.     }
  33.     if (a->r != nullptr) {
  34.         res += (a->r)->siz;
  35.     }
  36.     return res;
  37. }
  38. void upd(Node* a) {
  39.     if (a == nullptr)return;
  40.     a->siz = 1;
  41.     a->siz += calc(a);
  42. }
  43. typedef pair<Node*, Node*> Tree;
  44.  
  45. Tree split(Node* a, int x) {
  46.     if (a == nullptr) return { nullptr, nullptr };
  47.     if (a->x < x) {
  48.         Tree p = split(a->r, x);
  49.         a->r = p.first;
  50.         upd(a);
  51.         return { a, p.second };
  52.     }
  53.     else {
  54.         Tree p = split(a->l, x);
  55.         a->l = p.second;
  56.         upd(a);
  57.         return { p.first,a };
  58.     }
  59. }
  60.  
  61. Node* merge(Node* a, Node* b) {
  62.     if (a == nullptr)return b;
  63.     if (b == nullptr)return a;
  64.     if (a->y > b->y) {
  65.         a->r = merge(a->r, b);
  66.         upd(a);
  67.         return a;
  68.     }
  69.     else {
  70.         b->l = merge(a, b->l);
  71.         upd(b);
  72.         return b;
  73.     }
  74. }
  75.  
  76. Node* insert(Node* a, Node* x) {
  77.     if (!a) return x;
  78.     if (!x) return a;
  79.     if (x->y > a->y) {
  80.         Tree p = split(a, x->x);
  81.         x->l = p.first;
  82.         x->r = p.second;
  83.         upd(x);
  84.         return x;
  85.     }
  86.     if (x->x < a->x) {
  87.         a->l = insert(a->l, x);
  88.         upd(a);
  89.         return a;
  90.     }
  91.     else {
  92.         a->r = insert(a->r, x);
  93.         upd(a);
  94.         return a;
  95.     }
  96. }
  97.  
  98. Node* erase(Node* a, int x) {
  99.     if (a == nullptr)return nullptr;
  100.     Tree p1 = split(a, x);
  101.     Tree p2 = split(p1.second, x + 1);
  102.     return merge(p1.first, p2.second);
  103. }
  104. int get_k_stat(Node* a, int k) {
  105.     int res_r = 0;
  106.     if (a->r != nullptr) {
  107.         res_r += (a->r)->siz;
  108.     }
  109.     if (k == res_r + 1) {
  110.         return a->x;
  111.     }
  112.     else if (res_r + 1 > k) return get_k_stat(a->r, k);
  113.     else return get_k_stat(a->l, k - res_r - 1);
  114. }
  115.  
  116. signed main() {
  117. #ifdef _DEBUG
  118.     freopen("input.txt", "r", stdin);
  119.     freopen("output.txt", "w", stdout);
  120. #endif
  121.     ios_base::sync_with_stdio(false);
  122.     cin.tie(nullptr);
  123.     int n, m; cin >> n >> m;
  124.     if(m <= 1e6 + 1) {
  125.         Node* T = nullptr;
  126.         for (int i = 1; i <= m; i++) {
  127.             T = insert(T, new Node(i));
  128.         }
  129.         for (int i = 0; i < n; i++) {
  130.             int l, r; cin >> l >> r;
  131.             l--, r--;
  132.             vector<int> del;
  133.             int sz = siz(T);
  134.             for (int j = l; j <= r; j += 2) {
  135.                 del.push_back(get_k_stat(T, sz - j));
  136.             }
  137.             cout << del[0] << ' ' << del.back() << '\n';
  138.             for (int& i : del) {
  139.                 T = erase(T, i);
  140.             }
  141.         }
  142.     }
  143.     else if (n <= 10000) {
  144.         vector<pair<int, int>> a;
  145.         auto rotate = [&](int pref) {
  146.             for (int i = (int)a.size() - 1; i >= 0; i--) {
  147.                 if (pref < a[i].first) {
  148.                     continue;
  149.                 }
  150.                 pref += min((a[i].second - a[i].first) / 2, pref - a[i].first) + 1;
  151.             }
  152.             return pref;
  153.         };
  154.         for (int i = 0; i < n; i++) {
  155.             int l, r; cin >> l >> r;
  156.             cout << rotate(l) << ' ' << rotate(r) << '\n';
  157.             a.push_back({l, r});
  158.         }
  159.     }
  160.     else {
  161.         return 132;
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement