This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub maspypy/library
#include "ds/segtree/sortable_segtree.hpp"
#include "ds/fastset.hpp" #include "ds/segtree/segtree.hpp" template <typename Monoid> struct Sortable_SegTree { using MX = Monoid; using X = typename MX::value_type; const int N, KEY_MAX; struct Node { X x, rev_x; int size; Node *l, *r; }; Node* pool; const int NODES; int pid; using np = Node*; FastSet ss; // 区間の左端全体を表す fastset SegTree<MX> seg; // 区間を集約した値を区間の左端にのせた segtree vector<np> root; // 区間の左端に、dynamic segtree の node を乗せる vector<bool> rev; Sortable_SegTree(int NODES, int KEY_MAX, vector<int> key, vector<X> dat) : N(key.size()), KEY_MAX(KEY_MAX), NODES(NODES), pid(0), ss(key.size()), seg(dat) { pool = new Node[NODES]; init(key, dat); } ~Sortable_SegTree() { delete[] pool; } void set(int i, int key, const X& x) { assert(key < KEY_MAX); split_at(i), split_at(i + 1); rev[i] = 0, root[i] = new_node(); set_rec(root[i], 0, KEY_MAX, key, x); seg.set(i, x); } X prod_all() { return seg.prod_all(); } X prod(int l, int r) { if (pid > NODES * 0.9) rebuild(); split_at(l), split_at(r); return seg.prod(l, r); } void sort_inc(int l, int r) { split_at(l), split_at(r); while (1) { if (pid > NODES * 0.9) rebuild(); np c = root[l]; int i = ss.next(l + 1); if (i == r) break; root[l] = merge(c, root[i]); ss.erase(i), seg.set(i, MX::unit()); } rev[l] = 0, seg.set(l, root[l]->x); }; void sort_dec(int l, int r) { if (pid > NODES * 0.9) rebuild(); sort_inc(l, r), rev[l] = 1; seg.set(l, root[l]->rev_x); }; pair<vc<int>, vc<X>> get_all() { vector<int> key; vector<X> dat; key.reserve(N); dat.reserve(N); auto dfs = [&](auto& dfs, np n, int l, int r, bool rev) -> void { if (!n) return; if (r == l + 1) { key.eb(l), dat.eb(n->x); return; } int m = (l + r) / 2; if (!rev) { dfs(dfs, n->l, l, m, rev), dfs(dfs, n->r, m, r, rev); } if (rev) { dfs(dfs, n->r, m, r, rev), dfs(dfs, n->l, l, m, rev); } }; for (int i = 0; i < N; ++i) { if (ss[i]) dfs(dfs, root[i], 0, KEY_MAX, rev[i]); } return {key, dat}; } private: void init(vector<int>& key, vector<X>& dat) { rev.assign(N, 0), root.clear(), root.reserve(N); seg.build(N, [&](int i) -> X { return dat[i]; }); for (int i = 0; i < N; ++i) { ss.insert(i); root.eb(new_node(MX::unit())); assert(key[i] < KEY_MAX); set_rec(root[i], 0, KEY_MAX, key[i], dat[i]); } } // x が左端になるようにする void split_at(int x) { if (x == N || ss[x]) return; int a = ss.prev(x), b = ss.next(a + 1); ss.insert(x); if (!rev[a]) { auto [nl, nr] = split(root[a], x - a); root[a] = nl, root[x] = nr; rev[a] = rev[x] = 0; seg.set(a, root[a]->x), seg.set(x, root[x]->x); } else { auto [nl, nr] = split(root[a], b - x); root[a] = nr, root[x] = nl; rev[a] = rev[x] = 1; seg.set(a, root[a]->rev_x), seg.set(x, root[x]->rev_x); } } void rebuild() { auto [key, dat] = get_all(); pid = 0; init(key, dat); } np new_node(X x = MX::unit()) { assert(pid < NODES); pool[pid].x = pool[pid].rev_x = x; pool[pid].l = pool[pid].r = nullptr; pool[pid].size = 1; return &(pool[pid++]); } pair<np, np> split(np n, int k) { if (k == 0) { return {nullptr, n}; } if (k == n->size) { return {n, nullptr}; } int s = (n->l ? n->l->size : 0); Node* b = new_node(); if (k <= s) { auto [nl, nr] = split(n->l, k); b->l = nr, b->r = n->r, n->l = nl, n->r = nullptr; } if (k > s) { auto [nl, nr] = split(n->r, k - s); n->l = n->l, n->r = nl, b->l = nullptr, b->r = nr; } update(n), update(b); return {n, b}; } np merge(np a, np b) { if (!a) return b; if (!b) return a; a->l = merge(a->l, b->l), a->r = merge(a->r, b->r); update(a); return a; } void update(np n) { if (!(n->l) && !(n->r)) { return; } if (!(n->l)) { n->x = n->r->x, n->rev_x = n->r->rev_x, n->size = n->r->size; return; } if (!(n->r)) { n->x = n->l->x, n->rev_x = n->l->rev_x, n->size = n->l->size; return; } n->x = MX::op(n->l->x, n->r->x); n->rev_x = MX::op(n->r->rev_x, n->l->rev_x); n->size = n->l->size + n->r->size; } void set_rec(np n, int l, int r, int k, const X& x) { if (r == l + 1) { n->x = n->rev_x = x; return; } int m = (l + r) / 2; if (k < m) { if (!(n->l)) n->l = new_node(); set_rec(n->l, l, m, k, x); } if (m <= k) { if (!(n->r)) n->r = new_node(); set_rec(n->r, m, r, k, x); } update(n); } };
#line 2 "ds/fastset.hpp" // 64-ary tree // space: (N/63) * u64 struct FastSet { static constexpr u32 B = 64; int n, log; vvc<u64> seg; FastSet() {} FastSet(int n) { build(n); } int size() { return n; } template <typename F> FastSet(int n, F f) { build(n, f); } void build(int m) { seg.clear(); n = m; do { seg.push_back(vc<u64>((m + B - 1) / B)); m = (m + B - 1) / B; } while (m > 1); log = len(seg); } template <typename F> void build(int n, F f) { build(n); FOR(i, n) { seg[0][i / B] |= u64(f(i)) << (i % B); } FOR(h, log - 1) { FOR(i, len(seg[h])) { seg[h + 1][i / B] |= u64(bool(seg[h][i])) << (i % B); } } } bool operator[](int i) const { return seg[0][i / B] >> (i % B) & 1; } void insert(int i) { for (int h = 0; h < log; h++) { seg[h][i / B] |= u64(1) << (i % B), i /= B; } } void add(int i) { insert(i); } void erase(int i) { u64 x = 0; for (int h = 0; h < log; h++) { seg[h][i / B] &= ~(u64(1) << (i % B)); seg[h][i / B] |= x << (i % B); x = bool(seg[h][i / B]); i /= B; } } void remove(int i) { erase(i); } // min[x,n) or n int next(int i) { assert(i <= n); chmax(i, 0); for (int h = 0; h < log; h++) { if (i / B == seg[h].size()) break; u64 d = seg[h][i / B] >> (i % B); if (!d) { i = i / B + 1; continue; } i += lowbit(d); for (int g = h - 1; g >= 0; g--) { i *= B; i += lowbit(seg[g][i / B]); } return i; } return n; } // max [0,x], or -1 int prev(int i) { assert(i >= -1); if (i >= n) i = n - 1; for (int h = 0; h < log; h++) { if (i == -1) break; u64 d = seg[h][i / B] << (63 - i % B); if (!d) { i = i / B - 1; continue; } i -= __builtin_clzll(d); for (int g = h - 1; g >= 0; g--) { i *= B; i += topbit(seg[g][i / B]); } return i; } return -1; } bool any(int l, int r) { return next(l) < r; } // [l, r) template <typename F> void enumerate(int l, int r, F f) { for (int x = next(l); x < r; x = next(x + 1)) f(x); } string to_string() { string s(n, '?'); for (int i = 0; i < n; ++i) s[i] = ((*this)[i] ? '1' : '0'); return s; } }; #line 2 "ds/segtree/segtree.hpp" template <class Monoid> struct SegTree { using MX = Monoid; using X = typename MX::value_type; using value_type = X; vc<X> dat; int n, log, size; SegTree() {} SegTree(int n) { build(n); } template <typename F> SegTree(int n, F f) { build(n, f); } SegTree(const vc<X>& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc<X>& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template <typename F> void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; size = 1 << log; dat.assign(size << 1, MX::unit()); FOR(i, n) dat[size + i] = f(i); FOR_R(i, 1, size) update(i); } X get(int i) { return dat[size + i]; } vc<X> get_all() { return {dat.begin() + size, dat.begin() + size + n}; } void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); } void set(int i, const X& x) { assert(i < n); dat[i += size] = x; while (i >>= 1) update(i); } void multiply(int i, const X& x) { assert(i < n); i += size; dat[i] = Monoid::op(dat[i], x); while (i >>= 1) update(i); } X prod(int L, int R) { assert(0 <= L && L <= R && R <= n); X vl = Monoid::unit(), vr = Monoid::unit(); L += size, R += size; while (L < R) { if (L & 1) vl = Monoid::op(vl, dat[L++]); if (R & 1) vr = Monoid::op(dat[--R], vr); L >>= 1, R >>= 1; } return Monoid::op(vl, vr); } X prod_all() { return dat[1]; } template <class F> int max_right(F check, int L) { assert(0 <= L && L <= n && check(Monoid::unit())); if (L == n) return n; L += size; X sm = Monoid::unit(); do { while (L % 2 == 0) L >>= 1; if (!check(Monoid::op(sm, dat[L]))) { while (L < size) { L = 2 * L; if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); } } return L - size; } sm = Monoid::op(sm, dat[L++]); } while ((L & -L) != L); return n; } template <class F> int min_left(F check, int R) { assert(0 <= R && R <= n && check(Monoid::unit())); if (R == 0) return 0; R += size; X sm = Monoid::unit(); do { --R; while (R > 1 && (R % 2)) R >>= 1; if (!check(Monoid::op(dat[R], sm))) { while (R < size) { R = 2 * R + 1; if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); } } return R + 1 - size; } sm = Monoid::op(dat[R], sm); } while ((R & -R) != R); return 0; } // prod_{l<=i<r} A[i xor x] X xor_prod(int l, int r, int xor_val) { static_assert(Monoid::commute); X x = Monoid::unit(); for (int k = 0; k < log + 1; ++k) { if (l >= r) break; if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); } if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); } l /= 2, r /= 2, xor_val /= 2; } return x; } }; #line 3 "ds/segtree/sortable_segtree.hpp" template <typename Monoid> struct Sortable_SegTree { using MX = Monoid; using X = typename MX::value_type; const int N, KEY_MAX; struct Node { X x, rev_x; int size; Node *l, *r; }; Node* pool; const int NODES; int pid; using np = Node*; FastSet ss; // 区間の左端全体を表す fastset SegTree<MX> seg; // 区間を集約した値を区間の左端にのせた segtree vector<np> root; // 区間の左端に、dynamic segtree の node を乗せる vector<bool> rev; Sortable_SegTree(int NODES, int KEY_MAX, vector<int> key, vector<X> dat) : N(key.size()), KEY_MAX(KEY_MAX), NODES(NODES), pid(0), ss(key.size()), seg(dat) { pool = new Node[NODES]; init(key, dat); } ~Sortable_SegTree() { delete[] pool; } void set(int i, int key, const X& x) { assert(key < KEY_MAX); split_at(i), split_at(i + 1); rev[i] = 0, root[i] = new_node(); set_rec(root[i], 0, KEY_MAX, key, x); seg.set(i, x); } X prod_all() { return seg.prod_all(); } X prod(int l, int r) { if (pid > NODES * 0.9) rebuild(); split_at(l), split_at(r); return seg.prod(l, r); } void sort_inc(int l, int r) { split_at(l), split_at(r); while (1) { if (pid > NODES * 0.9) rebuild(); np c = root[l]; int i = ss.next(l + 1); if (i == r) break; root[l] = merge(c, root[i]); ss.erase(i), seg.set(i, MX::unit()); } rev[l] = 0, seg.set(l, root[l]->x); }; void sort_dec(int l, int r) { if (pid > NODES * 0.9) rebuild(); sort_inc(l, r), rev[l] = 1; seg.set(l, root[l]->rev_x); }; pair<vc<int>, vc<X>> get_all() { vector<int> key; vector<X> dat; key.reserve(N); dat.reserve(N); auto dfs = [&](auto& dfs, np n, int l, int r, bool rev) -> void { if (!n) return; if (r == l + 1) { key.eb(l), dat.eb(n->x); return; } int m = (l + r) / 2; if (!rev) { dfs(dfs, n->l, l, m, rev), dfs(dfs, n->r, m, r, rev); } if (rev) { dfs(dfs, n->r, m, r, rev), dfs(dfs, n->l, l, m, rev); } }; for (int i = 0; i < N; ++i) { if (ss[i]) dfs(dfs, root[i], 0, KEY_MAX, rev[i]); } return {key, dat}; } private: void init(vector<int>& key, vector<X>& dat) { rev.assign(N, 0), root.clear(), root.reserve(N); seg.build(N, [&](int i) -> X { return dat[i]; }); for (int i = 0; i < N; ++i) { ss.insert(i); root.eb(new_node(MX::unit())); assert(key[i] < KEY_MAX); set_rec(root[i], 0, KEY_MAX, key[i], dat[i]); } } // x が左端になるようにする void split_at(int x) { if (x == N || ss[x]) return; int a = ss.prev(x), b = ss.next(a + 1); ss.insert(x); if (!rev[a]) { auto [nl, nr] = split(root[a], x - a); root[a] = nl, root[x] = nr; rev[a] = rev[x] = 0; seg.set(a, root[a]->x), seg.set(x, root[x]->x); } else { auto [nl, nr] = split(root[a], b - x); root[a] = nr, root[x] = nl; rev[a] = rev[x] = 1; seg.set(a, root[a]->rev_x), seg.set(x, root[x]->rev_x); } } void rebuild() { auto [key, dat] = get_all(); pid = 0; init(key, dat); } np new_node(X x = MX::unit()) { assert(pid < NODES); pool[pid].x = pool[pid].rev_x = x; pool[pid].l = pool[pid].r = nullptr; pool[pid].size = 1; return &(pool[pid++]); } pair<np, np> split(np n, int k) { if (k == 0) { return {nullptr, n}; } if (k == n->size) { return {n, nullptr}; } int s = (n->l ? n->l->size : 0); Node* b = new_node(); if (k <= s) { auto [nl, nr] = split(n->l, k); b->l = nr, b->r = n->r, n->l = nl, n->r = nullptr; } if (k > s) { auto [nl, nr] = split(n->r, k - s); n->l = n->l, n->r = nl, b->l = nullptr, b->r = nr; } update(n), update(b); return {n, b}; } np merge(np a, np b) { if (!a) return b; if (!b) return a; a->l = merge(a->l, b->l), a->r = merge(a->r, b->r); update(a); return a; } void update(np n) { if (!(n->l) && !(n->r)) { return; } if (!(n->l)) { n->x = n->r->x, n->rev_x = n->r->rev_x, n->size = n->r->size; return; } if (!(n->r)) { n->x = n->l->x, n->rev_x = n->l->rev_x, n->size = n->l->size; return; } n->x = MX::op(n->l->x, n->r->x); n->rev_x = MX::op(n->r->rev_x, n->l->rev_x); n->size = n->l->size + n->r->size; } void set_rec(np n, int l, int r, int k, const X& x) { if (r == l + 1) { n->x = n->rev_x = x; return; } int m = (l + r) / 2; if (k < m) { if (!(n->l)) n->l = new_node(); set_rec(n->l, l, m, k, x); } if (m <= k) { if (!(n->r)) n->r = new_node(); set_rec(n->r, m, r, k, x); } update(n); } };