Advertisement
prog3r

Запросы сортировки. Поддерживаем лес ДОшек, каждая из которых это отсортированное множество.

Mar 30th, 2025
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. signed main() {
  4.     int n, q;
  5.     cin >> n >> q;
  6.     struct Node {
  7.         int l = -1;
  8.         int r = -1;
  9.         int su = 0;
  10.         int mn = 1e9;
  11.         int mx = -1e9;
  12.     };
  13.     vector<Node> tree;
  14.     auto su = [&] (int u) -> int {
  15.         return (u == -1 ? 0 : tree[u].su);
  16.     };
  17.     auto mn = [&] (int u) -> int {
  18.         return (u == -1 ? 1e9 : tree[u].mn);
  19.     };
  20.     auto mx = [&] (int u) -> int {
  21.         return (u == -1 ? -1e9 : tree[u].mx);
  22.     };
  23.     auto recalc = [&] (int u) -> void {
  24.         if (u == -1) {
  25.             return;
  26.         }
  27.         tree[u] = {tree[u].l, tree[u].r, su(tree[u].l)+su(tree[u].r),
  28.                                          min(mn(tree[u].l), mn(tree[u].r)),
  29.                                          max(mx(tree[u].l), mx(tree[u].r))};
  30.     };
  31.     auto nw = [&] () -> int {
  32.         tree.push_back({});
  33.         return tree.size()-1;
  34.     };
  35.     auto inc = [&] (auto f, int u, int tl, int tr, int pos) -> void {
  36.         if (tl == tr) {
  37.             tree[u].su += 1;
  38.             tree[u].mn = tl;
  39.             tree[u].mx = tl;
  40.             return;
  41.         }
  42.         int tm = (tl + tr) >> 1;
  43.         if (pos <= tm) {
  44.             if (tree[u].l == -1) {
  45.                 tree[u].l = nw();
  46.             }
  47.             f(f, tree[u].l, tl, tm, pos);
  48.         } else {
  49.             if (tree[u].r == -1) {
  50.                 tree[u].r = nw();
  51.             }
  52.             f(f, tree[u].r, tm+1, tr, pos);
  53.         }
  54.         recalc(u);
  55.     };
  56.     auto split = [&] (auto f, int u, int sz) -> int {
  57.         int ret = nw();
  58.         if (!sz) {
  59.             return ret;
  60.         }
  61.         if (su(u) == sz) {
  62.             swap(tree[u], tree[ret]);
  63.             return ret;
  64.         }
  65.         if (tree[u].l == -1 && tree[u].r == -1) {
  66.             tree[ret] = tree[u];
  67.             tree[u].su -= sz;
  68.             tree[ret].su = sz;
  69.             return ret;
  70.         }
  71.         if (su(tree[u].l) >= sz) {
  72.             tree[ret].l = f(f, tree[u].l, sz);
  73.             recalc(u);
  74.             recalc(ret);
  75.             return ret;
  76.         } else {
  77.             sz -= su(tree[u].l);
  78.             swap(tree[ret].l, tree[u].l);
  79.             tree[ret].r = f(f, tree[u].r, sz);
  80.             recalc(u);
  81.             recalc(ret);
  82.             return ret;
  83.         }
  84.     };
  85.     auto merge = [&] (auto f, int& u, int& v) -> void {
  86.         if (su(u) == 0) {
  87.             swap(u, v);
  88.             return;
  89.         }
  90.         if (su(v) == 0) {
  91.             return;
  92.         }
  93.         if (tree[u].l == -1 && tree[u].r == -1) {
  94.             // leaf
  95.             assert(tree[v].l == -1 && tree[v].r == -1);
  96.             tree[u].su += tree[v].su;
  97.             v = -1;
  98.             return;
  99.         }
  100.         // v is never used after
  101.         f(f, tree[u].l, tree[v].l);
  102.         f(f, tree[u].r, tree[v].r);
  103.         v = -1;
  104.         recalc(u);
  105.     };
  106.     int prev = 1e9;
  107.     vector<int> roots(1'000'000);
  108.     vector<int> LL(1'000'000); // starting_point_by_order
  109.     auto comp = [&] (auto const& a, auto const& b) {
  110.         return LL[a] < LL[b];
  111.     };
  112.     set<int, decltype(comp)> st(comp);
  113.     int C = 0;
  114.     for (int i = 0; i < n; i += 1) {
  115.         int x;
  116.         cin >> x;
  117.         if (x < prev) {
  118.             LL[C] = i;
  119.             st.insert(C);
  120.             roots[C] = nw();
  121.             C += 1;
  122.         }
  123.         prev = x;
  124.         inc(inc, roots[C-1], 1, 1e9, x);
  125.     }
  126.     for (int ii = 0; ii < q; ii += 1) {
  127. //        for (const auto &x : st) {
  128. //            cout << x << ": " << LL[x] << ": " << su(roots[x]) << ":D\n";
  129. //        }
  130.         int l, r;
  131.         cin >> l >> r;
  132.         l -= 1;
  133.         r -= 1;
  134.         LL[999'999] = l;
  135.        auto it1 = (--st.upper_bound(999'999));
  136.         int ordl = *it1;
  137.         LL[999'999] = r;
  138.        auto it2 = (--st.upper_bound(999'999));
  139.         int ordr = *it2;
  140.         if (ordl == ordr) {
  141.             cout << "YES\n";
  142.             continue;
  143.         }
  144.         cout << "NO\n";
  145.         int ret = split(split, roots[ordl], l-LL[ordl]);
  146.         swap(ret, roots[ordl]);
  147.         int pref = split(split, roots[ordr], r-LL[ordr]+1);
  148.         merge(merge, ret, pref);
  149.         it1++;
  150.         while (it1 != it2) {
  151.             merge(merge, ret, roots[*it1]);
  152.             auto d = it1;
  153.             it1++;
  154.             st.erase(d);
  155.         }
  156.         if (l == LL[ordl]) {
  157.             st.erase(ordl); // если левый плохой то удалили
  158.         }
  159.         it2++;
  160.         if (r == n-1 || (it2 != st.end() && LL[*it2] == r+1)) {
  161.             // если правый плохой то удалили
  162.             st.erase(ordr);
  163.         } else {
  164.             // иначе подвинули
  165.             st.erase(ordr);
  166.             LL[ordr] = r+1;
  167.             st.insert(ordr);
  168.         }
  169.         assert(su(ret) == r-l+1);
  170.         LL[C] = l;
  171. //        cout << C << "!!\n";
  172.         st.insert(C);
  173.         roots[C] = ret;
  174.         auto ml = st.lower_bound(C);
  175.         if (ml != st.begin()) {
  176.             ml--;
  177.             if (tree[roots[*ml]].mx <= tree[roots[C]].mn) {
  178.                 merge(merge, roots[*ml], roots[C]);
  179.                 st.erase(C);
  180.             }
  181.         }
  182.         auto it = st.upper_bound(C);
  183.         assert(it != st.begin());
  184.         it--;
  185.         auto mr = st.upper_bound(C);
  186.         if (mr != st.end()) {
  187.             if (tree[roots[*it]].mx <= tree[roots[*mr]].mn) {
  188.                 merge(merge, roots[*it], roots[*mr]);
  189.                 st.erase(*mr);
  190.             }
  191.         }
  192.         C += 1;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement