ivnikkk

Untitled

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