#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); // Treap by Aeren // T: data type, U: lazy type. // TT: usual addition // UU: combination of lazy (new, old) // UT: lazy to the data (new, old) template <bool HAS_QUERY, bool HAS_UPDATE, bool HAS_FLIP, bool IS_COMMUTATIVE, class T, class U, class F1, class F2, class F3, class F4> struct treap_base { #define ifQ if constexpr (HAS_QUERY) #define ifU if constexpr (HAS_UPDATE) #define ifF if constexpr (HAS_FLIP) #define ifNC if constexpr (!IS_COMMUTATIVE) vector<unsigned int> priority; vector<int> pv, left, right, internal_size, lazy_flip; vector<T> data, subtr_data; vector<U> lazy; F1 TT; // monoid operation (always adjacent) T T_id; // monoid identity F2 UU; // monoid operation (superset, subset) U U_id; // monoid identity F3 UT; // action of U on T (superset, subset) F4 FlipT; treap_base(F1 TT, T T_id, F2 UU, U U_id, F3 UT, F4 FlipT = plus<>()) : TT(TT), T_id(T_id), UU(UU), U_id(U_id), UT(UT), FlipT(FlipT) {} void push(int u) { ifU if (lazy[u] != U_id) { if (auto v = left[u]; ~v) { data[v] = UT(lazy[u], data[v]); lazy[v] = UU(lazy[u], lazy[v]); ifQ subtr_data[v] = UT(lazy[u], subtr_data[v]); } if (auto w = right[u]; ~w) { data[w] = UT(lazy[u], data[w]); lazy[w] = UU(lazy[u], lazy[w]); ifQ subtr_data[w] = UT(lazy[u], subtr_data[w]); } lazy[u] = U_id; } ifF if (lazy_flip[u]) { if (auto v = left[u]; ~v) { swap(left[v], right[v]); lazy_flip[v] ^= 1; ifQ ifNC subtr_data[v] = FlipT(subtr_data[v]); } if (auto w = right[u]; ~w) { swap(left[w], right[w]); lazy_flip[w] ^= 1; ifQ ifNC subtr_data[w] = FlipT(subtr_data[w]); } lazy_flip[u] = false; } } template <bool UPDATE_STRUCTURE = true> void refresh(int u) { if constexpr (UPDATE_STRUCTURE) internal_size[u] = 1; ifQ subtr_data[u] = T_id; if (auto v = left[u]; ~v) { if constexpr (UPDATE_STRUCTURE) { pv[v] = u; internal_size[u] += internal_size[v]; } ifQ { auto x = subtr_data[v]; ifU if (lazy[u] != U_id) x = UT(lazy[u], x); ifF ifNC if (lazy_flip[u]) x = FlipT(x); subtr_data[u] = x; } } ifQ subtr_data[u] = TT(subtr_data[u], data[u]); if (auto w = right[u]; ~w) { if constexpr (UPDATE_STRUCTURE) { pv[w] = u; internal_size[u] += internal_size[w]; } ifQ { auto x = subtr_data[w]; ifU if (lazy[u] != U_id) x = UT(lazy[u], x); ifF ifNC if (lazy_flip[u]) x = FlipT(x); subtr_data[u] = TT(subtr_data[u], x); } } } void heapify(int u) { if (~u) { int v = u; if (~left[u] && priority[left[u]] > priority[v]) v = left[u]; if (~right[u] && priority[right[u]] > priority[v]) v = right[u]; if (u != v) swap(priority[u], priority[v]), heapify(v); } } template <class output_stream> output_stream &print(output_stream &out, int root) { #ifdef LOCAL out << "["; traverse(root, [&](int root) { out << data[root] << ", "; }); out << "\b\b]\n"; #endif return out; } vector<int> dead_node; int new_node(const T &x, int v = -1, int w = -1) { // Among all seeds in range [0, 1769666], // 72718 has the longest sequence of unique numbers, which is of length 362129. static mt19937 rng(72718); int u; if (dead_node.empty()) { u = (int)data.size(); pv.push_back(-1); left.push_back(v); right.push_back(w); priority.push_back(rng()); internal_size.push_back(1); data.push_back(x); ifQ subtr_data.push_back(x); ifU lazy.push_back(U_id); ifF lazy_flip.push_back(0); } else { u = dead_node.back(); dead_node.pop_back(); pv[u] = -1; left[u] = v; right[u] = w; priority[u] = rng(); internal_size[u] = 1; data[u] = x; ifQ subtr_data[u] = x; ifU lazy[u] = U_id; ifF lazy_flip[u] = 0; } heapify(u); refresh(u); return u; } // O(1) int size(int root) const { return ~root ? internal_size[root] : 0; } // Split to [-inf, x), [x, inf) // Data must be sorted < // O(log(n)) pair<int, int> split_by_key(int root, const T &x) { if (!~root) return {-1, -1}; push(root); if (data[root] < x) { auto [a, b] = split_by_key(right[root], x); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_key(left[root], x); left[root] = b, refresh(root); return {a, root}; } } // Split to [-inf, x), [x, inf) // Data must be sorted cmp // O(log(n)) template <class Compare> pair<int, int> split_by_key(int root, const T &x, Compare cmp) { if (!~root) return {-1, -1}; push(root); if (cmp(data[root], x)) { auto [a, b] = split_by_key(right[root], x, cmp); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_key(left[root], x, cmp); left[root] = b, refresh(root); return {a, root}; } } // Split into [0, pos), [pos, size(root)) // O(log(n)) pair<int, int> split_by_order(int root, int pos) { if (!~root) { assert(pos == 0); return {-1, -1}; } push(root); if (size(left[root]) < pos) { auto [a, b] = split_by_order(right[root], pos - size(left[root]) - 1); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_order(left[root], pos); left[root] = b, refresh(root); return {a, root}; } } // Split to [true Segment], [false Segment] // Data must be sorted by pred // O(log(n)) pair<int, int> split_by_pred(int root, auto pred) { if (!~root) return {-1, -1}; push(root); if (pred(root)) { auto [a, b] = split_by_pred(right[root], pred); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_pred(left[root], pred); left[root] = b, refresh(root); return {a, root}; } } // Split into [0, l), [l, r), [r, size(root)) // O(log(n)) array<int, 3> split_to_three(int u, int l, int r) { assert(0 <= l && l <= r && r <= size(u)); if (!~u) return {-1, -1, -1}; int a, b, c; tie(a, b) = split_by_order(u, l); tie(b, c) = split_by_order(b, r - l); return {a, b, c}; } // Split into [0, pos[0]), [pos[0], pos[1]), ..., [pos[size(pos) - 1], size(root)) // O(k * log(n)) vector<int> split(int root, const vector<int> &pos) { assert(is_sorted(pos.begin(), pos.end())); if (pos.empty()) return {root}; assert(0 <= pos.front() && pos.back() <= size(root)); if (!~root) return vector<int>((int)pos.size() + 1, -1); vector<int> res((int)pos.size() + 1); res[0] = root; for (auto i = 0, last = 0; i < (int)pos.size(); ++i) { tie(res[i], res[i + 1]) = split_by_order(res[i], pos[i] - last); last = pos[i]; } return res; } // Append u and v // O(log(n)) int append(int u, int v) { if (!~u || !~v) return ~u ? u : v; push(u); push(v); if (priority[v] < priority[u]) { right[u] = append(right[u], v), refresh(u); return u; } else { left[v] = append(u, left[v]), refresh(v); return v; } } // Append treaps in order // O((list length) * log(n)) int append(const vector<int> &list) { return accumulate(list.begin(), list.end(), -1, [&](int u, int v) { return append(u, v); }); } // Data must be sorted by < // O(log(n)) int insert_node_by_key(int root, int u) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (data[root] < data[u]) right[root] = insert_node_by_key(right[root], u); else left[root] = insert_node_by_key(left[root], u); refresh(root); return root; } auto [a, b] = split_by_key(root, data[u]); left[u] = a, right[u] = b; refresh(u); return u; } // Data must be sorted by < // O(log(n)) int insert_by_key(int root, const T &x) { return insert_node_by_key(root, new_node(x)); } // Data must be sorted by cmp // O(log(n)) template <class Compare> int insert_node_by_key(int root, int u, Compare cmp) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (cmp(data[root], data[u])) right[root] = insert_node_by_key(right[root], u, cmp); else left[root] = insert_node_by_key(left[root], u, cmp); refresh(root); return root; } auto [a, b] = split_by_key(root, data[u], cmp); left[u] = a, right[u] = b; refresh(u); return u; } // Data must be sorted by cmp // O(log(n)) template <class Compare> int insert_by_key(int root, const T &x, Compare cmp) { return insert_node_by_key(root, new_node(x), cmp); } // O(log(n)) int insert_node_by_order(int root, int pos, int u) { if (!~root) { assert(pos == 0); return u; } push(root); if (priority[root] > priority[u]) { if (size(left[root]) < pos) right[root] = insert_node_by_order(right[root], pos - size(left[root]) - 1, u); else left[root] = insert_node_by_order(left[root], pos, u); refresh(root); return root; } auto [a, b] = split_by_order(root, pos); left[u] = a, right[u] = b; refresh(u); return u; } // O(log(n)) int insert_by_order(int root, int pos, const T &x) { return insert_node_by_order(root, pos, new_node(x)); } // Data must be sorted by pred // O(log(n)) int insert_node_by_pred(int root, auto pred, int u) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (pred(data[root])) right[root] = insert_node_by_pred(right[root], pred, u); else left[root] = insert_node_by_pred(left[root], pred, u); refresh(root); return root; } auto [a, b] = split_by_pred(root, pred); left[u] = a, right[u] = b; refresh(u); return u; } // O(log(n)) int insert_by_pred(int root, auto pred, const T &x) { return insert_node_by_pred(root, pred, new_node(x)); } // Erase the smallest element >= x. // Return -2 when no such element exists // Data must be sorted by < // O(log(n)) int erase_by_key(int root, const T &x) { if (!~root) return -2; push(root); if (data[root] < x) { int u = erase_by_key(right[root], x); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_key(left[root], x); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // Erase the smallest element >= x. // Return -2 when no such element exists // Data must be sorted by cmp // O(log(n)) template <class Compare> int erase_by_key(int root, const T &x, Compare cmp) { if (!~root) return -2; push(root); if (cmp(data[root], x)) { int u = erase_by_key(right[root], x, cmp); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_key(left[root], x, cmp); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // O(log(n)) int erase_by_order(int root, int pos) { assert(~root); push(root); if (size(left[root]) == pos) { dead_node.push_back(root); return append(left[root], right[root]); } if (size(left[root]) < pos) right[root] = erase_by_order(right[root], pos - size(left[root]) - 1); else left[root] = erase_by_order(left[root], pos); refresh(root); return root; } // Erase the smallest element x with !pred(x) // Return -2 when no such element exists // Data must be sorted by pred // O(log(n)) int erase_by_pred(int root, auto pred) { if (!~root) return -2; push(root); if (pred(data[root])) { int u = erase_by_pred(right[root], pred); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_pred(left[root], pred); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // O(# of elements erased) void erase(int root) { if (!~root) return; dead_node.push_back(root); for (auto beg = (int)dead_node.size() - 1; beg < (int)dead_node.size(); ++beg) { int u = dead_node[beg]; if (~left[u]) dead_node.push_back(left[u]); if (~right[u]) dead_node.push_back(right[u]); } } // Data must be sorted by < // O(min(size(u), size(v)) * log(size ratio)) int unite_by_key(int u, int v) { if (!~u || !~v) return ~u ? u : v; if (priority[u] < priority[v]) swap(u, v); auto [a, b] = split_by_key(v, data[u]); push(u); left[u] = unite_by_key(left[u], a); right[u] = unite_by_key(right[u], b); refresh(u); return u; } // Data must be sorted by cmp // O(min(size(u), size(v)) * log(size ratio)) template <class Compare> int unite_by_key(int u, int v, Compare cmp) { if (!~u || !~v) return ~u ? u : v; if (priority[u] < priority[v]) swap(u, v); auto [a, b] = split_by_key(v, data[u], cmp); push(u); left[u] = unite_by_key(left[u], a, cmp); right[u] = unite_by_key(right[u], b, cmp); refresh(u); return u; } void traverse(int root, auto f) { if (~root) { push(root); traverse(left[root], f); f(root); traverse(right[root], f); refresh(root); } } int build(int n) { return build(vector<T>(n, T_id)); } int build(int n, T init) { return build(vector<T>(n, init)); } template <class V> int build(const vector<V> &a) { auto recurse = [&](auto self, int l, int r) -> int { if (l == r) return -1; int m = l + ((r - l) >> 1); return new_node(a[m], self(self, l, m), self(self, m + 1, r)); }; return recurse(recurse, 0, (int)a.size()); } // Data must be sorted by < // O(log(n)) int order_of_key(int root, const T &x) { int res = 0; while (~root) { push(root); if (data[root] < x) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // Data must be sorted by cmp // O(log(n)) template <class Compare> int order_of_key(int root, const T &x, Compare cmp) { int res = 0; while (~root) { push(root); if (cmp(data[root], x)) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // O(log(n)) int node_order(int root, int u) { assert(~root && ~u); int res = size(left[u]); while (root != u) { push(pv[u]); if (right[pv[u]] == u) res += size(left[pv[u]]) + 1; u = pv[u]; } return res; } // pred must be T, T, ..., T, F, F, ..., F // O(log(n)) int partition_point(int root, auto pred) { int res = 0; while (~root) { push(root); if (pred(data[root])) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // O(log(n)) void set(int root, int p, const T &x) { assert(0 <= p && p < size(root)); int u = root; while (true) { push(u); if (size(left[u]) == p) { data[u] = x; refresh<false>(u); break; } if (size(left[u]) > p) u = left[u]; else { p -= size(left[u]) + 1; u = right[u]; } } while (u != root) { u = pv[u]; refresh<false>(u); } } // O(log(n)) T query(int root, int p) { assert(0 <= p && p < size(root)); while (true) { push(root); if (size(left[root]) == p) return data[root]; if (size(left[root]) > p) root = left[root]; else { p -= size(left[root]) + 1; root = right[root]; } } } T _query(int root, int ql, int qr) { static_assert(HAS_QUERY); if (!~root || qr <= 0 || size(root) <= ql) return T_id; if (ql <= 0 && size(root) <= qr) return subtr_data[root]; push(root); T res = T_id; if (ql < size(left[root])) res = _query(left[root], ql, qr); if (ql <= size(left[root]) && size(left[root]) + 1 <= qr) res = TT(res, data[root]); if (size(left[root]) + 1 < qr) res = TT(res, _query(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1)); return res; } // O(log(n)) T query(int root, int ql, int qr) { static_assert(HAS_QUERY); assert(0 <= ql && ql <= qr && qr <= size(root)); return ql == qr ? T_id : _query(root, ql, qr); } // O(log(n)) void update(int root, int p, const U &f) { static_assert(HAS_UPDATE); assert(0 <= p && p < size(root)); int u = root; while (true) { push(u); if (size(left[u]) == p) { data[u] = UT(f, data[u]); refresh<false>(u); break; } if (size(left[u]) > p) u = left[u]; else { p -= size(left[u]) + 1; u = right[u]; } } while (u != root) { u = pv[u]; refresh<false>(u); } } // O(log(n)) void _update(int root, int ql, int qr, const U &f) { static_assert(HAS_UPDATE); if (!~root || qr <= 0 || size(root) <= ql) return; if (ql <= 0 && size(root) <= qr) { data[root] = UT(f, data[root]); lazy[root] = UU(f, lazy[root]); refresh<false>(root); return; } push(root); if (ql < size(left[root])) _update(left[root], ql, qr, f); if (ql <= size(left[root]) && size(left[root]) + 1 <= qr) data[root] = UT(f, data[root]); if (size(left[root]) + 1 < qr) _update(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1, f); refresh<false>(root); } // O(log(n)) void update(int root, int ql, int qr, const U &f) { static_assert(HAS_UPDATE); assert(0 <= ql && ql <= qr && qr <= size(root)); if (ql == qr) return; _update(root, ql, qr, f); } // O(log(n)) void _flip(int root, int ql, int qr) { static_assert(HAS_FLIP); push(root); if (qr <= size(left[root])) { _flip(left[root], ql, qr); refresh<false>(root); } else if (size(left[root]) + 1 <= ql) { _flip(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1); refresh<false>(root); } else { auto [ar, br] = split_by_order(right[root], qr - size(left[root]) - 1); auto [al, bl] = split_by_order(left[root], ql); if (~bl) { push(bl); swap(left[bl], right[bl]); lazy_flip[bl] ^= 1; refresh<false>(bl); } if (~ar) { push(ar); swap(left[ar], right[ar]); lazy_flip[ar] ^= 1; refresh<false>(ar); } left[root] = append(al, ar); right[root] = append(bl, br); refresh(root); } } // O(log(n)) void flip(int root, int ql, int qr) { static_assert(HAS_FLIP); assert(0 <= ql && ql <= qr && qr <= size(root)); if (!~root || qr - ql <= 1) return; _flip(root, ql, qr); } // pred(sum[ql, r)) is T, T, ..., T, F, F, ..., F // Returns max r with T // O(log(n)) int max_pref(int u, int ql, auto pred) { static_assert(HAS_QUERY); int n = size(u); assert(0 <= ql && ql <= n && pred(T_id)); if (ql == n) return n; T sum = T_id; auto recurse = [&](auto self, int u, int l) -> int { int r = l + size(u); if (!~u || r <= ql) return n; if (ql <= l && pred(TT(sum, subtr_data[u]))) { sum = TT(sum, subtr_data[u]); return n; } push(u); if (auto p = self(self, left[u], l); p < n) return p; l += size(left[u]); return ql <= l && !pred(sum = TT(sum, data[u])) ? l : self(self, right[u], l + 1); }; return recurse(recurse, u, 0); } // pred(sum[l, qr)) is F, F, ..., F, T, T, ..., T // Returns min l with T // O(log(n)) int max_suff(int u, int qr, auto pred) { static_assert(HAS_QUERY); int n = size(u); assert(0 <= qr && qr <= n && pred(T_id)); if (qr == 0) return 0; T sum = T_id; auto recurse = [&](auto self, int u, int r) -> int { int l = r - size(u); if (!~u || qr <= l) return 0; if (r <= qr && pred(TT(subtr_data[u], sum))) { sum = TT(subtr_data[u], sum); return 0; } push(u); if (auto p = self(self, right[u], r); p > 0) return p; r -= size(right[u]); return r <= qr && !pred(sum = TT(data[u], sum)) ? r : self(self, left[u], r - 1); }; return recurse(recurse, u, n); } #undef ifQ #undef ifU #undef ifF #undef ifNC }; struct node { int sum, idx; int sub_cnt; bool operator<(const node &nd) const { if (sum != nd.sum) return sum < nd.sum; return idx > nd.idx; } node() { sum = sub_cnt = idx = 0; } node(int s, int i, int c) { sum = s; idx = i; sub_cnt = c; } node operator+(const node &nd) const { return node(sum + nd.sum, -1, sub_cnt + nd.sub_cnt); } }; struct lazy { bool flush; int n; bool operator!=(const lazy &l) const { return flush != l.flush || n != l.n; } lazy() { flush = 0; n = 0; } lazy(bool _f, lint _n) { flush = _f; n = _n; } lazy operator+(const lazy &lz) const { if (lz.flush) return lz; return lazy(flush, lz.n + n); } node operator+(const node &nd) const { node ret = nd; if (flush) { ret.sum = 0; } ret.sum += ret.sub_cnt * n; return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); auto _TT = [&](node a, node b) -> node { return a + b; }; auto _UU = [&](lazy a, lazy b) -> lazy { return b + a; }; auto _UT = [&](lazy a, node b) -> node { return a + b; }; auto _FlipT = [&](node a) -> node { return a; }; auto treap = treap_base<true, true, false, false, node, lazy, decltype(_TT), decltype(_UU), decltype(_UT), decltype(_FlipT)>(_TT, node(), _UU, lazy(), _UT, _FlipT); int n, m; cin >> n >> m; vector<int> a(n); for (auto &x : a) { cin >> x; } vector<int> roots[64]; for (int i = n - 1; i >= 0; i--) { vector<int> pos(64); auto count = [&](lint v) { // count < v node to_value(v / n, n - 1 - v % n, 1); int cur = 0; int thres = (n - i - 1) / 2; for (int i = 0; i < 64; i++) { pos[i] = 0; for (auto &j : roots[i]) { int k = treap.partition_point(j, [&](node p) { if (p.sum == -1) p.sum = 0; else p.sum += i + 1; return p < to_value; }); pos[i] += k; cur += k; if (cur > thres) return cur; } } return accumulate(all(pos), 0); }; auto proc = [&](lint v) { // assert(count(v) == (n - 1 - i) / 2); count(v); lint sum = 0; vector<int> minuses(64, -1); for (int i = 0; i < 64; i++) { // coalesce all -1s here vector<int> new_roots; for (int j = 0; j < sz(roots[i]); j++) { auto [t1, t2] = treap.split_by_pred(roots[i][j], [&](int p) { return treap.data[p].sum == -1; }); minuses[i] = treap.append(minuses[i], t1); if (~t2) new_roots.push_back(t2); } roots[i] = new_roots; } for (int i = 0; i < 64; i++) { int to_sum = pos[i] - treap.size(minuses[i]); if (to_sum <= 0) continue; for (auto &j : roots[i]) { if (to_sum == 0) break; int here_sum = min(to_sum, treap.size(j)); auto g = treap.query(j, 0, here_sum); sum += g.sum + g.sub_cnt * (i + 1); to_sum -= here_sum; } } lint dpvalue; if (sum > m) { dpvalue = -1; // get minus back for (int i = 0; i < 64; i++) { if (~minuses[i]) { roots[i].insert(roots[i].begin(), minuses[i]); } } } else { dpvalue = m - sum; for (int i = 0; i < 64; i++) { vector<int> new_roots; int gonezero = -1; auto nullify = [&](int p) { treap.update(p, 0, treap.size(p), lazy(true, 0)); gonezero = treap.unite_by_key(gonezero, p); }; if (~minuses[i]) { int dx = min(pos[i], treap.size(minuses[i])); pos[i] -= dx; auto [t1, t2] = treap.split_by_order(minuses[i], dx); nullify(t2); treap.update(t1, 0, treap.size(t1), lazy(false, 1)); minuses[i] = t1; } for (auto &j : roots[i]) { int dx = min(pos[i], treap.size(j)); auto [t1, t2] = treap.split_by_order(j, dx); treap.update(t1, 0, treap.size(t1), lazy(false, i + 1)); if (~t1) new_roots.push_back(t1); nullify(t2); pos[i] -= dx; } roots[i] = new_roots; minuses[i] = treap.unite_by_key(minuses[i], gonezero); if (~minuses[i]) roots[i].insert(roots[i].begin(), minuses[i]); } } { vector<node> nd = {node(dpvalue, i, 1)}; bool ok = 0; int p = a[i] - 1; for (int j = 0; j < sz(roots[p]); j++) { int spp = treap.partition_point(roots[p][j], [&](node p) { return p < nd[0]; }); if (spp < treap.size(roots[p][j])) { auto [t1, t2] = treap.split_by_order(roots[p][j], spp); t1 = treap.insert_by_key(t1, nd[0]); roots[p].erase(roots[p].begin() + j); if (~t1) roots[p].insert(roots[p].begin() + j, t2); if (~t2) roots[p].insert(roots[p].begin() + j, t1); ok = 1; break; } } if (!ok) { int newborn = treap.build(nd); roots[p].push_back(newborn); } } }; lint s = 0, e = 1ll * n * 8300000; while (s != e) { lint mid = (s + e) / 2; lint ct = count(mid); if (ct < (n - i - 1) / 2) s = mid + 1; else if (ct > (n - i - 1) / 2) e = mid - 1; else { s = e = mid; } } proc(s); } vector<int> dp(n); for (int i = 0; i < 64; i++) { for (auto &j : roots[i]) { treap.traverse(j, [&](int p) { dp[treap.data[p].idx] = treap.data[p].sum; }); } } for (int i = 0; i < n; i++) cout << dp[i] << (i + 1 == n ? " \n" : " "); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | #include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); // Treap by Aeren // T: data type, U: lazy type. // TT: usual addition // UU: combination of lazy (new, old) // UT: lazy to the data (new, old) template <bool HAS_QUERY, bool HAS_UPDATE, bool HAS_FLIP, bool IS_COMMUTATIVE, class T, class U, class F1, class F2, class F3, class F4> struct treap_base { #define ifQ if constexpr (HAS_QUERY) #define ifU if constexpr (HAS_UPDATE) #define ifF if constexpr (HAS_FLIP) #define ifNC if constexpr (!IS_COMMUTATIVE) vector<unsigned int> priority; vector<int> pv, left, right, internal_size, lazy_flip; vector<T> data, subtr_data; vector<U> lazy; F1 TT; // monoid operation (always adjacent) T T_id; // monoid identity F2 UU; // monoid operation (superset, subset) U U_id; // monoid identity F3 UT; // action of U on T (superset, subset) F4 FlipT; treap_base(F1 TT, T T_id, F2 UU, U U_id, F3 UT, F4 FlipT = plus<>()) : TT(TT), T_id(T_id), UU(UU), U_id(U_id), UT(UT), FlipT(FlipT) {} void push(int u) { ifU if (lazy[u] != U_id) { if (auto v = left[u]; ~v) { data[v] = UT(lazy[u], data[v]); lazy[v] = UU(lazy[u], lazy[v]); ifQ subtr_data[v] = UT(lazy[u], subtr_data[v]); } if (auto w = right[u]; ~w) { data[w] = UT(lazy[u], data[w]); lazy[w] = UU(lazy[u], lazy[w]); ifQ subtr_data[w] = UT(lazy[u], subtr_data[w]); } lazy[u] = U_id; } ifF if (lazy_flip[u]) { if (auto v = left[u]; ~v) { swap(left[v], right[v]); lazy_flip[v] ^= 1; ifQ ifNC subtr_data[v] = FlipT(subtr_data[v]); } if (auto w = right[u]; ~w) { swap(left[w], right[w]); lazy_flip[w] ^= 1; ifQ ifNC subtr_data[w] = FlipT(subtr_data[w]); } lazy_flip[u] = false; } } template <bool UPDATE_STRUCTURE = true> void refresh(int u) { if constexpr (UPDATE_STRUCTURE) internal_size[u] = 1; ifQ subtr_data[u] = T_id; if (auto v = left[u]; ~v) { if constexpr (UPDATE_STRUCTURE) { pv[v] = u; internal_size[u] += internal_size[v]; } ifQ { auto x = subtr_data[v]; ifU if (lazy[u] != U_id) x = UT(lazy[u], x); ifF ifNC if (lazy_flip[u]) x = FlipT(x); subtr_data[u] = x; } } ifQ subtr_data[u] = TT(subtr_data[u], data[u]); if (auto w = right[u]; ~w) { if constexpr (UPDATE_STRUCTURE) { pv[w] = u; internal_size[u] += internal_size[w]; } ifQ { auto x = subtr_data[w]; ifU if (lazy[u] != U_id) x = UT(lazy[u], x); ifF ifNC if (lazy_flip[u]) x = FlipT(x); subtr_data[u] = TT(subtr_data[u], x); } } } void heapify(int u) { if (~u) { int v = u; if (~left[u] && priority[left[u]] > priority[v]) v = left[u]; if (~right[u] && priority[right[u]] > priority[v]) v = right[u]; if (u != v) swap(priority[u], priority[v]), heapify(v); } } template <class output_stream> output_stream &print(output_stream &out, int root) { #ifdef LOCAL out << "["; traverse(root, [&](int root) { out << data[root] << ", "; }); out << "\b\b]\n"; #endif return out; } vector<int> dead_node; int new_node(const T &x, int v = -1, int w = -1) { // Among all seeds in range [0, 1769666], // 72718 has the longest sequence of unique numbers, which is of length 362129. static mt19937 rng(72718); int u; if (dead_node.empty()) { u = (int)data.size(); pv.push_back(-1); left.push_back(v); right.push_back(w); priority.push_back(rng()); internal_size.push_back(1); data.push_back(x); ifQ subtr_data.push_back(x); ifU lazy.push_back(U_id); ifF lazy_flip.push_back(0); } else { u = dead_node.back(); dead_node.pop_back(); pv[u] = -1; left[u] = v; right[u] = w; priority[u] = rng(); internal_size[u] = 1; data[u] = x; ifQ subtr_data[u] = x; ifU lazy[u] = U_id; ifF lazy_flip[u] = 0; } heapify(u); refresh(u); return u; } // O(1) int size(int root) const { return ~root ? internal_size[root] : 0; } // Split to [-inf, x), [x, inf) // Data must be sorted < // O(log(n)) pair<int, int> split_by_key(int root, const T &x) { if (!~root) return {-1, -1}; push(root); if (data[root] < x) { auto [a, b] = split_by_key(right[root], x); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_key(left[root], x); left[root] = b, refresh(root); return {a, root}; } } // Split to [-inf, x), [x, inf) // Data must be sorted cmp // O(log(n)) template <class Compare> pair<int, int> split_by_key(int root, const T &x, Compare cmp) { if (!~root) return {-1, -1}; push(root); if (cmp(data[root], x)) { auto [a, b] = split_by_key(right[root], x, cmp); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_key(left[root], x, cmp); left[root] = b, refresh(root); return {a, root}; } } // Split into [0, pos), [pos, size(root)) // O(log(n)) pair<int, int> split_by_order(int root, int pos) { if (!~root) { assert(pos == 0); return {-1, -1}; } push(root); if (size(left[root]) < pos) { auto [a, b] = split_by_order(right[root], pos - size(left[root]) - 1); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_order(left[root], pos); left[root] = b, refresh(root); return {a, root}; } } // Split to [true Segment], [false Segment] // Data must be sorted by pred // O(log(n)) pair<int, int> split_by_pred(int root, auto pred) { if (!~root) return {-1, -1}; push(root); if (pred(root)) { auto [a, b] = split_by_pred(right[root], pred); right[root] = a, refresh(root); return {root, b}; } else { auto [a, b] = split_by_pred(left[root], pred); left[root] = b, refresh(root); return {a, root}; } } // Split into [0, l), [l, r), [r, size(root)) // O(log(n)) array<int, 3> split_to_three(int u, int l, int r) { assert(0 <= l && l <= r && r <= size(u)); if (!~u) return {-1, -1, -1}; int a, b, c; tie(a, b) = split_by_order(u, l); tie(b, c) = split_by_order(b, r - l); return {a, b, c}; } // Split into [0, pos[0]), [pos[0], pos[1]), ..., [pos[size(pos) - 1], size(root)) // O(k * log(n)) vector<int> split(int root, const vector<int> &pos) { assert(is_sorted(pos.begin(), pos.end())); if (pos.empty()) return {root}; assert(0 <= pos.front() && pos.back() <= size(root)); if (!~root) return vector<int>((int)pos.size() + 1, -1); vector<int> res((int)pos.size() + 1); res[0] = root; for (auto i = 0, last = 0; i < (int)pos.size(); ++i) { tie(res[i], res[i + 1]) = split_by_order(res[i], pos[i] - last); last = pos[i]; } return res; } // Append u and v // O(log(n)) int append(int u, int v) { if (!~u || !~v) return ~u ? u : v; push(u); push(v); if (priority[v] < priority[u]) { right[u] = append(right[u], v), refresh(u); return u; } else { left[v] = append(u, left[v]), refresh(v); return v; } } // Append treaps in order // O((list length) * log(n)) int append(const vector<int> &list) { return accumulate(list.begin(), list.end(), -1, [&](int u, int v) { return append(u, v); }); } // Data must be sorted by < // O(log(n)) int insert_node_by_key(int root, int u) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (data[root] < data[u]) right[root] = insert_node_by_key(right[root], u); else left[root] = insert_node_by_key(left[root], u); refresh(root); return root; } auto [a, b] = split_by_key(root, data[u]); left[u] = a, right[u] = b; refresh(u); return u; } // Data must be sorted by < // O(log(n)) int insert_by_key(int root, const T &x) { return insert_node_by_key(root, new_node(x)); } // Data must be sorted by cmp // O(log(n)) template <class Compare> int insert_node_by_key(int root, int u, Compare cmp) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (cmp(data[root], data[u])) right[root] = insert_node_by_key(right[root], u, cmp); else left[root] = insert_node_by_key(left[root], u, cmp); refresh(root); return root; } auto [a, b] = split_by_key(root, data[u], cmp); left[u] = a, right[u] = b; refresh(u); return u; } // Data must be sorted by cmp // O(log(n)) template <class Compare> int insert_by_key(int root, const T &x, Compare cmp) { return insert_node_by_key(root, new_node(x), cmp); } // O(log(n)) int insert_node_by_order(int root, int pos, int u) { if (!~root) { assert(pos == 0); return u; } push(root); if (priority[root] > priority[u]) { if (size(left[root]) < pos) right[root] = insert_node_by_order(right[root], pos - size(left[root]) - 1, u); else left[root] = insert_node_by_order(left[root], pos, u); refresh(root); return root; } auto [a, b] = split_by_order(root, pos); left[u] = a, right[u] = b; refresh(u); return u; } // O(log(n)) int insert_by_order(int root, int pos, const T &x) { return insert_node_by_order(root, pos, new_node(x)); } // Data must be sorted by pred // O(log(n)) int insert_node_by_pred(int root, auto pred, int u) { if (!~root) return u; push(root); if (priority[root] > priority[u]) { if (pred(data[root])) right[root] = insert_node_by_pred(right[root], pred, u); else left[root] = insert_node_by_pred(left[root], pred, u); refresh(root); return root; } auto [a, b] = split_by_pred(root, pred); left[u] = a, right[u] = b; refresh(u); return u; } // O(log(n)) int insert_by_pred(int root, auto pred, const T &x) { return insert_node_by_pred(root, pred, new_node(x)); } // Erase the smallest element >= x. // Return -2 when no such element exists // Data must be sorted by < // O(log(n)) int erase_by_key(int root, const T &x) { if (!~root) return -2; push(root); if (data[root] < x) { int u = erase_by_key(right[root], x); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_key(left[root], x); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // Erase the smallest element >= x. // Return -2 when no such element exists // Data must be sorted by cmp // O(log(n)) template <class Compare> int erase_by_key(int root, const T &x, Compare cmp) { if (!~root) return -2; push(root); if (cmp(data[root], x)) { int u = erase_by_key(right[root], x, cmp); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_key(left[root], x, cmp); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // O(log(n)) int erase_by_order(int root, int pos) { assert(~root); push(root); if (size(left[root]) == pos) { dead_node.push_back(root); return append(left[root], right[root]); } if (size(left[root]) < pos) right[root] = erase_by_order(right[root], pos - size(left[root]) - 1); else left[root] = erase_by_order(left[root], pos); refresh(root); return root; } // Erase the smallest element x with !pred(x) // Return -2 when no such element exists // Data must be sorted by pred // O(log(n)) int erase_by_pred(int root, auto pred) { if (!~root) return -2; push(root); if (pred(data[root])) { int u = erase_by_pred(right[root], pred); if (u == -2) return -2; right[root] = u; } else { int u = erase_by_pred(left[root], pred); if (u == -2) { dead_node.push_back(root); return append(left[root], right[root]); } left[root] = u; } refresh(root); return root; } // O(# of elements erased) void erase(int root) { if (!~root) return; dead_node.push_back(root); for (auto beg = (int)dead_node.size() - 1; beg < (int)dead_node.size(); ++beg) { int u = dead_node[beg]; if (~left[u]) dead_node.push_back(left[u]); if (~right[u]) dead_node.push_back(right[u]); } } // Data must be sorted by < // O(min(size(u), size(v)) * log(size ratio)) int unite_by_key(int u, int v) { if (!~u || !~v) return ~u ? u : v; if (priority[u] < priority[v]) swap(u, v); auto [a, b] = split_by_key(v, data[u]); push(u); left[u] = unite_by_key(left[u], a); right[u] = unite_by_key(right[u], b); refresh(u); return u; } // Data must be sorted by cmp // O(min(size(u), size(v)) * log(size ratio)) template <class Compare> int unite_by_key(int u, int v, Compare cmp) { if (!~u || !~v) return ~u ? u : v; if (priority[u] < priority[v]) swap(u, v); auto [a, b] = split_by_key(v, data[u], cmp); push(u); left[u] = unite_by_key(left[u], a, cmp); right[u] = unite_by_key(right[u], b, cmp); refresh(u); return u; } void traverse(int root, auto f) { if (~root) { push(root); traverse(left[root], f); f(root); traverse(right[root], f); refresh(root); } } int build(int n) { return build(vector<T>(n, T_id)); } int build(int n, T init) { return build(vector<T>(n, init)); } template <class V> int build(const vector<V> &a) { auto recurse = [&](auto self, int l, int r) -> int { if (l == r) return -1; int m = l + ((r - l) >> 1); return new_node(a[m], self(self, l, m), self(self, m + 1, r)); }; return recurse(recurse, 0, (int)a.size()); } // Data must be sorted by < // O(log(n)) int order_of_key(int root, const T &x) { int res = 0; while (~root) { push(root); if (data[root] < x) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // Data must be sorted by cmp // O(log(n)) template <class Compare> int order_of_key(int root, const T &x, Compare cmp) { int res = 0; while (~root) { push(root); if (cmp(data[root], x)) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // O(log(n)) int node_order(int root, int u) { assert(~root && ~u); int res = size(left[u]); while (root != u) { push(pv[u]); if (right[pv[u]] == u) res += size(left[pv[u]]) + 1; u = pv[u]; } return res; } // pred must be T, T, ..., T, F, F, ..., F // O(log(n)) int partition_point(int root, auto pred) { int res = 0; while (~root) { push(root); if (pred(data[root])) { res += size(left[root]) + 1; root = right[root]; } else root = left[root]; } return res; } // O(log(n)) void set(int root, int p, const T &x) { assert(0 <= p && p < size(root)); int u = root; while (true) { push(u); if (size(left[u]) == p) { data[u] = x; refresh<false>(u); break; } if (size(left[u]) > p) u = left[u]; else { p -= size(left[u]) + 1; u = right[u]; } } while (u != root) { u = pv[u]; refresh<false>(u); } } // O(log(n)) T query(int root, int p) { assert(0 <= p && p < size(root)); while (true) { push(root); if (size(left[root]) == p) return data[root]; if (size(left[root]) > p) root = left[root]; else { p -= size(left[root]) + 1; root = right[root]; } } } T _query(int root, int ql, int qr) { static_assert(HAS_QUERY); if (!~root || qr <= 0 || size(root) <= ql) return T_id; if (ql <= 0 && size(root) <= qr) return subtr_data[root]; push(root); T res = T_id; if (ql < size(left[root])) res = _query(left[root], ql, qr); if (ql <= size(left[root]) && size(left[root]) + 1 <= qr) res = TT(res, data[root]); if (size(left[root]) + 1 < qr) res = TT(res, _query(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1)); return res; } // O(log(n)) T query(int root, int ql, int qr) { static_assert(HAS_QUERY); assert(0 <= ql && ql <= qr && qr <= size(root)); return ql == qr ? T_id : _query(root, ql, qr); } // O(log(n)) void update(int root, int p, const U &f) { static_assert(HAS_UPDATE); assert(0 <= p && p < size(root)); int u = root; while (true) { push(u); if (size(left[u]) == p) { data[u] = UT(f, data[u]); refresh<false>(u); break; } if (size(left[u]) > p) u = left[u]; else { p -= size(left[u]) + 1; u = right[u]; } } while (u != root) { u = pv[u]; refresh<false>(u); } } // O(log(n)) void _update(int root, int ql, int qr, const U &f) { static_assert(HAS_UPDATE); if (!~root || qr <= 0 || size(root) <= ql) return; if (ql <= 0 && size(root) <= qr) { data[root] = UT(f, data[root]); lazy[root] = UU(f, lazy[root]); refresh<false>(root); return; } push(root); if (ql < size(left[root])) _update(left[root], ql, qr, f); if (ql <= size(left[root]) && size(left[root]) + 1 <= qr) data[root] = UT(f, data[root]); if (size(left[root]) + 1 < qr) _update(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1, f); refresh<false>(root); } // O(log(n)) void update(int root, int ql, int qr, const U &f) { static_assert(HAS_UPDATE); assert(0 <= ql && ql <= qr && qr <= size(root)); if (ql == qr) return; _update(root, ql, qr, f); } // O(log(n)) void _flip(int root, int ql, int qr) { static_assert(HAS_FLIP); push(root); if (qr <= size(left[root])) { _flip(left[root], ql, qr); refresh<false>(root); } else if (size(left[root]) + 1 <= ql) { _flip(right[root], ql - size(left[root]) - 1, qr - size(left[root]) - 1); refresh<false>(root); } else { auto [ar, br] = split_by_order(right[root], qr - size(left[root]) - 1); auto [al, bl] = split_by_order(left[root], ql); if (~bl) { push(bl); swap(left[bl], right[bl]); lazy_flip[bl] ^= 1; refresh<false>(bl); } if (~ar) { push(ar); swap(left[ar], right[ar]); lazy_flip[ar] ^= 1; refresh<false>(ar); } left[root] = append(al, ar); right[root] = append(bl, br); refresh(root); } } // O(log(n)) void flip(int root, int ql, int qr) { static_assert(HAS_FLIP); assert(0 <= ql && ql <= qr && qr <= size(root)); if (!~root || qr - ql <= 1) return; _flip(root, ql, qr); } // pred(sum[ql, r)) is T, T, ..., T, F, F, ..., F // Returns max r with T // O(log(n)) int max_pref(int u, int ql, auto pred) { static_assert(HAS_QUERY); int n = size(u); assert(0 <= ql && ql <= n && pred(T_id)); if (ql == n) return n; T sum = T_id; auto recurse = [&](auto self, int u, int l) -> int { int r = l + size(u); if (!~u || r <= ql) return n; if (ql <= l && pred(TT(sum, subtr_data[u]))) { sum = TT(sum, subtr_data[u]); return n; } push(u); if (auto p = self(self, left[u], l); p < n) return p; l += size(left[u]); return ql <= l && !pred(sum = TT(sum, data[u])) ? l : self(self, right[u], l + 1); }; return recurse(recurse, u, 0); } // pred(sum[l, qr)) is F, F, ..., F, T, T, ..., T // Returns min l with T // O(log(n)) int max_suff(int u, int qr, auto pred) { static_assert(HAS_QUERY); int n = size(u); assert(0 <= qr && qr <= n && pred(T_id)); if (qr == 0) return 0; T sum = T_id; auto recurse = [&](auto self, int u, int r) -> int { int l = r - size(u); if (!~u || qr <= l) return 0; if (r <= qr && pred(TT(subtr_data[u], sum))) { sum = TT(subtr_data[u], sum); return 0; } push(u); if (auto p = self(self, right[u], r); p > 0) return p; r -= size(right[u]); return r <= qr && !pred(sum = TT(data[u], sum)) ? r : self(self, left[u], r - 1); }; return recurse(recurse, u, n); } #undef ifQ #undef ifU #undef ifF #undef ifNC }; struct node { int sum, idx; int sub_cnt; bool operator<(const node &nd) const { if (sum != nd.sum) return sum < nd.sum; return idx > nd.idx; } node() { sum = sub_cnt = idx = 0; } node(int s, int i, int c) { sum = s; idx = i; sub_cnt = c; } node operator+(const node &nd) const { return node(sum + nd.sum, -1, sub_cnt + nd.sub_cnt); } }; struct lazy { bool flush; int n; bool operator!=(const lazy &l) const { return flush != l.flush || n != l.n; } lazy() { flush = 0; n = 0; } lazy(bool _f, lint _n) { flush = _f; n = _n; } lazy operator+(const lazy &lz) const { if (lz.flush) return lz; return lazy(flush, lz.n + n); } node operator+(const node &nd) const { node ret = nd; if (flush) { ret.sum = 0; } ret.sum += ret.sub_cnt * n; return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); auto _TT = [&](node a, node b) -> node { return a + b; }; auto _UU = [&](lazy a, lazy b) -> lazy { return b + a; }; auto _UT = [&](lazy a, node b) -> node { return a + b; }; auto _FlipT = [&](node a) -> node { return a; }; auto treap = treap_base<true, true, false, false, node, lazy, decltype(_TT), decltype(_UU), decltype(_UT), decltype(_FlipT)>(_TT, node(), _UU, lazy(), _UT, _FlipT); int n, m; cin >> n >> m; vector<int> a(n); for (auto &x : a) { cin >> x; } vector<int> roots[64]; for (int i = n - 1; i >= 0; i--) { vector<int> pos(64); auto count = [&](lint v) { // count < v node to_value(v / n, n - 1 - v % n, 1); int cur = 0; int thres = (n - i - 1) / 2; for (int i = 0; i < 64; i++) { pos[i] = 0; for (auto &j : roots[i]) { int k = treap.partition_point(j, [&](node p) { if (p.sum == -1) p.sum = 0; else p.sum += i + 1; return p < to_value; }); pos[i] += k; cur += k; if (cur > thres) return cur; } } return accumulate(all(pos), 0); }; auto proc = [&](lint v) { // assert(count(v) == (n - 1 - i) / 2); count(v); lint sum = 0; vector<int> minuses(64, -1); for (int i = 0; i < 64; i++) { // coalesce all -1s here vector<int> new_roots; for (int j = 0; j < sz(roots[i]); j++) { auto [t1, t2] = treap.split_by_pred(roots[i][j], [&](int p) { return treap.data[p].sum == -1; }); minuses[i] = treap.append(minuses[i], t1); if (~t2) new_roots.push_back(t2); } roots[i] = new_roots; } for (int i = 0; i < 64; i++) { int to_sum = pos[i] - treap.size(minuses[i]); if (to_sum <= 0) continue; for (auto &j : roots[i]) { if (to_sum == 0) break; int here_sum = min(to_sum, treap.size(j)); auto g = treap.query(j, 0, here_sum); sum += g.sum + g.sub_cnt * (i + 1); to_sum -= here_sum; } } lint dpvalue; if (sum > m) { dpvalue = -1; // get minus back for (int i = 0; i < 64; i++) { if (~minuses[i]) { roots[i].insert(roots[i].begin(), minuses[i]); } } } else { dpvalue = m - sum; for (int i = 0; i < 64; i++) { vector<int> new_roots; int gonezero = -1; auto nullify = [&](int p) { treap.update(p, 0, treap.size(p), lazy(true, 0)); gonezero = treap.unite_by_key(gonezero, p); }; if (~minuses[i]) { int dx = min(pos[i], treap.size(minuses[i])); pos[i] -= dx; auto [t1, t2] = treap.split_by_order(minuses[i], dx); nullify(t2); treap.update(t1, 0, treap.size(t1), lazy(false, 1)); minuses[i] = t1; } for (auto &j : roots[i]) { int dx = min(pos[i], treap.size(j)); auto [t1, t2] = treap.split_by_order(j, dx); treap.update(t1, 0, treap.size(t1), lazy(false, i + 1)); if (~t1) new_roots.push_back(t1); nullify(t2); pos[i] -= dx; } roots[i] = new_roots; minuses[i] = treap.unite_by_key(minuses[i], gonezero); if (~minuses[i]) roots[i].insert(roots[i].begin(), minuses[i]); } } { vector<node> nd = {node(dpvalue, i, 1)}; bool ok = 0; int p = a[i] - 1; for (int j = 0; j < sz(roots[p]); j++) { int spp = treap.partition_point(roots[p][j], [&](node p) { return p < nd[0]; }); if (spp < treap.size(roots[p][j])) { auto [t1, t2] = treap.split_by_order(roots[p][j], spp); t1 = treap.insert_by_key(t1, nd[0]); roots[p].erase(roots[p].begin() + j); if (~t1) roots[p].insert(roots[p].begin() + j, t2); if (~t2) roots[p].insert(roots[p].begin() + j, t1); ok = 1; break; } } if (!ok) { int newborn = treap.build(nd); roots[p].push_back(newborn); } } }; lint s = 0, e = 1ll * n * 8300000; while (s != e) { lint mid = (s + e) / 2; lint ct = count(mid); if (ct < (n - i - 1) / 2) s = mid + 1; else if (ct > (n - i - 1) / 2) e = mid - 1; else { s = e = mid; } } proc(s); } vector<int> dp(n); for (int i = 0; i < 64; i++) { for (auto &j : roots[i]) { treap.traverse(j, [&](int p) { dp[treap.data[p].idx] = treap.data[p].sum; }); } } for (int i = 0; i < n; i++) cout << dp[i] << (i + 1 == n ? " \n" : " "); } |