This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub shiomusubi496/library
#include "data-struct/other/SkipList.hpp"
セグ木の各区切り目の高さをルーラー関数ではなくランダムにした感じのデータ構造。なので insert や erase ができる。
SkipList()
SkipList(Rand rnd)
rnd
SkipList(vector<T> , Rand rnd = Rand())
v
void init(vector<T> v)
T prod(int l, int r)
op(a[l], a[l+1], ..., a[r-1])
T get(int k)
a[k]
T all_prod()
op(a[0], a[1], ..., a[N-1])
int size()
bool empty()
void insert(int k, T x)
k
x
void erase(int k)
void set(int k, T x)
void apply(int k, U f)
mp(f, a[k])
void update(int k, T upd(T))
upd(a[k])
void apply(int l, int r, U f)
a[l], a[l+1], ..., a[r-1]
mp(f, a[l]), mp(f, a[l+1]), ..., mp(f, a[r-1])
int max_right(int l, bool f(T))
[l, r)
f
true
r
f(e) = true
r = l
f(op(a[l], a[l+1], ..., a[r-1])) = true
r = n
f(op(a[l], a[l+1], ..., a[r])) = false
int min_left(int r, bool f(T))
l
l = r
l = 0
f(op(a[l-1], a[l], ..., a[r-1])) = false
#pragma once #include "../../other/template.hpp" #include "../../other/monoid.hpp" #include "../../random/Random.hpp" template<class A, class Rand = Random32> class SkipList { private: using M = typename A::M; using E = typename A::E; using T = typename M::value_type; using U = typename E::value_type; static inline int get_level(Rand& rnd) { int level = 1; while ((rnd() & 1) == 0) ++level; return level; } struct node; using node_ptr = node*; struct next_node { node_ptr node; int dist; T sm; U lazy; bool lazyflag; next_node(node_ptr n, int d, const T& s) : node(n), dist(d), sm(s), lazyflag(false) {} next_node(node_ptr n, int d, const T& s, const U& l) : node(n), dist(d), sm(s), lazy(l), lazyflag(true) {} }; struct node { std::vector<next_node> nxt; std::vector<node_ptr> prv; int level() const { assert(nxt.size() == prv.size()); return nxt.size(); } node(Rand& rnd) : node(get_level(rnd)) {} node(int lev) : nxt(lev, {nullptr, 1, M::id()}), prv(lev, nullptr) {} }; using nodepair = std::pair<node_ptr, node_ptr>; Rand rnd; nodepair sl; template<bool AlwaysTrue = true, typename std::enable_if<!Monoid::has_mul_op<A>::value && AlwaysTrue>::type* = nullptr> static inline T Aop(const U& a, const T& b, int) { return A::op(a, b); } template<bool AlwaysTrue = true, typename std::enable_if<Monoid::has_mul_op<A>::value && AlwaysTrue>::type* = nullptr> static inline T Aop(const U& a, const T& b, int c) { return A::mul_op(a, c, b); } static inline void all_apply(const node_ptr& nd, int k, const U& x) { assert(0 <= k && k < nd->level()); nd->nxt[k].sm = Aop(x, nd->nxt[k].sm, nd->nxt[k].dist); if (k != 0) { if (nd->nxt[k].lazyflag) { nd->nxt[k].lazy = E::op(nd->nxt[k].lazy, x); } else { nd->nxt[k].lazy = x; nd->nxt[k].lazyflag = true; } } } static inline void eval(const node_ptr& nd, int k) { assert(0 <= k && k < nd->level()); if (k != 0 && nd->nxt[k].lazyflag) { for (auto ptr = nd; ptr != nd->nxt[k].node; ptr = ptr->nxt[k - 1].node) { all_apply(ptr, k - 1, nd->nxt[k].lazy); } nd->nxt[k].lazyflag = false; } } static inline void all_eval(const nodepair& sl, int k) { auto nd = sl.first; int cnt = 0; rrep (i, sl.first->level(), 1) { while (cnt + nd->nxt[i].dist <= k) { cnt += nd->nxt[i].dist; nd = nd->nxt[i].node; } eval(nd, i); } } static inline void calc(const node_ptr& l, int k) { assert(1 <= k && k < l->level()); l->nxt[k].sm = l->nxt[k - 1].sm; for (node_ptr ptr = l->nxt[k - 1].node; ptr != l->nxt[k].node; ptr = ptr->nxt[k - 1].node) { l->nxt[k].sm = M::op(l->nxt[k].sm, ptr->nxt[k - 1].sm); } } static inline void all_calc(const nodepair& sl, int k) { auto nd = sl.first; int cnt = 0; std::vector<node_ptr> nds(sl.first->level()); rrep (i, sl.first->level(), 1) { while (cnt + nd->nxt[i].dist <= k) { cnt += nd->nxt[i].dist; nd = nd->nxt[i].node; } nds[i] = nd; } rep (i, 1, sl.first->level()) calc(nds[i], i); } static void match_level(nodepair& lhs, nodepair& rhs) { const int llv = lhs.first->level(), rlv = rhs.second->level(); if (llv < rlv) { eval(lhs.first, llv - 1); lhs.first->prv.resize(rlv, {lhs.first->prv.back()}); lhs.first->nxt.resize(rlv, {lhs.first->nxt.back()}); lhs.second->prv.resize(rlv, {lhs.second->prv.back()}); lhs.second->nxt.resize(rlv, {lhs.second->nxt.back()}); } else if (llv > rlv) { eval(rhs.second, rlv - 1); rhs.first->prv.resize(llv, {rhs.first->prv.back()}); rhs.first->nxt.resize(llv, {rhs.first->nxt.back()}); rhs.second->prv.resize(llv, {rhs.second->prv.back()}); rhs.second->nxt.resize(llv, {rhs.second->nxt.back()}); } } static nodepair merge(nodepair&& lhs, nodepair&& rhs, Rand& rnd) { assert(lhs.first != rhs.first); assert(lhs.second != rhs.second); if (lhs.first == lhs.second) { delete lhs.first; auto res = std::move(rhs); lhs = rhs = {nullptr, nullptr}; return res; } if (rhs.first == rhs.second) { delete rhs.first; auto res = std::move(lhs); lhs = rhs = {nullptr, nullptr}; return res; } match_level(lhs, rhs); rep (i, lhs.first->level()) { auto&& l = lhs.second->prv[i]; auto&& r = rhs.first; l->nxt[i].node = r; r->prv[i] = std::move(l); } delete lhs.second; const int lev = get_level(rnd); while (lev < rhs.first->level()) { const int h = rhs.first->level(); const auto lp = std::move(rhs.first->prv.back()); const auto l = lp->nxt[h - 1]; const auto r = std::move(rhs.first->nxt.back()); eval(lp, h - 1); eval(rhs.first, h - 1); rhs.first->prv.pop_back(); rhs.first->nxt.pop_back(); lp->nxt[h - 1] = {r.node, l.dist + r.dist, M::op(l.sm, r.sm)}; r.node->prv[h - 1] = lp; } if (lev >= lhs.first->level()) { eval(lhs.first, lhs.first->level() - 1); eval(rhs.first, lhs.first->level() - 1); lhs.first->prv.resize(lev + 1, {lhs.first->prv.back()}); lhs.first->nxt.resize(lev, {lhs.first->nxt.back()}); rhs.first->prv.resize(lev, {rhs.first->prv.back()}); rhs.first->nxt.resize(lev, {rhs.first->nxt.back()}); rhs.second->prv.resize(lev, {rhs.second->prv.back()}); rhs.second->nxt.resize(lev + 1, {rhs.second->nxt.back()}); const auto& lp = rhs.first->prv.back(); const auto& l = lp->nxt[lev - 1]; const auto& r = rhs.first->nxt.back(); lhs.first->nxt.emplace_back(rhs.second, l.dist + r.dist, M::op(l.sm, r.sm)); rhs.second->prv.push_back(lhs.first); } nodepair res{lhs.first, rhs.second}; lhs = rhs = {nullptr, nullptr}; return res; } static std::pair<nodepair, nodepair> split(nodepair&& sl, int k) { const int n = sl.first->nxt.back().dist; assert(0 <= k && k <= n); if (n == 0 || k == 0) { node_ptr np = new node(1); auto res = std::make_pair(nodepair{np, np}, std::move(sl)); sl = {nullptr, nullptr}; return res; } if (k == n) { node_ptr np = new node(1); auto res = std::make_pair(std::move(sl), nodepair{np, np}); sl = {nullptr, nullptr}; return res; } const int h = sl.first->level(); std::vector<node_ptr> lft(h); std::vector<int> idx(h); lft[h - 1] = sl.first; idx[h - 1] = 0; rrep (i, h - 1) { lft[i] = lft[i + 1]; idx[i] = idx[i + 1]; while (idx[i] + lft[i]->nxt[i].dist < k) { idx[i] += lft[i]->nxt[i].dist; lft[i] = lft[i]->nxt[i].node; } } rrep (i, h, 1) eval(lft[i], i); node_ptr npl = new node(h); node_ptr npr = lft[0]->nxt[0].node; rep (i, h) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; const int d = l->nxt[i].dist; l->nxt[i] = {npl, k - idx[i], l->nxt[i].sm}; npl->prv[i] = l; if (i != 0) calc(l, i); if (npr == r) { r->prv[i] = nullptr; } else { npr->prv.push_back(nullptr); npr->nxt.emplace_back(r, d + idx[i] - k, M::id()); r->prv[i] = npr; calc(npr, i); } } auto res = std::make_pair(nodepair{sl.first, npl}, nodepair{npr, sl.second}); sl = {nullptr, nullptr}; return res; } SkipList(const nodepair& sl, const Rand& rnd) : rnd(rnd), sl(sl) {} SkipList(nodepair&& sl, const Rand& rnd) : rnd(rnd), sl(std::move(sl)) {} static node_ptr get_ptr(const nodepair& sl, int k) { int cnt = 0; node_ptr nw = sl.first; rrep (i, sl.first->level()) { while (cnt + nw->nxt[i].dist <= k) { cnt += nw->nxt[i].dist; nw = nw->nxt[i].node; } } return nw; } public: SkipList() : SkipList(Rand()) {} SkipList(const Rand& rnd) : rnd(rnd) { sl.first = sl.second = new node(1); } SkipList(const std::vector<T>& v, const Rand& rnd = Rand()) : rnd(rnd) { init(v); } SkipList(const SkipList& other) : SkipList(other.get_data(), other.rnd) {} SkipList(SkipList&& other) : rnd(other.rnd), sl(std::move(other.sl)) { other.sl = {nullptr, nullptr}; } ~SkipList() { for (node_ptr ptr = sl.first; ptr;) { node_ptr nxt = ptr->nxt[0].node; delete ptr; ptr = nxt; } sl = {nullptr, nullptr}; } SkipList& operator=(const SkipList& other) { if (this == &other) return *this; init(other.get_data()); return *this; } SkipList& operator=(SkipList&& other) { if (this == &other) return *this; sl = std::move(other.sl); other.sl = {nullptr, nullptr}; return *this; } void init(const std::vector<T>& v) { if (sl.first) { for (node_ptr ptr = sl.first; ptr;) { node_ptr nxt = ptr->nxt[0].node; delete ptr; ptr = nxt; } } const int n = v.size(); std::vector<int> lev(n + 1); rep (i, 1, n) lev[i] = get_level(rnd); lev[0] = lev[n] = *max_element(lev.begin() + 1, lev.end() - 1) + 1; std::vector<node_ptr> nd(n + 1); rep (i, n + 1) nd[i] = new node(lev[i]); rep (i, n) { nd[i]->nxt[0] = {nd[i + 1], 1, v[i]}; nd[i + 1]->prv[0] = nd[i]; } nd[0]->prv[0] = nullptr; nd[n]->nxt[0] = {nullptr, 1, M::id()}; rep (i, 1, lev[0]) { std::vector<int> idx; rep (j, n + 1) { if (lev[j] > i) idx.push_back(j); } const int m = idx.size(); rep (j, m - 1) { nd[idx[j]]->nxt[i] = {nd[idx[j + 1]], idx[j + 1] - idx[j], nd[idx[j]]->nxt[i - 1].sm}; for (node_ptr ptr = nd[idx[j]]->nxt[i - 1].node; ptr != nd[idx[j + 1]]; ptr = ptr->nxt[i - 1].node) { nd[idx[j]]->nxt[i].sm = M::op(nd[idx[j]]->nxt[i].sm, ptr->nxt[i - 1].sm); } nd[idx[j + 1]]->prv[i] = nd[idx[j]]; } nd[idx[0]]->prv[i] = nullptr; nd[idx[m - 1]]->nxt[i] = {nullptr, 1, M::id()}; } sl = {nd[0], nd[n]}; } int size() const { assert(sl.first); return sl.first == sl.second ? 0 : sl.first->nxt.back().dist; } bool empty() const { return sl.first == sl.second; } void insert(int k, const T& x) { const int n = size(); assert(0 <= k && k <= n); if (n == 0) { delete sl.first; sl.first = new node(1); sl.second = new node(1); sl.first->nxt[0] = {sl.second, 1, x}; sl.second->prv[0] = sl.first; return; } if (k == 0) { /* if (lev < sl.first->level()) { node_ptr ptr = new node(lev); rep (i, lev) { ptr->nxt[i] = sl.first->nxt[i]; sl.first->nxt[i].node->prv[i] = ptr; sl.first->nxt[i] = {ptr, 1, x}; ptr->prv[i] = sl.first; } rep (i, lev, sl.first->level()) { ++sl.first->nxt[i].dist; sl.first->nxt[i].sm = M::op(x, sl.first->nxt[i].sm); } } else { node_ptr ptr = new node(lev); rep (i, sl.first->level()) { ptr->nxt[i] = sl.first->nxt[i]; sl.first->nxt[i].node->prv[i] = ptr; sl.first->nxt[i] = {ptr, 1, x}; ptr->prv[i] = sl.first; } rep (i, sl.first->level(), lev) { sl.first->nxt[i] = sl.first->nxt[i - 1]; sl.first->prv[i] = sl.first->prv[i - 1]; } sl.first->prv.resize(lev + 1, {sl.first->prv.back()}); sl.first->nxt.resize(lev + 1, {sl.first->nxt.back()}); sl.second->prv.resize(lev + 1, {sl.second->prv.back()}); sl.second->nxt.resize(lev + 1, {sl.second->nxt.back()}); sl.first->nxt.back() = {sl.second, sl.first->nxt.back().dist + 1, M::op(x, sl.first->nxt.back().sm)}; sl.second->prv.back() = sl.first; } */ nodepair p{new node(1), new node(1)}; p.first->nxt[0] = {p.second, 1, x}; p.second->prv[0] = p.first; sl = merge(std::move(p), std::move(sl), rnd); return; } const int h = sl.first->level(); std::vector<node_ptr> lft(h); std::vector<int> idx(h); lft[h - 1] = sl.first; idx[h - 1] = 0; rrep (i, h - 1) { lft[i] = lft[i + 1]; idx[i] = idx[i + 1]; while (idx[i] + lft[i]->nxt[i].dist < k) { idx[i] += lft[i]->nxt[i].dist; lft[i] = lft[i]->nxt[i].node; } } rrep (i, h, 1) eval(lft[i], i); const int lev = get_level(rnd); node_ptr np = new node(lev); if (lev < h) { rep (i, lev) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; np->nxt[i] = {r, idx[i] + l->nxt[i].dist - k + 1, x}; r->prv[i] = np; l->nxt[i] = {np, k - idx[i], l->nxt[i].sm}; np->prv[i] = l; } rep (i, lev, h) ++lft[i]->nxt[i].dist; rep (i, 1, h) { calc(lft[i], i); if (i < lev) calc(lft[i]->nxt[i].node, i); } } else { rep (i, h) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; np->nxt[i] = {r, idx[i] + l->nxt[i].dist - k + 1, x}; r->prv[i] = np; l->nxt[i] = {np, k - idx[i], l->nxt[i].sm}; np->prv[i] = l; } rep (i, 1, h) { calc(lft[i], i); if (i < lev) calc(lft[i]->nxt[i].node, i); } sl.first->prv.resize(lev + 1, {sl.first->prv.back()}); sl.first->nxt.resize(lev + 1, {sl.second, n + 1, x}); sl.second->prv.resize(lev + 1, sl.first); sl.second->nxt.resize(lev + 1, {sl.second->nxt.back()}); rep (i, h, lev) { sl.first->nxt[i] = {np, k, x}; np->prv[i] = sl.first; np->nxt[i] = {sl.second, n - k + 1, x}; sl.second->prv[i] = np; calc(sl.first, i); calc(np, i); } calc(sl.first, lev); } } void erase(int k) { const int n = size(); assert(0 <= k && k < n); if (n == 1) { delete sl.first; delete sl.second; sl.first = sl.second = new node(1); return; } if (k == 0) { all_eval(sl, 0); all_eval(sl, 1); rep (i, sl.first->level()) { if (sl.first->nxt[i].dist == 1) { const auto l = sl.first; const auto m = l->nxt[i].node; const auto r = m->nxt[i].node; l->nxt[i] = {r, l->nxt[i].dist + m->nxt[i].dist - 1, m->nxt[i].sm}; r->prv[i] = l; } else { sl.first->nxt[i].dist--; } } rep (i, 1, sl.first->level()) calc(sl.first, i); return; } all_eval(sl, k - 1); all_eval(sl, k); node_ptr np = sl.first; int cnt = 0; rrep (i, sl.first->level()) { while (cnt + np->nxt[i].dist <= k) { cnt += np->nxt[i].dist; np = np->nxt[i].node; } if (cnt == k) { const auto l = np->prv[i]; const auto r = np->nxt[i].node; r->prv[i] = l; l->nxt[i] = {r, l->nxt[i].dist + np->nxt[i].dist - 1, l->nxt[i].sm}; } else { np->nxt[i].dist--; } } delete np; all_calc(sl, k - 1); } T prod(int l, int r) const { assert(0 <= l && l <= r && r <= size()); all_eval(sl, l); all_eval(sl, r - 1); auto np = get_ptr(sl, l); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (l + np->nxt[t].dist > r) break; l += np->nxt[t].dist; sm = M::op(sm, np->nxt[t].sm); np = np->nxt[t].node; } } return sm; } T all_prod() const { return sl.first->nxt.back().sm; } T get(int k) const { assert(0 <= k && k < size()); all_eval(sl, k); return get_ptr(sl, k)->nxt[0].sm; } void apply(int l, int r, const U& x) { assert(0 <= l && l <= r && r <= size()); all_eval(sl, l); all_eval(sl, r - 1); auto np = get_ptr(sl, l); int idx = l; rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (idx + np->nxt[t].dist > r) break; idx += np->nxt[t].dist; all_apply(np, t, x); np = np->nxt[t].node; } } all_eval(sl, l); all_eval(sl, r - 1); all_calc(sl, l); all_calc(sl, r - 1); } template<class Upd> void update(int k, const Upd& upd) { assert(0 <= k && k < size()); all_eval(sl, k); auto nd = get_ptr(sl, k); nd->nxt[0].sm = upd(nd->nxt[0].sm); all_calc(sl, k); } void set(int k, const T& x) { update(k, [&](const T&) { return x; }); } void apply(int k, const U& x) { update(k, [&](const T& sm) { return Aop(x, sm, 1); }); } template<class C> int max_right(int l, const C& cond) const { assert(0 <= l && l <= size()); assert(cond(M::id())); if (l == size()) return size(); all_eval(sl, l); auto np = get_ptr(sl, l); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (t != np->level() - 1) eval(np, t + 1); if (!cond(M::op(sm, np->nxt[t].sm))) break; sm = M::op(sm, np->nxt[t].sm); l += np->nxt[t].dist; np = np->nxt[t].node; if (np == sl.second) return size(); } } return l; } template<class C> int min_left(int r, const C& cond) const { assert(0 <= r && r <= size()); assert(cond(M::id())); if (r == 0) return 0; all_eval(sl, r - 1); auto np = get_ptr(sl, r); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (t != np->level() - 1) eval(np->prv[t], t + 1); if (!cond(M::op(sm, np->prv[t]->nxt[t].sm))) break; sm = M::op(sm, np->prv[t]->nxt[t].sm); r -= np->prv[t]->nxt[t].dist; np = np->prv[t]; if (np == sl.first) return 0; } } return r; } std::vector<T> get_data() const { rrep (i, sl.first->level(), 1) { for (node_ptr ptr = sl.first; ptr != sl.second; ptr = ptr->nxt[i].node) { eval(ptr, i); } } std::vector<T> res; res.reserve(size()); for (node_ptr ptr = sl.first; ptr != sl.second; ptr = ptr->nxt[0].node) { res.push_back(ptr->nxt[0].sm); } return res; } friend SkipList merge(SkipList lhs, SkipList rhs) { return {merge(std::move(lhs.sl), std::move(rhs.sl), lhs.rnd), lhs.rnd}; } friend std::pair<SkipList, SkipList> split(SkipList sl, int k) { auto s = split(std::move(sl.sl), k); return {SkipList{std::move(s.first), sl.rnd}, SkipList{std::move(s.second), sl.rnd}}; } }; /** * @brief SkipList * @docs docs/data-struct/other/SkipList.md */
#line 2 "data-struct/other/SkipList.hpp" #line 2 "other/template.hpp" #include <bits/stdc++.h> #line 2 "template/macros.hpp" #line 4 "template/macros.hpp" #ifndef __COUNTER__ #define __COUNTER__ __LINE__ #endif #define OVERLOAD5(a, b, c, d, e, ...) e #define REP1_0(b, c) REP1_1(b, c) #define REP1_1(b, c) \ for (ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(b); ++REP_COUNTER_##c) #define REP1(b) REP1_0(b, __COUNTER__) #define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i) #define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c)) #define rep(...) OVERLOAD5(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define RREP2(i, a) for (ll i = (ll)(a)-1; i >= 0; --i) #define RREP3(i, a, b) for (ll i = (ll)(a)-1; i >= (ll)(b); --i) #define RREP4(i, a, b, c) for (ll i = (ll)(a)-1; i >= (ll)(b); i -= (ll)(c)) #define rrep(...) OVERLOAD5(__VA_ARGS__, RREP4, RREP3, RREP2)(__VA_ARGS__) #define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i) #define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i) #define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c)) #define reps(...) OVERLOAD5(__VA_ARGS__, REPS4, REPS3, REPS2)(__VA_ARGS__) #define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i) #define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i) #define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c)) #define rreps(...) OVERLOAD5(__VA_ARGS__, RREPS4, RREPS3, RREPS2)(__VA_ARGS__) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define all(v) std::begin(v), std::end(v) #define rall(v) std::rbegin(v), std::rend(v) #if __cpp_if_constexpr >= 201606L #define IF_CONSTEXPR constexpr #else #define IF_CONSTEXPR #endif #define IO_BUFFER_SIZE (1 << 17) #line 2 "template/alias.hpp" #line 4 "template/alias.hpp" using ll = long long; using uint = unsigned int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; using ld = long double; using PLL = std::pair<ll, ll>; template<class T> using prique = std::priority_queue<T, std::vector<T>, std::greater<T>>; template<class T> struct infinity { static constexpr T value = std::numeric_limits<T>::max() / 2; static constexpr T mvalue = std::numeric_limits<T>::lowest() / 2; static constexpr T max = std::numeric_limits<T>::max(); static constexpr T min = std::numeric_limits<T>::lowest(); }; #if __cplusplus <= 201402L template<class T> constexpr T infinity<T>::value; template<class T> constexpr T infinity<T>::mvalue; template<class T> constexpr T infinity<T>::max; template<class T> constexpr T infinity<T>::min; #endif #if __cpp_variable_templates >= 201304L template<class T> constexpr T INF = infinity<T>::value; #endif constexpr ll inf = infinity<ll>::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; #line 2 "template/type_traits.hpp" #line 5 "template/type_traits.hpp" template<class T, class... Args> struct function_traits_impl { using result_type = T; template<std::size_t idx> using argument_type = typename std::tuple_element<idx, std::tuple<Args...>>::type; using argument_tuple = std::tuple<Args...>; static constexpr std::size_t arg_size() { return sizeof...(Args); } }; template<class> struct function_traits_helper; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...)> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...)&> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...) const> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...) const&> { using type = function_traits_impl<Res, Args...>; }; #if __cpp_noexcept_function_type >= 201510L template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...) noexcept> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...)& noexcept> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...) const noexcept> { using type = function_traits_impl<Res, Args...>; }; template<class Res, class Tp, class... Args> struct function_traits_helper<Res (Tp::*)(Args...) const& noexcept> { using type = function_traits_impl<Res, Args...>; }; #endif template<class F> using function_traits = typename function_traits_helper< decltype(&std::remove_reference<F>::type::operator())>::type; template<class F> using function_result_type = typename function_traits<F>::result_type; template<class F, std::size_t idx> using function_argument_type = typename function_traits<F>::template argument_type<idx>; template<class F> using function_argument_tuple = typename function_traits<F>::argument_tuple; template<class T> using is_signed_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_signed<T>::value) || std::is_same<T, i128>::value>; template<class T> using is_unsigned_int = std::integral_constant<bool, (std::is_integral<T>::value && std::is_unsigned<T>::value) || std::is_same<T, u128>::value>; template<class T> using is_int = std::integral_constant<bool, is_signed_int<T>::value || is_unsigned_int<T>::value>; template<class T> using make_signed_int = typename std::conditional< std::is_same<T, i128>::value || std::is_same<T, u128>::value, std::common_type<i128>, std::make_signed<T>>::type; template<class T> using make_unsigned_int = typename std::conditional< std::is_same<T, i128>::value || std::is_same<T, u128>::value, std::common_type<u128>, std::make_unsigned<T>>::type; template<class T, class = void> struct is_range : std::false_type {}; template<class T> struct is_range< T, decltype(all(std::declval<typename std::add_lvalue_reference<T>::type>()), (void)0)> : std::true_type {}; template<class T, bool = is_range<T>::value> struct range_rank : std::integral_constant<std::size_t, 0> {}; template<class T> struct range_rank<T, true> : std::integral_constant<std::size_t, range_rank<typename T::value_type>::value + 1> {}; template<std::size_t size> struct int_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::int_least8_t, typename std::conditional< size <= 16, std::int_least16_t, typename std::conditional< size <= 32, std::int_least32_t, typename std::conditional<size <= 64, std::int_least64_t, i128>::type>::type>::type>::type; }; template<std::size_t size> using int_least_t = typename int_least<size>::type; template<std::size_t size> struct uint_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::uint_least8_t, typename std::conditional< size <= 16, std::uint_least16_t, typename std::conditional< size <= 32, std::uint_least32_t, typename std::conditional<size <= 64, std::uint_least64_t, u128>::type>::type>::type>::type; }; template<std::size_t size> using uint_least_t = typename uint_least<size>::type; template<class T> using double_size_int = int_least<std::numeric_limits<T>::digits * 2 + 1>; template<class T> using double_size_int_t = typename double_size_int<T>::type; template<class T> using double_size_uint = uint_least<std::numeric_limits<T>::digits * 2>; template<class T> using double_size_uint_t = typename double_size_uint<T>::type; template<class T> using double_size = typename std::conditional<is_signed_int<T>::value, double_size_int<T>, double_size_uint<T>>::type; template<class T> using double_size_t = typename double_size<T>::type; #line 2 "template/in.hpp" #line 4 "template/in.hpp" #include <unistd.h> #line 8 "template/in.hpp" template<std::size_t buf_size = IO_BUFFER_SIZE, std::size_t decimal_precision = 16> class Scanner { private: template<class, class = void> struct has_scan : std::false_type {}; template<class T> struct has_scan< T, decltype(std::declval<T>().scan(std::declval<Scanner&>()), (void)0)> : std::true_type {}; int fd; int idx, sz; bool state; std::array<char, IO_BUFFER_SIZE + 1> buffer; inline char cur() { if (idx == sz) load(); if (idx == sz) { state = false; return '\0'; } return buffer[idx]; } inline void next() { if (idx == sz) load(); if (idx == sz) return; ++idx; } public: inline void load() { int len = sz - idx; if (idx < len) return; std::memcpy(buffer.begin(), buffer.begin() + idx, len); sz = len + read(fd, buffer.data() + len, buf_size - len); buffer[sz] = 0; idx = 0; } Scanner(int fd) : fd(fd), idx(0), sz(0), state(true) {} Scanner(FILE* fp) : fd(fileno(fp)), idx(0), sz(0), state(true) {} inline char scan_char() { if (idx == sz) load(); return idx == sz ? '\0' : buffer[idx++]; } Scanner ignore(int n = 1) { if (idx + n > sz) load(); idx += n; return *this; } inline void discard_space() { if (idx == sz) load(); while (('\t' <= buffer[idx] && buffer[idx] <= '\r') || buffer[idx] == ' ') { if (++idx == sz) load(); } } void scan(char& a) { discard_space(); a = scan_char(); } void scan(bool& a) { discard_space(); a = scan_char() != '0'; } void scan(std::string& a) { discard_space(); a.clear(); while (cur() != '\0' && (buffer[idx] < '\t' || '\r' < buffer[idx]) && buffer[idx] != ' ') { a += scan_char(); } } template<std::size_t len> void scan(std::bitset<len>& a) { discard_space(); if (idx + len > sz) load(); rrep (i, len) a[i] = buffer[idx++] != '0'; } template<class T, typename std::enable_if<is_signed_int<T>::value && !has_scan<T>::value>::type* = nullptr> void scan(T& a) { discard_space(); if (buffer[idx] == '-') { ++idx; if (idx + 40 > sz && (idx == sz || ('0' <= buffer[sz - 1] && buffer[sz - 1] <= '9'))) load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 - (buffer[idx++] - '0'); } } else { if (idx + 40 > sz && '0' <= buffer[sz - 1] && buffer[sz - 1] <= '9') load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 + (buffer[idx++] - '0'); } } } template<class T, typename std::enable_if<is_unsigned_int<T>::value && !has_scan<T>::value>::type* = nullptr> void scan(T& a) { discard_space(); if (idx + 40 > sz && '0' <= buffer[sz - 1] && buffer[sz - 1] <= '9') load(); a = 0; while ('0' <= buffer[idx] && buffer[idx] <= '9') { a = a * 10 + (buffer[idx++] - '0'); } } template<class T, typename std::enable_if<std::is_floating_point<T>::value && !has_scan<T>::value>::type* = nullptr> void scan(T& a) { discard_space(); bool sgn = false; if (cur() == '-') { sgn = true; next(); } a = 0; while ('0' <= cur() && cur() <= '9') { a = a * 10 + cur() - '0'; next(); } if (cur() == '.') { next(); T n = 0, d = 1; for (int i = 0; '0' <= cur() && cur() <= '9' && i < (int)decimal_precision; ++i) { n = n * 10 + cur() - '0'; d *= 10; next(); } while ('0' <= cur() && cur() <= '9') next(); a += n / d; } if (sgn) a = -a; } private: template<std::size_t i, class... Args> void scan(std::tuple<Args...>& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { scan(std::get<i>(a)); scan<i + 1, Args...>(a); } } public: template<class... Args> void scan(std::tuple<Args...>& a) { scan<0, Args...>(a); } template<class T, class U> void scan(std::pair<T, U>& a) { scan(a.first); scan(a.second); } template<class T, typename std::enable_if<is_range<T>::value && !has_scan<T>::value>::type* = nullptr> void scan(T& a) { for (auto&& i : a) scan(i); } template<class T, typename std::enable_if<has_scan<T>::value>::type* = nullptr> void scan(T& a) { a.scan(*this); } void operator()() {} template<class Head, class... Args> void operator()(Head& head, Args&... args) { scan(head); operator()(args...); } template<class T> Scanner& operator>>(T& a) { scan(a); return *this; } explicit operator bool() const { return state; } friend Scanner& getline(Scanner& scan, std::string& a) { a.erase(); char c; if ((c = scan.scan_char()) == '\n' || c == '\0') return scan; a += c; while ((c = scan.scan_char()) != '\n' && c != '\0') a += c; scan.state = true; return scan; } }; Scanner<> scan(0); #line 2 "template/out.hpp" #line 8 "template/out.hpp" struct NumberToString { char buf[10000][4]; constexpr NumberToString() : buf{} { rep (i, 10000) { int n = i; rrep (j, 4) { buf[i][j] = (char)('0' + n % 10); n /= 10; } } } } constexpr precalc_number_to_string; template<std::size_t buf_size = IO_BUFFER_SIZE, bool debug = false> class Printer { private: template<class, bool = debug, class = void> struct has_print : std::false_type {}; template<class T> struct has_print<T, false, decltype(std::declval<T>().print(std::declval<Printer&>()), (void)0)> : std::true_type {}; template<class T> struct has_print<T, true, decltype(std::declval<T>().debug(std::declval<Printer&>()), (void)0)> : std::true_type {}; int fd; std::size_t idx; std::array<char, buf_size> buffer; std::size_t decimal_precision; public: inline void print_char(char c) { #if SHIO_LOCAL buffer[idx++] = c; if (idx == buf_size) flush(); #else if IF_CONSTEXPR (!debug) { buffer[idx++] = c; if (idx == buf_size) flush(); } #endif } inline void flush() { idx = write(fd, buffer.begin(), idx); idx = 0; } Printer(int fd) : fd(fd), idx(0), decimal_precision(16) {} Printer(FILE* fp) : fd(fileno(fp)), idx(0), decimal_precision(16) {} ~Printer() { flush(); } void set_decimal_precision(std::size_t decimal_precision) { this->decimal_precision = decimal_precision; } void print(char c) { if IF_CONSTEXPR (debug) print_char('\''); print_char(c); if IF_CONSTEXPR (debug) print_char('\''); } void print(bool b) { print_char((char)(b + '0')); } void print(const char* a) { if IF_CONSTEXPR (debug) print_char('"'); for (; *a != '\0'; ++a) print_char(*a); if IF_CONSTEXPR (debug) print_char('"'); } template<std::size_t len> void print(const char (&a)[len]) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } void print(const std::string& a) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } template<std::size_t len> void print(const std::bitset<len>& a) { rrep (i, len) print_char((char)(a[i] + '0')); } template<class T, typename std::enable_if<is_int<T>::value && !has_print<T>::value>::type* = nullptr> void print(T a) { if (!a) { print_char('0'); return; } if IF_CONSTEXPR (is_signed_int<T>::value) { if (a < 0) { print_char('-'); using U = typename make_unsigned_int<T>::type; print(static_cast<U>(-static_cast<U>(a))); return; } } if (idx + 40 >= buf_size) flush(); static char s[40]; int t = 40; while (a >= 10000) { int i = a % 10000; a /= 10000; t -= 4; std::memcpy(s + t, precalc_number_to_string.buf[i], 4); } if (a >= 1000) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a], 4); idx += 4; } else if (a >= 100) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a] + 1, 3); idx += 3; } else if (a >= 10) { std::memcpy(buffer.begin() + idx, precalc_number_to_string.buf[a] + 2, 2); idx += 2; } else { buffer[idx++] = '0' | a; } std::memcpy(buffer.begin() + idx, s + t, 40 - t); idx += 40 - t; } template<class T, typename std::enable_if<std::is_floating_point<T>::value && !has_print<T>::value>::type* = nullptr> void print(T a) { if (a == std::numeric_limits<T>::infinity()) { print("inf"); return; } if (a == -std::numeric_limits<T>::infinity()) { print("-inf"); return; } if (std::isnan(a)) { print("nan"); return; } if (a < 0) { print_char('-'); a = -a; } T b = a; if (b < 1) { print_char('0'); } else { std::string s; while (b >= 1) { s += (char)('0' + (int)std::fmod(b, 10.0)); b /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } print_char('.'); rep (decimal_precision) { a *= 10; print_char((char)('0' + (int)std::fmod(a, 10.0))); } } private: template<std::size_t i, class... Args> void print(const std::tuple<Args...>& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(std::get<i>(a)); print<i + 1, Args...>(a); } } public: template<class... Args> void print(const std::tuple<Args...>& a) { if IF_CONSTEXPR (debug) print_char('('); if IF_CONSTEXPR (sizeof...(Args) != 0) print(std::get<0>(a)); print<1, Args...>(a); if IF_CONSTEXPR (debug) print_char(')'); } template<class T, class U> void print(const std::pair<T, U>& a) { if IF_CONSTEXPR (debug) print_char('('); print(a.first); if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(a.second); if IF_CONSTEXPR (debug) print_char(')'); } template<class T, typename std::enable_if<is_range<T>::value && !has_print<T>::value>::type* = nullptr> void print(const T& a) { if IF_CONSTEXPR (debug) print_char('{'); for (auto i = std::begin(a); i != std::end(a); ++i) { if (i != std::begin(a)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); } print(*i); } if IF_CONSTEXPR (debug) print_char('}'); } template<class T, typename std::enable_if<has_print<T>::value && !debug>::type* = nullptr> void print(const T& a) { a.print(*this); } template<class T, typename std::enable_if<has_print<T>::value && debug>::type* = nullptr> void print(const T& a) { a.debug(*this); } void operator()() {} template<class Head, class... Args> void operator()(const Head& head, const Args&... args) { print(head); operator()(args...); } template<class T> Printer& operator<<(const T& a) { print(a); return *this; } Printer& operator<<(Printer& (*pf)(Printer&)) { return pf(*this); } }; template<std::size_t buf_size, bool debug> Printer<buf_size, debug>& endl(Printer<buf_size, debug>& pr) { pr.print_char('\n'); pr.flush(); return pr; } template<std::size_t buf_size, bool debug> Printer<buf_size, debug>& flush(Printer<buf_size, debug>& pr) { pr.flush(); return pr; } struct SetPrec { int n; template<class Pr> void print(Pr& pr) const { pr.set_decimal_precision(n); } template<class Pr> void debug(Pr& pr) const { pr.set_decimal_precision(n); } }; SetPrec setprec(int n) { return SetPrec{n}; }; Printer<> print(1), eprint(2); void prints() { print.print_char('\n'); } template<class T> auto prints(const T& v) -> decltype(print << v, (void)0) { print << v; print.print_char('\n'); } template<class Head, class... Tail> auto prints(const Head& head, const Tail&... tail) -> decltype(print << head, (void)0) { print << head; print.print_char(' '); prints(tail...); } Printer<IO_BUFFER_SIZE, true> debug(1), edebug(2); void debugs() { debug.print_char('\n'); } template<class T> auto debugs(const T& v) -> decltype(debug << v, (void)0) { debug << v; debug.print_char('\n'); } template<class Head, class... Tail> auto debugs(const Head& head, const Tail&... tail) -> decltype(debug << head, (void)0) { debug << head; debug.print_char(' '); debugs(tail...); } #line 2 "template/bitop.hpp" #line 6 "template/bitop.hpp" namespace bitop { #define KTH_BIT(b, k) (((b) >> (k)) & 1) #define POW2(k) (1ull << (k)) inline ull next_combination(int n, ull x) { if (n == 0) return 1; ull a = x & -x; ull b = x + a; return (x & ~b) / a >> 1 | b; } #define rep_comb(i, n, k) \ for (ull i = (1ull << (k)) - 1; i < (1ull << (n)); \ i = bitop::next_combination((n), i)) inline constexpr int msb(ull x) { int res = x ? 0 : -1; if (x & 0xFFFFFFFF00000000) x &= 0xFFFFFFFF00000000, res += 32; if (x & 0xFFFF0000FFFF0000) x &= 0xFFFF0000FFFF0000, res += 16; if (x & 0xFF00FF00FF00FF00) x &= 0xFF00FF00FF00FF00, res += 8; if (x & 0xF0F0F0F0F0F0F0F0) x &= 0xF0F0F0F0F0F0F0F0, res += 4; if (x & 0xCCCCCCCCCCCCCCCC) x &= 0xCCCCCCCCCCCCCCCC, res += 2; return res + ((x & 0xAAAAAAAAAAAAAAAA) ? 1 : 0); } inline constexpr int ceil_log2(ull x) { return x ? msb(x - 1) + 1 : 0; } inline constexpr ull reverse(ull x) { x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1); x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2); x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4); x = ((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8); x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16); return (x >> 32) | (x << 32); } inline constexpr ull reverse(ull x, int n) { return reverse(x) >> (64 - n); } } // namespace bitop inline constexpr int popcnt(ull x) noexcept { #if __cplusplus >= 202002L return std::popcount(x); #endif x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } #line 2 "template/func.hpp" #line 6 "template/func.hpp" template<class T, class U, class Comp = std::less<>> inline constexpr bool chmin(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(b, a))) { return cmp(b, a) ? a = b, true : false; } template<class T, class U, class Comp = std::less<>> inline constexpr bool chmax(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(a, b))) { return cmp(a, b) ? a = b, true : false; } inline constexpr ll gcd(ll a, ll b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b) { const ll c = a; a = b; b = c % b; } return a; } inline constexpr ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } inline constexpr bool is_prime(ll N) { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector<ll> prime_factor(ll N) { std::vector<ll> res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline constexpr ll my_pow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline constexpr ll mod_pow(ll a, ll b, ll mod) { assert(mod > 0); if (mod == 1) return 0; a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } inline PLL extGCD(ll a, ll b) { const ll n = a, m = b; ll x = 1, y = 0, u = 0, v = 1; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); std::swap(y -= t * v, v); } if (x < 0) { x += m; y -= n; } return {x, y}; } inline ll mod_inv(ll a, ll mod) { ll b = mod; ll x = 1, u = 0; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); } if (x < 0) x += mod; assert(a == 1); return x; } #line 2 "template/util.hpp" #line 6 "template/util.hpp" template<class F> class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward<F>(f_)) {} template<class... Args> constexpr auto operator()(Args&&... args) -> decltype(f(*this, std::forward<Args>(args)...)) { return f(*this, std::forward<Args>(args)...); } }; template<class F> inline constexpr RecLambda<F> rec_lambda(F&& f) { return RecLambda<F>(std::forward<F>(f)); } template<class Head, class... Tail> struct multi_dim_vector { using type = std::vector<typename multi_dim_vector<Tail...>::type>; }; template<class T> struct multi_dim_vector<T> { using type = T; }; template<class T, class Arg> constexpr std::vector<T> make_vec(int n, Arg&& arg) { return std::vector<T>(n, std::forward<Arg>(arg)); } template<class T, class... Args> constexpr typename multi_dim_vector<Args..., T>::type make_vec(int n, Args&&... args) { return typename multi_dim_vector<Args..., T>::type( n, make_vec<T>(std::forward<Args>(args)...)); } template<class T, class Comp = std::less<T>> class compressor { private: std::vector<T> dat; Comp cmp; bool sorted = false; public: compressor() : compressor(Comp()) {} compressor(const Comp& cmp) : cmp(cmp) {} compressor(const std::vector<T>& vec, bool f = false, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) { if (f) build(); } compressor(std::vector<T>&& vec, bool f = false, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) { if (f) build(); } compressor(std::initializer_list<T> il, bool f = false, const Comp& cmp = Comp()) : dat(all(il)), cmp(cmp) { if (f) build(); } void reserve(int n) { assert(!sorted); dat.reserve(n); } void push_back(const T& v) { assert(!sorted); dat.push_back(v); } void push_back(T&& v) { assert(!sorted); dat.push_back(std::move(v)); } template<class... Args> void emplace_back(Args&&... args) { assert(!sorted); dat.emplace_back(std::forward<Args>(args)...); } void push(const std::vector<T>& vec) { assert(!sorted); const int n = dat.size(); dat.resize(n + vec.size()); rep (i, vec.size()) dat[n + i] = vec[i]; } int build() { assert(!sorted); sorted = true; std::sort(all(dat), cmp); dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool { return !cmp(a, b) && !cmp(b, a); }), dat.end()); return dat.size(); } const T& operator[](int k) const& { assert(sorted); assert(0 <= k && k < (int)dat.size()); return dat[k]; } int get(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); assert(itr != dat.end() && !cmp(val, *itr)); return itr - dat.begin(); } int lower_bound(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); return itr - dat.begin(); } int upper_bound(const T& val) const { assert(sorted); auto itr = std::upper_bound(all(dat), val, cmp); return itr - dat.begin(); } bool contains(const T& val) const { assert(sorted); return std::binary_search(all(dat), val, cmp); } std::vector<int> pressed(const std::vector<T>& vec) const { assert(sorted); std::vector<int> res(vec.size()); rep (i, vec.size()) res[i] = get(vec[i]); return res; } void press(std::vector<T>& vec) const { assert(sorted); for (auto&& i : vec) i = get(i); } int size() const { assert(sorted); return dat.size(); } }; #line 2 "other/monoid.hpp" #line 4 "other/monoid.hpp" namespace Monoid { template<class M, class = void> class has_value_type : public std::false_type {}; template<class M> class has_value_type<M, decltype((void)std::declval<typename M::value_type>())> : public std::true_type {}; template<class M, class = void> class has_op : public std::false_type {}; template<class M> class has_op<M, decltype((void)M::op)> : public std::true_type {}; template<class M, class = void> class has_id : public std::false_type {}; template<class M> class has_id<M, decltype((void)M::id)> : public std::true_type {}; template<class M, class = void> class has_inv : public std::false_type {}; template<class M> class has_inv<M, decltype((void)M::inv)> : public std::true_type {}; template<class M, class = void> class has_get_inv : public std::false_type {}; template<class M> class has_get_inv<M, decltype((void)M::get_inv)> : public std::true_type {}; template<class M, class = void> class has_init : public std::false_type {}; template<class M> class has_init<M, decltype((void)M::init(0, 0))> : public std::true_type {}; template<class A, class = void> class has_mul_op : public std::false_type {}; template<class A> class has_mul_op<A, decltype((void)A::mul_op)> : public std::true_type {}; template<class T, class = void> class is_semigroup : public std::false_type {}; template<class T> class is_semigroup<T, decltype(std::declval<typename T::value_type>(), (void)T::op)> : public std::true_type {}; template<class T, class = void> class is_monoid : public std::false_type {}; template<class T> class is_monoid<T, decltype(std::declval<typename T::value_type>(), (void)T::op, (void)T::id)> : public std::true_type {}; template<class T, class = void> class is_group : public std::false_type {}; template<class T> class is_group<T, decltype(std::declval<typename T::value_type>(), (void)T::op, (void)T::id, (void)T::get_inv)> : public std::true_type {}; template<class T, class = void> class is_action : public std::false_type {}; template<class T> class is_action<T, typename std::enable_if<is_monoid<typename T::M>::value && is_semigroup<typename T::E>::value && (has_op<T>::value || has_mul_op<T>::value)>::type> : public std::true_type {}; template<class T, class = void> class is_distributable_action : public std::false_type {}; template<class T> class is_distributable_action< T, typename std::enable_if<is_action<T>::value && !has_mul_op<T>::value>::type> : public std::true_type {}; template<class T> struct Sum { using value_type = T; static constexpr T op(const T& a, const T& b) { return a + b; } static constexpr T id() { return T{0}; } static constexpr T inv(const T& a, const T& b) { return a - b; } static constexpr T get_inv(const T& a) { return -a; } }; template<class T, int i = -1> struct Min { using value_type = T; static T max_value; static T op(const T& a, const T& b) { return a < b ? a : b; } static T id() { return max_value; } }; template<class T> struct Min<T, -1> { using value_type = T; static constexpr T op(const T& a, const T& b) { return a < b ? a : b; } static constexpr T id() { return infinity<T>::value; } }; template<class T> struct Min<T, -2> { using value_type = T; static constexpr T op(const T& a, const T& b) { return a < b ? a : b; } static constexpr T id() { return infinity<T>::max; } }; template<class T, int id> T Min<T, id>::max_value; template<class T, int i = -1> struct Max { using value_type = T; static T min_value; static T op(const T& a, const T& b) { return a > b ? a : b; } static T id() { return min_value; } }; template<class T> struct Max<T, -1> { using value_type = T; static constexpr T op(const T& a, const T& b) { return a > b ? a : b; } static constexpr T id() { return infinity<T>::mvalue; } }; template<class T> struct Max<T, -2> { using value_type = T; static constexpr T op(const T& a, const T& b) { return a > b ? a : b; } static constexpr T id() { return infinity<T>::min; } }; template<class T> struct Assign { using value_type = T; static constexpr T op(const T&, const T& b) { return b; } }; template<class T, int id = -1> struct AssignMin { using M = Min<T, id>; using E = Assign<T>; static constexpr T op(const T& a, const T&) { return a; } }; template<class T, int id = -1> struct AssignMax { using M = Max<T, id>; using E = Assign<T>; static constexpr T op(const T& a, const T&) { return a; } }; template<class T> struct AssignSum { using M = Sum<T>; using E = Assign<T>; static constexpr T mul_op(const T& a, int b, const T&) { return a * b; } }; template<class T, int id = -1> struct AddMin { using M = Min<T, id>; using E = Sum<T>; static constexpr T op(const T& a, const T& b) { return b + a; } }; template<class T, int id = -1> struct AddMax { using M = Max<T, id>; using E = Sum<T>; static constexpr T op(const T& a, const T& b) { return b + a; } }; template<class T> struct AddSum { using M = Sum<T>; using E = Sum<T>; static constexpr T mul_op(const T& a, int b, const T& c) { return c + a * b; } }; template<class T, int id = -1> struct ChminMin { using M = Min<T, id>; using E = Min<T>; static constexpr T op(const T& a, const T& b) { return std::min(b, a); } }; template<class T, int id = -1> struct ChminMax { using M = Max<T, id>; using E = Min<T>; static constexpr T op(const T& a, const T& b) { return std::min(b, a); } }; template<class T, int id = -1> struct ChmaxMin { using M = Min<T, id>; using E = Max<T>; static constexpr T op(const T& a, const T& b) { return std::max(b, a); } }; template<class T, int id = -1> struct ChmaxMax { using M = Max<T, id>; using E = Max<T>; static constexpr T op(const T& a, const T& b) { return std::max(b, a); } }; template<class M> struct ReverseMonoid { using value_type = typename M::value_type; static value_type op(const value_type& a, const value_type& b) { return M::op(b, a); } static value_type id() { static_assert(has_id<M>::value, "id is not defined"); return M::id(); } static value_type inv(const value_type& a, const value_type& b) { static_assert(has_inv<M>::value, "inv is not defined"); return M::inv(b, a); } static value_type get_inv(const value_type& a) { static_assert(has_get_inv<M>::value, "get_inv is not defined"); return M::get_inv(a); } }; template<class E_> struct MakeAction { using M = E_; using E = E_; using T = typename E_::value_type; static T op(const T& a, const T& b) { return E_::op(b, a); } }; } // namespace Monoid #line 2 "random/Random.hpp" #line 4 "random/Random.hpp" template<class Engine> class Random { private: Engine rnd; public: using result_type = typename Engine::result_type; Random() : Random(std::random_device{}()) {} Random(result_type seed) : rnd(seed) {} result_type operator()() { return rnd(); } result_type min() const { return rnd.min(); } result_type max() const { return rnd.max(); } template<class IntType = ll> IntType uniform(IntType l, IntType r) { static_assert(std::is_integral<IntType>::value, "template argument must be an integral type"); assert(l <= r); return std::uniform_int_distribution<IntType>{l, r}(rnd); } template<class RealType = double> RealType uniform_real(RealType l, RealType r) { static_assert(std::is_floating_point<RealType>::value, "template argument must be an floating point type"); assert(l <= r); return std::uniform_real_distribution<RealType>{l, r}(rnd); } bool uniform_bool() { return uniform<int>(0, 1) == 1; } template<class T = ll> std::pair<T, T> uniform_pair(T l, T r) { assert(l < r); T a, b; do { a = uniform<T>(l, r); b = uniform<T>(l, r); } while (a == b); if (a > b) swap(a, b); return {a, b}; } template<class T = ll> std::vector<T> choice(int n, T l, T r) { assert(l <= r); assert(T(n) <= (r - l + 1)); std::set<T> res; while ((int)res.size() < n) res.insert(uniform<T>(l, r)); return {res.begin(), res.end()}; } template<class Iter> void shuffle(const Iter& first, const Iter& last) { std::shuffle(first, last, rnd); } template<class T> std::vector<T> permutation(T n) { std::vector<T> res(n); rep (i, n) res[i] = i; shuffle(all(res)); return res; } template<class T = ll> std::vector<T> choice_shuffle(int n, T l, T r, bool sorted = true) { assert(l <= r); assert(T(n) <= (r - l + 1)); std::vector<T> res(r - l + 1); rep (i, l, r + 1) res[i - l] = i; shuffle(all(res)); res.erase(res.begin() + n, res.end()); if (sorted) sort(all(res)); return res; } }; using Random32 = Random<std::mt19937>; Random32 rand32; using Random64 = Random<std::mt19937_64>; Random64 rand64; /** * @brief Random * @docs docs/random/Random.md */ #line 6 "data-struct/other/SkipList.hpp" template<class A, class Rand = Random32> class SkipList { private: using M = typename A::M; using E = typename A::E; using T = typename M::value_type; using U = typename E::value_type; static inline int get_level(Rand& rnd) { int level = 1; while ((rnd() & 1) == 0) ++level; return level; } struct node; using node_ptr = node*; struct next_node { node_ptr node; int dist; T sm; U lazy; bool lazyflag; next_node(node_ptr n, int d, const T& s) : node(n), dist(d), sm(s), lazyflag(false) {} next_node(node_ptr n, int d, const T& s, const U& l) : node(n), dist(d), sm(s), lazy(l), lazyflag(true) {} }; struct node { std::vector<next_node> nxt; std::vector<node_ptr> prv; int level() const { assert(nxt.size() == prv.size()); return nxt.size(); } node(Rand& rnd) : node(get_level(rnd)) {} node(int lev) : nxt(lev, {nullptr, 1, M::id()}), prv(lev, nullptr) {} }; using nodepair = std::pair<node_ptr, node_ptr>; Rand rnd; nodepair sl; template<bool AlwaysTrue = true, typename std::enable_if<!Monoid::has_mul_op<A>::value && AlwaysTrue>::type* = nullptr> static inline T Aop(const U& a, const T& b, int) { return A::op(a, b); } template<bool AlwaysTrue = true, typename std::enable_if<Monoid::has_mul_op<A>::value && AlwaysTrue>::type* = nullptr> static inline T Aop(const U& a, const T& b, int c) { return A::mul_op(a, c, b); } static inline void all_apply(const node_ptr& nd, int k, const U& x) { assert(0 <= k && k < nd->level()); nd->nxt[k].sm = Aop(x, nd->nxt[k].sm, nd->nxt[k].dist); if (k != 0) { if (nd->nxt[k].lazyflag) { nd->nxt[k].lazy = E::op(nd->nxt[k].lazy, x); } else { nd->nxt[k].lazy = x; nd->nxt[k].lazyflag = true; } } } static inline void eval(const node_ptr& nd, int k) { assert(0 <= k && k < nd->level()); if (k != 0 && nd->nxt[k].lazyflag) { for (auto ptr = nd; ptr != nd->nxt[k].node; ptr = ptr->nxt[k - 1].node) { all_apply(ptr, k - 1, nd->nxt[k].lazy); } nd->nxt[k].lazyflag = false; } } static inline void all_eval(const nodepair& sl, int k) { auto nd = sl.first; int cnt = 0; rrep (i, sl.first->level(), 1) { while (cnt + nd->nxt[i].dist <= k) { cnt += nd->nxt[i].dist; nd = nd->nxt[i].node; } eval(nd, i); } } static inline void calc(const node_ptr& l, int k) { assert(1 <= k && k < l->level()); l->nxt[k].sm = l->nxt[k - 1].sm; for (node_ptr ptr = l->nxt[k - 1].node; ptr != l->nxt[k].node; ptr = ptr->nxt[k - 1].node) { l->nxt[k].sm = M::op(l->nxt[k].sm, ptr->nxt[k - 1].sm); } } static inline void all_calc(const nodepair& sl, int k) { auto nd = sl.first; int cnt = 0; std::vector<node_ptr> nds(sl.first->level()); rrep (i, sl.first->level(), 1) { while (cnt + nd->nxt[i].dist <= k) { cnt += nd->nxt[i].dist; nd = nd->nxt[i].node; } nds[i] = nd; } rep (i, 1, sl.first->level()) calc(nds[i], i); } static void match_level(nodepair& lhs, nodepair& rhs) { const int llv = lhs.first->level(), rlv = rhs.second->level(); if (llv < rlv) { eval(lhs.first, llv - 1); lhs.first->prv.resize(rlv, {lhs.first->prv.back()}); lhs.first->nxt.resize(rlv, {lhs.first->nxt.back()}); lhs.second->prv.resize(rlv, {lhs.second->prv.back()}); lhs.second->nxt.resize(rlv, {lhs.second->nxt.back()}); } else if (llv > rlv) { eval(rhs.second, rlv - 1); rhs.first->prv.resize(llv, {rhs.first->prv.back()}); rhs.first->nxt.resize(llv, {rhs.first->nxt.back()}); rhs.second->prv.resize(llv, {rhs.second->prv.back()}); rhs.second->nxt.resize(llv, {rhs.second->nxt.back()}); } } static nodepair merge(nodepair&& lhs, nodepair&& rhs, Rand& rnd) { assert(lhs.first != rhs.first); assert(lhs.second != rhs.second); if (lhs.first == lhs.second) { delete lhs.first; auto res = std::move(rhs); lhs = rhs = {nullptr, nullptr}; return res; } if (rhs.first == rhs.second) { delete rhs.first; auto res = std::move(lhs); lhs = rhs = {nullptr, nullptr}; return res; } match_level(lhs, rhs); rep (i, lhs.first->level()) { auto&& l = lhs.second->prv[i]; auto&& r = rhs.first; l->nxt[i].node = r; r->prv[i] = std::move(l); } delete lhs.second; const int lev = get_level(rnd); while (lev < rhs.first->level()) { const int h = rhs.first->level(); const auto lp = std::move(rhs.first->prv.back()); const auto l = lp->nxt[h - 1]; const auto r = std::move(rhs.first->nxt.back()); eval(lp, h - 1); eval(rhs.first, h - 1); rhs.first->prv.pop_back(); rhs.first->nxt.pop_back(); lp->nxt[h - 1] = {r.node, l.dist + r.dist, M::op(l.sm, r.sm)}; r.node->prv[h - 1] = lp; } if (lev >= lhs.first->level()) { eval(lhs.first, lhs.first->level() - 1); eval(rhs.first, lhs.first->level() - 1); lhs.first->prv.resize(lev + 1, {lhs.first->prv.back()}); lhs.first->nxt.resize(lev, {lhs.first->nxt.back()}); rhs.first->prv.resize(lev, {rhs.first->prv.back()}); rhs.first->nxt.resize(lev, {rhs.first->nxt.back()}); rhs.second->prv.resize(lev, {rhs.second->prv.back()}); rhs.second->nxt.resize(lev + 1, {rhs.second->nxt.back()}); const auto& lp = rhs.first->prv.back(); const auto& l = lp->nxt[lev - 1]; const auto& r = rhs.first->nxt.back(); lhs.first->nxt.emplace_back(rhs.second, l.dist + r.dist, M::op(l.sm, r.sm)); rhs.second->prv.push_back(lhs.first); } nodepair res{lhs.first, rhs.second}; lhs = rhs = {nullptr, nullptr}; return res; } static std::pair<nodepair, nodepair> split(nodepair&& sl, int k) { const int n = sl.first->nxt.back().dist; assert(0 <= k && k <= n); if (n == 0 || k == 0) { node_ptr np = new node(1); auto res = std::make_pair(nodepair{np, np}, std::move(sl)); sl = {nullptr, nullptr}; return res; } if (k == n) { node_ptr np = new node(1); auto res = std::make_pair(std::move(sl), nodepair{np, np}); sl = {nullptr, nullptr}; return res; } const int h = sl.first->level(); std::vector<node_ptr> lft(h); std::vector<int> idx(h); lft[h - 1] = sl.first; idx[h - 1] = 0; rrep (i, h - 1) { lft[i] = lft[i + 1]; idx[i] = idx[i + 1]; while (idx[i] + lft[i]->nxt[i].dist < k) { idx[i] += lft[i]->nxt[i].dist; lft[i] = lft[i]->nxt[i].node; } } rrep (i, h, 1) eval(lft[i], i); node_ptr npl = new node(h); node_ptr npr = lft[0]->nxt[0].node; rep (i, h) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; const int d = l->nxt[i].dist; l->nxt[i] = {npl, k - idx[i], l->nxt[i].sm}; npl->prv[i] = l; if (i != 0) calc(l, i); if (npr == r) { r->prv[i] = nullptr; } else { npr->prv.push_back(nullptr); npr->nxt.emplace_back(r, d + idx[i] - k, M::id()); r->prv[i] = npr; calc(npr, i); } } auto res = std::make_pair(nodepair{sl.first, npl}, nodepair{npr, sl.second}); sl = {nullptr, nullptr}; return res; } SkipList(const nodepair& sl, const Rand& rnd) : rnd(rnd), sl(sl) {} SkipList(nodepair&& sl, const Rand& rnd) : rnd(rnd), sl(std::move(sl)) {} static node_ptr get_ptr(const nodepair& sl, int k) { int cnt = 0; node_ptr nw = sl.first; rrep (i, sl.first->level()) { while (cnt + nw->nxt[i].dist <= k) { cnt += nw->nxt[i].dist; nw = nw->nxt[i].node; } } return nw; } public: SkipList() : SkipList(Rand()) {} SkipList(const Rand& rnd) : rnd(rnd) { sl.first = sl.second = new node(1); } SkipList(const std::vector<T>& v, const Rand& rnd = Rand()) : rnd(rnd) { init(v); } SkipList(const SkipList& other) : SkipList(other.get_data(), other.rnd) {} SkipList(SkipList&& other) : rnd(other.rnd), sl(std::move(other.sl)) { other.sl = {nullptr, nullptr}; } ~SkipList() { for (node_ptr ptr = sl.first; ptr;) { node_ptr nxt = ptr->nxt[0].node; delete ptr; ptr = nxt; } sl = {nullptr, nullptr}; } SkipList& operator=(const SkipList& other) { if (this == &other) return *this; init(other.get_data()); return *this; } SkipList& operator=(SkipList&& other) { if (this == &other) return *this; sl = std::move(other.sl); other.sl = {nullptr, nullptr}; return *this; } void init(const std::vector<T>& v) { if (sl.first) { for (node_ptr ptr = sl.first; ptr;) { node_ptr nxt = ptr->nxt[0].node; delete ptr; ptr = nxt; } } const int n = v.size(); std::vector<int> lev(n + 1); rep (i, 1, n) lev[i] = get_level(rnd); lev[0] = lev[n] = *max_element(lev.begin() + 1, lev.end() - 1) + 1; std::vector<node_ptr> nd(n + 1); rep (i, n + 1) nd[i] = new node(lev[i]); rep (i, n) { nd[i]->nxt[0] = {nd[i + 1], 1, v[i]}; nd[i + 1]->prv[0] = nd[i]; } nd[0]->prv[0] = nullptr; nd[n]->nxt[0] = {nullptr, 1, M::id()}; rep (i, 1, lev[0]) { std::vector<int> idx; rep (j, n + 1) { if (lev[j] > i) idx.push_back(j); } const int m = idx.size(); rep (j, m - 1) { nd[idx[j]]->nxt[i] = {nd[idx[j + 1]], idx[j + 1] - idx[j], nd[idx[j]]->nxt[i - 1].sm}; for (node_ptr ptr = nd[idx[j]]->nxt[i - 1].node; ptr != nd[idx[j + 1]]; ptr = ptr->nxt[i - 1].node) { nd[idx[j]]->nxt[i].sm = M::op(nd[idx[j]]->nxt[i].sm, ptr->nxt[i - 1].sm); } nd[idx[j + 1]]->prv[i] = nd[idx[j]]; } nd[idx[0]]->prv[i] = nullptr; nd[idx[m - 1]]->nxt[i] = {nullptr, 1, M::id()}; } sl = {nd[0], nd[n]}; } int size() const { assert(sl.first); return sl.first == sl.second ? 0 : sl.first->nxt.back().dist; } bool empty() const { return sl.first == sl.second; } void insert(int k, const T& x) { const int n = size(); assert(0 <= k && k <= n); if (n == 0) { delete sl.first; sl.first = new node(1); sl.second = new node(1); sl.first->nxt[0] = {sl.second, 1, x}; sl.second->prv[0] = sl.first; return; } if (k == 0) { /* if (lev < sl.first->level()) { node_ptr ptr = new node(lev); rep (i, lev) { ptr->nxt[i] = sl.first->nxt[i]; sl.first->nxt[i].node->prv[i] = ptr; sl.first->nxt[i] = {ptr, 1, x}; ptr->prv[i] = sl.first; } rep (i, lev, sl.first->level()) { ++sl.first->nxt[i].dist; sl.first->nxt[i].sm = M::op(x, sl.first->nxt[i].sm); } } else { node_ptr ptr = new node(lev); rep (i, sl.first->level()) { ptr->nxt[i] = sl.first->nxt[i]; sl.first->nxt[i].node->prv[i] = ptr; sl.first->nxt[i] = {ptr, 1, x}; ptr->prv[i] = sl.first; } rep (i, sl.first->level(), lev) { sl.first->nxt[i] = sl.first->nxt[i - 1]; sl.first->prv[i] = sl.first->prv[i - 1]; } sl.first->prv.resize(lev + 1, {sl.first->prv.back()}); sl.first->nxt.resize(lev + 1, {sl.first->nxt.back()}); sl.second->prv.resize(lev + 1, {sl.second->prv.back()}); sl.second->nxt.resize(lev + 1, {sl.second->nxt.back()}); sl.first->nxt.back() = {sl.second, sl.first->nxt.back().dist + 1, M::op(x, sl.first->nxt.back().sm)}; sl.second->prv.back() = sl.first; } */ nodepair p{new node(1), new node(1)}; p.first->nxt[0] = {p.second, 1, x}; p.second->prv[0] = p.first; sl = merge(std::move(p), std::move(sl), rnd); return; } const int h = sl.first->level(); std::vector<node_ptr> lft(h); std::vector<int> idx(h); lft[h - 1] = sl.first; idx[h - 1] = 0; rrep (i, h - 1) { lft[i] = lft[i + 1]; idx[i] = idx[i + 1]; while (idx[i] + lft[i]->nxt[i].dist < k) { idx[i] += lft[i]->nxt[i].dist; lft[i] = lft[i]->nxt[i].node; } } rrep (i, h, 1) eval(lft[i], i); const int lev = get_level(rnd); node_ptr np = new node(lev); if (lev < h) { rep (i, lev) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; np->nxt[i] = {r, idx[i] + l->nxt[i].dist - k + 1, x}; r->prv[i] = np; l->nxt[i] = {np, k - idx[i], l->nxt[i].sm}; np->prv[i] = l; } rep (i, lev, h) ++lft[i]->nxt[i].dist; rep (i, 1, h) { calc(lft[i], i); if (i < lev) calc(lft[i]->nxt[i].node, i); } } else { rep (i, h) { const auto l = lft[i]; const auto r = lft[i]->nxt[i].node; np->nxt[i] = {r, idx[i] + l->nxt[i].dist - k + 1, x}; r->prv[i] = np; l->nxt[i] = {np, k - idx[i], l->nxt[i].sm}; np->prv[i] = l; } rep (i, 1, h) { calc(lft[i], i); if (i < lev) calc(lft[i]->nxt[i].node, i); } sl.first->prv.resize(lev + 1, {sl.first->prv.back()}); sl.first->nxt.resize(lev + 1, {sl.second, n + 1, x}); sl.second->prv.resize(lev + 1, sl.first); sl.second->nxt.resize(lev + 1, {sl.second->nxt.back()}); rep (i, h, lev) { sl.first->nxt[i] = {np, k, x}; np->prv[i] = sl.first; np->nxt[i] = {sl.second, n - k + 1, x}; sl.second->prv[i] = np; calc(sl.first, i); calc(np, i); } calc(sl.first, lev); } } void erase(int k) { const int n = size(); assert(0 <= k && k < n); if (n == 1) { delete sl.first; delete sl.second; sl.first = sl.second = new node(1); return; } if (k == 0) { all_eval(sl, 0); all_eval(sl, 1); rep (i, sl.first->level()) { if (sl.first->nxt[i].dist == 1) { const auto l = sl.first; const auto m = l->nxt[i].node; const auto r = m->nxt[i].node; l->nxt[i] = {r, l->nxt[i].dist + m->nxt[i].dist - 1, m->nxt[i].sm}; r->prv[i] = l; } else { sl.first->nxt[i].dist--; } } rep (i, 1, sl.first->level()) calc(sl.first, i); return; } all_eval(sl, k - 1); all_eval(sl, k); node_ptr np = sl.first; int cnt = 0; rrep (i, sl.first->level()) { while (cnt + np->nxt[i].dist <= k) { cnt += np->nxt[i].dist; np = np->nxt[i].node; } if (cnt == k) { const auto l = np->prv[i]; const auto r = np->nxt[i].node; r->prv[i] = l; l->nxt[i] = {r, l->nxt[i].dist + np->nxt[i].dist - 1, l->nxt[i].sm}; } else { np->nxt[i].dist--; } } delete np; all_calc(sl, k - 1); } T prod(int l, int r) const { assert(0 <= l && l <= r && r <= size()); all_eval(sl, l); all_eval(sl, r - 1); auto np = get_ptr(sl, l); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (l + np->nxt[t].dist > r) break; l += np->nxt[t].dist; sm = M::op(sm, np->nxt[t].sm); np = np->nxt[t].node; } } return sm; } T all_prod() const { return sl.first->nxt.back().sm; } T get(int k) const { assert(0 <= k && k < size()); all_eval(sl, k); return get_ptr(sl, k)->nxt[0].sm; } void apply(int l, int r, const U& x) { assert(0 <= l && l <= r && r <= size()); all_eval(sl, l); all_eval(sl, r - 1); auto np = get_ptr(sl, l); int idx = l; rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (idx + np->nxt[t].dist > r) break; idx += np->nxt[t].dist; all_apply(np, t, x); np = np->nxt[t].node; } } all_eval(sl, l); all_eval(sl, r - 1); all_calc(sl, l); all_calc(sl, r - 1); } template<class Upd> void update(int k, const Upd& upd) { assert(0 <= k && k < size()); all_eval(sl, k); auto nd = get_ptr(sl, k); nd->nxt[0].sm = upd(nd->nxt[0].sm); all_calc(sl, k); } void set(int k, const T& x) { update(k, [&](const T&) { return x; }); } void apply(int k, const U& x) { update(k, [&](const T& sm) { return Aop(x, sm, 1); }); } template<class C> int max_right(int l, const C& cond) const { assert(0 <= l && l <= size()); assert(cond(M::id())); if (l == size()) return size(); all_eval(sl, l); auto np = get_ptr(sl, l); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (t != np->level() - 1) eval(np, t + 1); if (!cond(M::op(sm, np->nxt[t].sm))) break; sm = M::op(sm, np->nxt[t].sm); l += np->nxt[t].dist; np = np->nxt[t].node; if (np == sl.second) return size(); } } return l; } template<class C> int min_left(int r, const C& cond) const { assert(0 <= r && r <= size()); assert(cond(M::id())); if (r == 0) return 0; all_eval(sl, r - 1); auto np = get_ptr(sl, r); T sm = M::id(); rrep (i, sl.first->level()) { while (1) { int t = std::min((int)i, np->level() - 1); if (t != np->level() - 1) eval(np->prv[t], t + 1); if (!cond(M::op(sm, np->prv[t]->nxt[t].sm))) break; sm = M::op(sm, np->prv[t]->nxt[t].sm); r -= np->prv[t]->nxt[t].dist; np = np->prv[t]; if (np == sl.first) return 0; } } return r; } std::vector<T> get_data() const { rrep (i, sl.first->level(), 1) { for (node_ptr ptr = sl.first; ptr != sl.second; ptr = ptr->nxt[i].node) { eval(ptr, i); } } std::vector<T> res; res.reserve(size()); for (node_ptr ptr = sl.first; ptr != sl.second; ptr = ptr->nxt[0].node) { res.push_back(ptr->nxt[0].sm); } return res; } friend SkipList merge(SkipList lhs, SkipList rhs) { return {merge(std::move(lhs.sl), std::move(rhs.sl), lhs.rnd), lhs.rnd}; } friend std::pair<SkipList, SkipList> split(SkipList sl, int k) { auto s = split(std::move(sl.sl), k); return {SkipList{std::move(s.first), sl.rnd}, SkipList{std::move(s.second), sl.rnd}}; } }; /** * @brief SkipList * @docs docs/data-struct/other/SkipList.md */