結果
問題 | No.925 紲星 Extra |
ユーザー | Pachicobue |
提出日時 | 2019-11-09 08:56:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 8,559 ms / 10,000 ms |
コード長 | 56,630 bytes |
コンパイル時間 | 4,910 ms |
コンパイル使用メモリ | 276,120 KB |
実行使用メモリ | 160,732 KB |
最終ジャッジ日時 | 2024-09-15 04:11:06 |
合計ジャッジ時間 | 115,507 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 28 ms
5,376 KB |
testcase_04 | AC | 30 ms
5,376 KB |
testcase_05 | AC | 7,691 ms
122,788 KB |
testcase_06 | AC | 7,067 ms
123,840 KB |
testcase_07 | AC | 6,933 ms
123,104 KB |
testcase_08 | AC | 6,951 ms
123,548 KB |
testcase_09 | AC | 6,597 ms
123,820 KB |
testcase_10 | AC | 6,097 ms
101,924 KB |
testcase_11 | AC | 8,220 ms
144,332 KB |
testcase_12 | AC | 8,138 ms
149,612 KB |
testcase_13 | AC | 5,383 ms
124,160 KB |
testcase_14 | AC | 4,656 ms
97,612 KB |
testcase_15 | AC | 5,834 ms
124,568 KB |
testcase_16 | AC | 5,453 ms
123,844 KB |
testcase_17 | AC | 2,897 ms
91,716 KB |
testcase_18 | AC | 7,284 ms
124,272 KB |
testcase_19 | AC | 4,969 ms
89,948 KB |
testcase_20 | AC | 8,559 ms
154,076 KB |
testcase_21 | AC | 6,197 ms
160,732 KB |
ソースコード
#include <bits/stdc++.h> #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T> constexpr T popcount(const T u) { return u ? static_cast<T>(__builtin_popcountll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T log2p1(const T u) { return u ? static_cast<T>(64 - __builtin_clzll(static_cast<u64>(u))) : static_cast<T>(0); } template<typename T> constexpr T msbp1(const T u) { return log2p1(u); } template<typename T> constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template<typename T> constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast<T>(u); } template<typename T> constexpr bool ispow2(const T u) { return u and (static_cast<u64>(u) & static_cast<u64>(u - 1)) == 0; } template<typename T> constexpr T ceil2(const T u) { return static_cast<T>(1) << clog(u); } template<typename T> constexpr T floor2(const T u) { return u == 0 ? static_cast<T>(0) : static_cast<T>(1) << (log2p1(u) - 1); } template<typename T> constexpr bool btest(const T mask, const usize ind) { return static_cast<bool>((static_cast<u64>(mask) >> ind) & static_cast<u64>(1)); } template<typename T> void bset(T& mask, const usize ind) { mask |= (static_cast<T>(1) << ind); } template<typename T> void breset(T& mask, const usize ind) { mask &= ~(static_cast<T>(1) << ind); } template<typename T> void bflip(T& mask, const usize ind) { mask ^= (static_cast<T>(1) << ind); } template<typename T> void bset(T& mask, const usize ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); } template<typename T> constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast<T>(0) : static_cast<T>((static_cast<u64>(mask) << (64 - ind)) >> (64 - ind)); } template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4; template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template<typename T> T read() { T v; return std::cin >> v, v; } template<typename T, typename... Args> auto read(const usize size, Args... args) { std::vector<decltype(read<T>(args...))> ans(size); for (usize i = 0; i < size; i++) { ans[i] = read<T>(args...); } return ans; } template<typename... Types> auto reads() { return std::tuple<std::decay_t<Types>...>{read<Types>()...}; } # define SHOW(...) static_cast<void>(0) template<typename T> T make_v(const T v) { return v; } template<typename... Args> auto make_v(const std::size_t size, Args... args) { return std::vector<decltype(make_v(args...))>(size, make_v(args...)); } class stopwatch { public: stopwatch() : start{std::chrono::system_clock::now()}, rap_point{start} {} template<typename Duration = std::chrono::milliseconds> int64_t rap() { const auto now = std::chrono::system_clock::now(); const auto cnt = std::chrono::duration_cast<Duration>(now - rap_point).count(); return rap_point = now, cnt; } template<typename Duration = std::chrono::milliseconds> int64_t total() { const auto now = std::chrono::system_clock::now(); const auto cnt = std::chrono::duration_cast<Duration>(now - start).count(); return cnt; } private: std::chrono::system_clock::time_point start; std::chrono::system_clock::time_point rap_point; }; namespace bbst_node { template<typename Key, typename Node, typename Comp> struct key_node : Node { using ptr = key_node* const; using const_ptr = const ptr; using key_type = Key; using comp_type = Comp; key_node() : Node{}, key{Key{}} {} template<typename... Args> key_node(const Key& key, Args... args) : Node{args...}, key{key} { this->sz = 1; } void pull_up(const_ptr l, const_ptr r) { Node::pull_up(l, r); } void push_down(ptr l, ptr r) { Node::push_down(l, r); } template<typename Value> void set(const Value& val, const_ptr l, const_ptr r) { Node::set(val, l, r); } template<typename Op> void act(const Op& o) { Node::act(o); } friend std::ostream& operator<<(std::ostream& os, const key_node& n) { return os << "key=" << n.key << ":" << static_cast<Node>(n); } const key_type key; }; struct node { using ptr = node* const; using const_ptr = const ptr; void pull_up(const_ptr l, const_ptr r) { sz = (l ? l->sz : 0UL) + 1UL + (r ? r->sz : 0UL); } void push_down(ptr, ptr) {} friend std::ostream& operator<<(std::ostream& os, const node& n) { return os << "size=" << n.sz; } usize sz = 0; }; template<typename Value> struct value_node { using ptr = value_node* const; using const_ptr = const ptr; using value_type = Value; value_node() = default; value_node(const value_type& value) : value{value}, sz{1} {} void pull_up(const_ptr l, const_ptr r) { sz = (l ? l->sz : 0UL) + 1UL + (r ? r->sz : 0UL); } void push_down(ptr, ptr) {} void set(const value_type& val, const_ptr, const_ptr) { value = val; } friend std::ostream& operator<<(std::ostream& os, const value_node& n) { return os << "value=" << n.value; } value_type value{}; usize sz = 0; }; template<typename ValueMonoid> struct merge_node { using ptr = merge_node* const; using const_ptr = const ptr; using value_monoid_type = ValueMonoid; using value_type = typename value_monoid_type::value_type; merge_node() = default; merge_node(const value_type& value) : value{value}, merged{value}, sz{1} {} void pull_up(const_ptr l, const_ptr r) { sz = (l ? l->sz : 0UL) + 1UL + (r ? r->sz : 0UL), merged = value_monoid_type::merge((l ? l->merged : value_monoid_type::id()), value_monoid_type::merge(value, (r ? r->merged : value_monoid_type::id()))); } void push_down(ptr, ptr) {} void set(const value_type& val, const_ptr l, const_ptr r) { value = val, pull_up(l, r); } friend std::ostream& operator<<(std::ostream& os, const merge_node& n) { return os << "value=" << n.value << ",merged=" << n.merged; } value_type value = value_monoid_type::id(), merged = value_monoid_type::id(); usize sz = 0; }; template<typename MonoidAct> struct lazy_node { using ptr = lazy_node* const; using const_ptr = const ptr; using monoid_act_type = MonoidAct; using value_monoid_type = typename monoid_act_type::value_monoid_type; using operator_monoid_type = typename monoid_act_type::operator_monoid_type; using value_type = typename value_monoid_type::value_type; using operator_type = typename operator_monoid_type::operator_type; lazy_node() = default; lazy_node(const value_type& value) : value{value}, merged{value}, sz{1} {} void pull_up(const_ptr l, const_ptr r) { sz = (l ? l->sz : 0UL) + 1UL + (r ? r->sz : 0UL), merged = value_monoid_type::merge((l ? l->merged : value_monoid_type::id()), value_monoid_type::merge(value, (r ? r->merged : value_monoid_type::id()))); } void push_down(ptr l, ptr r) { if (op == operator_monoid_type::id()) { return; } if (l) { l->act(op); } if (r) { r->act(op); } op = operator_monoid_type::id(); } void set(const value_type& val, const_ptr l, const_ptr r) { value = val, pull_up(l, r); } void act(const operator_type& o) { value = monoid_act_type::apply(o, value, 1), merged = monoid_act_type::apply(o, merged, sz), op = operator_monoid_type::compose(op, o); } friend std::ostream& operator<<(std::ostream& os, const lazy_node& n) { return os << "value=" << n.value << ",merged=" << n.merged << ",op=" << n.op; } value_type value = value_monoid_type::id(), merged = value_monoid_type::id(); operator_type op = operator_monoid_type::id(); usize sz = 0; }; } // namespace bbst_node /** * http://xoshiro.di.unimi.it/xoshiro128starstar.c * http://xoshiro.di.unimi.it/xoshiro256starstar.c * http://xoshiro.di.unimi.it/splitmix64.c */ class xoshiro { public: using result_type = uint32_t; static constexpr result_type min() { return std::numeric_limits<result_type>::min(); } static constexpr result_type max() { return std::numeric_limits<result_type>::max(); } xoshiro() : xoshiro(std::random_device{}()) {} xoshiro(uint64_t seed) { uint64_t z = 0; for (int i = 0; i < 4; i++) { z = (seed += 0x9e3779b97f4a7c15), z = (z ^ (z >> 33)) * 0x62A9D9ED799705F5, z = (z ^ (z >> 28)) * 0xCB24D0A5C88C35B3, s[i] = static_cast<result_type>(z >> 32); } } result_type operator()() { const result_type result = rotl(s[1] * 5, 7) * 9, t = s[1] << 9; return s[2] ^= s[0], s[3] ^= s[1], s[1] ^= s[2], s[0] ^= s[3], s[2] ^= t, s[3] = rotl(s[3], 11), result; } void discard(const usize rep) { for (usize i = 0; i < rep; i++) { (*this)(); } } private: result_type s[4]; static result_type rotl(const result_type x, const int k) { return (x << k) | (x >> (32 - k)); } }; class xoshiro_64 { public: using result_type = uint64_t; static constexpr result_type min() { return std::numeric_limits<result_type>::min(); } static constexpr result_type max() { return std::numeric_limits<result_type>::max(); } xoshiro_64() : xoshiro_64(std::random_device{}()) {} xoshiro_64(uint64_t seed) { uint64_t z = 0; for (int i = 0; i < 4; i++) { z = (seed += 0x9e3779b97f4a7c15), z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9, z = (z ^ (z >> 27)) * 0x94d049bb133111eb, s[i] = static_cast<result_type>(z ^ (z >> 31)); } } result_type operator()() { const result_type result = rotl(s[1] * 5, 7) * 9, t = s[1] << 17; return s[2] ^= s[0], s[3] ^= s[1], s[1] ^= s[2], s[0] ^= s[3], s[2] ^= t, s[3] = rotl(s[3], 45), result; } void discard(const usize rep) { for (usize i = 0; i < rep; i++) { (*this)(); } } private: result_type s[4]; static result_type rotl(const result_type x, const int k) { return (x << k) | (x >> (64 - k)); } }; template<typename Rng> class rng_base { public: using rng_type = Rng; using result_type = typename rng_type::result_type; static constexpr result_type min() { return rng_type::min(); } static constexpr result_type max() { return rng_type::max(); } rng_base() : rng_base(std::random_device{}()) {} rng_base(const u64 seed) : rng(seed) {} ~rng_base() = default; result_type operator()(const result_type max = std::numeric_limits<result_type>::max()) { if (max == std::numeric_limits<result_type>::max()) { return static_cast<result_type>(rng()); } if (ispow2(max + 1)) { return static_cast<result_type>(rng() & max); } const result_type mask = static_cast<result_type>(ceil2(static_cast<u64>(max + 1))) - 1; while (true) { const result_type ans = static_cast<result_type>(rng() & mask); if (ans <= max) { return ans; } } } result_type operator()(const result_type min, const result_type max) { return min + (*this)(max - min); } operator bool() { return *this(0, 1); } template<typename Int> std::pair<Int, Int> pair(const Int min, const Int max, const bool sorted = false) { return sorted ? std::minmax(*this(min, max), *this(min, max)) : std::pair<Int, Int>{*this(min, max), *this(min, max)}; } template<typename Int> std::vector<Int> vec(const std::size_t size, const Int min, const Int max) { std::vector<Int> v(size); for (std::size_t i = 0; i < size; i++) { v[i] = *this(min, max); } return v; } std::vector<usize> perm(const usize n) { std::vector<usize> ans(n); std::iota(ans.begin(), ans.end(), 0UL); std::shuffle(ans.begin(), ans.end(), rng); return ans; } private: Rng rng; }; using rng_mt = rng_base<std::mt19937>; using rng_mt64 = rng_base<std::mt19937_64>; using rng_xoshiro = rng_base<xoshiro>; using rng_xoshiro64 = rng_base<xoshiro_64>; rng_mt g_rng_mt; rng_mt64 g_rng_mt64; rng_xoshiro g_rng_xo; rng_xoshiro64 g_rng_xo64; template<typename NodeData> class base_rbstree { private: class node { using ptr = node*; using const_ptr = const node* const; using tree = ptr; public: node() : node_data{} {} template<typename... Args> node(Args... args) : node_data{args...} {} usize size() const { return node_data.sz; } const NodeData& data() const { return node_data; } template<typename Operator> void act(const Operator& op) { node_data.act(op); } template<typename Value> void set(const Value& value) { node_data.set(value, data_ptr_of(l), data_ptr_of(r)), pull_up(); } friend std::ostream& operator<<(std::ostream& os, const node& n) { return os << "[" << n.node_data << "]"; } bool has_left() const { return l; } bool has_right() const { return r; } const node& left() const { return deptr(l); } const node& right() const { return deptr(r); } static node& deptr(ptr x) { return x ? (*x) : empty_node; } static ptr merge(tree tp1, tree tp2) { if (not tp1) { return tp2; } if (not tp2) { return tp1; } if (g_rng_xo(static_cast<uint>(size_of(tp1) + size_of(tp2) - 1)) < static_cast<uint>(size_of(tp1))) { return tp1->push_down(), tp1->r = merge(tp1->r, tp2), tp1->pull_up(), tp1; } else { return tp2->push_down(), tp2->l = merge(tp1, tp2->l), tp2->pull_up(), tp2; } } static std::pair<ptr, ptr> split_at(tree tp, const usize pos) { if (not tp) { return {nullptr, nullptr}; } tp->push_down(); if (pos == 0) { return std::make_pair(nullptr, tp); } if (pos == size_of(tp)) { return std::make_pair(tp, nullptr); } if (pos <= size_of(tp->l)) { auto ls = split_at(tp->l, pos); return tp->l = ls.second, tp->pull_up(), std::make_pair(ls.first, tp); } else { auto rs = split_at(tp->r, pos - size_of(tp->l) - 1); return tp->r = rs.first, tp->pull_up(), std::make_pair(tp, rs.second); } } static std::tuple<ptr, ptr, ptr> split_range(tree tp, const usize pos_min, const usize pos_sup) { auto ts = split_at(tp, pos_min), trs = split_at(ts.second, pos_sup - pos_min); return std::make_tuple(ts.first, trs.first, trs.second); } template<typename Key> static std::pair<ptr, ptr> split_lower(tree tp, const Key& key) { if (not tp) { return {nullptr, nullptr}; } const typename NodeData::comp_type& comp{}; if (comp(tp->data().key, key)) { auto rs = split_lower(tp->r, key); return tp->r = rs.first, tp->pull_up(), std::make_pair(tp, rs.second); } else { auto ls = split_lower(tp->l, key); return tp->l = ls.second, tp->pull_up(), std::make_pair(ls.first, tp); } } template<typename Key> static std::pair<ptr, ptr> split_upper(tree tp, const Key& key) { if (not tp) { return {nullptr, nullptr}; } const typename NodeData::comp_type& comp{}; if (comp(key, tp->data().key)) { auto ls = split_upper(tp->l, key); return tp->l = ls.second, tp->pull_up(), std::make_pair(ls.first, tp); } else { auto rs = split_upper(tp->r, key); return tp->r = rs.first, tp->pull_up(), std::make_pair(tp, rs.second); } } template<typename Key> static std::tuple<ptr, ptr, ptr> split_key_range(tree tp, const Key& key_min, const Key& key_max) { auto ts = split_lower(tp, key_min), trs = split_upper(ts.second, key_max); return std::make_tuple(ts.first, trs.first, trs.second); } private: static NodeData* data_ptr_of(ptr x) { return x ? &(x->node_data) : nullptr; } static usize size_of(const_ptr x) { return x ? x->size() : 0UL; } void pull_up() { node_data.pull_up(data_ptr_of(l), data_ptr_of(r)); } void push_down() { node_data.push_down(data_ptr_of(l), data_ptr_of(r)); } static node empty_node; // inline変数を使いたいでござる NodeData node_data; ptr l = nullptr, r = nullptr; }; using ptr = node*; ptr root = nullptr; public: base_rbstree() = default; base_rbstree(const ptr r) : root{r} {} template<typename... Args> base_rbstree(Args... args) : root{new node{args...}} {} bool empty() const { return not root; } usize size() const { return node::size(root); } const node& top() const { return node::deptr(root); } node at(const usize pos) { return fold_range(pos, pos + 1); } node fold_range(const usize pos_min, const usize pos_sup) { auto ts = node::split_range(root, pos_min, pos_sup); const node ans = node::deptr(std::get<1>(ts)); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), ans; } template<typename Key> node fold_key_range(const Key& key_min, const Key& key_max) { auto ts = node::split_key_range(root, key_min, key_max); const node ans = node::deptr(std::get<1>(ts)); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), ans; } template<typename Key> node fold_lower(const Key& key) { auto ts = node::split_lower(root, key); const node ans = node::deptr(ts.first); return root = node::merge(ts.first, ts.second), ans; } template<typename Key> node fold_upper(const Key& key) { auto ts = node::split_upper(root, key); const node ans = node::deptr(ts.first); return root = node::merge(ts.first, ts.second), ans; } template<typename Value> base_rbstree& set_at(const usize pos, const Value& value) { auto ts = node::split_range(root, pos, pos + 1); std::get<1>(ts)->set(value); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), *this; } template<typename Key, typename Value> base_rbstree& set(const Key& key, const Value& value) { auto ts = node::split_key_range(root, key, key); std::get<1>(ts)->set(value); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), *this; } template<typename Op> base_rbstree& act_range(const usize pos_min, const usize pos_sup, const Op& op) { auto ts = node::split_range(root, pos_min, pos_sup); std::get<1>(ts)->act(op); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), *this; } template<typename Key, typename Op> base_rbstree& act_key_range(const Key& key_min, const Key& key_max, const Op& op) { auto ts = node::split_key_range(root, key_min, key_max); std::get<1>(ts)->act(op); return root = node::merge(std::get<0>(ts), node::merge(std::get<1>(ts), std::get<2>(ts))), *this; } base_rbstree& merge(base_rbstree&& t) { return root = node::merge(root, t.root), *this; } base_rbstree split_at(const usize pos) { auto ts = node::split_at(root, pos); return root = ts.first, base_rbstree(ts.second); } std::pair<base_rbstree, base_rbstree> split_range(const usize pos_min, const usize pos_sup) { auto ts = node::split_range(root, pos_min, pos_sup); return root = std::get<0>(ts), std::make_pair(base_rbstree(std::get<1>(ts)), base_rbstree(std::get<2>(ts))); } base_rbstree& erase_at(const usize pos) { auto ts = node::split_at(root, pos), trs = node::split_at(ts.second, 1); return root = node::merge(ts.first, trs.second), *this; } base_rbstree& insert_at(const usize pos, base_rbstree&& t) { auto ts = node::split_at(root, pos); return root = node::merge(node::merge(ts.first, t.root), ts.second), *this; } template<typename Key> base_rbstree split_lower(const Key& key) { auto ts = node::split_lower(root, key); return root = ts.first, base_rbstree(ts.second); } template<typename Key> base_rbstree split_upper(const Key& key) { auto ts = node::split_upper(root, key); return root = ts.first, base_rbstree(ts.second); } template<typename Key> std::pair<base_rbstree, base_rbstree> split_key_range(const Key& key_min, const Key& key_max) { auto ts = node::split_key_range(root, key_min, key_max); return root = std::get<0>(ts), std::make_pair(base_rbstree(std::get<1>(ts)), base_rbstree(std::get<2>(ts))); } base_rbstree& insert(const node& n) { auto ts = node::split_lower(root, n.data().key); return root = node::merge(node::merge(ts.first, new node{n}), ts.second), *this; } std::vector<node> data() { if (empty()) { return std::vector<node>{}; } auto dfs = [&](auto&& self, const node& n) -> std::vector<node> { std::vector<node> ans; if (n.has_left()) { for (auto&& e : self(self, n.left())) { ans.emplace_back(e); } } ans.push_back(n); if (n.has_right()) { for (auto&& e : self(self, n.right())) { ans.emplace_back(e); } } return ans; }; return dfs(dfs, *root); } }; template<typename NodeData> typename base_rbstree<NodeData>::node base_rbstree<NodeData>::node::empty_node = node{}; template<typename Value> using rbstree = base_rbstree<bbst_node::value_node<Value>>; template<typename Key, typename Comp = std::less<Key>> using mset_rbstree = base_rbstree<bbst_node::key_node<Key, bbst_node::node, Comp>>; template<typename Key, typename Value, typename Comp = std::less<Key>> using mmap_rbstree = base_rbstree<bbst_node::key_node<Key, bbst_node::value_node<Value>, Comp>>; template<typename ValueMonoid> using merge_rbstree = base_rbstree<bbst_node::merge_node<ValueMonoid>>; template<typename MonoidAct> using lazy_rbstree = base_rbstree<bbst_node::lazy_node<MonoidAct>>; class segments { private: using P = std::pair<usize, usize>; const usize ceil; std::vector<P> rs; const usize num; // 区間0は存在しないので、実際は区間1,2,...,num-1 public: segments(const usize sz) : ceil{ceil2(sz)}, rs(ceil << 1, P{0, 0}), num{ceil << 1} { for (usize sz = 1; sz <= ceil; sz <<= 1) { const usize len = ceil / sz; for (usize j = sz; j < (sz << 1); j++) { rs[j] = {len * (j - sz), len * (j - sz + 1)}; } } } std::vector<usize> under(usize l, usize r) const { assert(l < r), assert(r <= ceil); std::vector<usize> lind, rind; for (l += ceil, r += ceil; l < r; l >>= 1, r >>= 1) { if (l & 1) { lind.push_back(l++); } if (r & 1) { rind.push_back(--r); } } for (; not rind.empty(); rind.pop_back()) { lind.push_back(rind.back()); } return lind; } std::vector<usize> over(const usize a) const { assert(a < ceil); std::vector<usize> ans; for (usize i = a + ceil; i >= 1; i >>= 1) { ans.push_back(i); } std::reverse(ans.begin(), ans.end()); return ans; } usize max_index() const { return num; } const P& operator[](const usize i) const { assert(i >= 1), assert(i < 2 * ceil); return rs[i]; } }; template<typename Element> struct plus { using element_type = Element; using operator_type = element_type; plus() = delete; static operator_type compose(const operator_type& a, const operator_type& b) { return a + b; } static constexpr operator_type id() { return operator_type{}; } }; template<typename Element> struct sum { using element_type = Element; using value_type = element_type; sum() = delete; static value_type merge(const value_type& a, const value_type& b) { return a + b; } static constexpr value_type id() { return value_type{}; } }; template<typename ValueElement, typename OperatorElement> struct sum_plus { using value_element_type = ValueElement; using operator_element_type = OperatorElement; using value_monoid_type = sum<value_element_type>; using operator_monoid_type = plus<operator_element_type>; using value_type = typename value_monoid_type::value_type; using operator_type = typename operator_monoid_type::operator_type; sum_plus() = delete; template<typename Ind> static value_type apply(const operator_type& f, const value_type& x, const Ind l) { return x + static_cast<value_type>(l) * static_cast<value_type>(f); } }; enum { NOTFOUND = 0xFFFFFFFFFFFFFFFFLLU }; uint64_t NODE_NO = 0; class Node { public: uint64_t no; // node番号 // internal nodeのときに使用 uint64_t num; // 左の子の部分木のもつbitの数 uint64_t ones; // 左の子の部分木のもつ1の数 Node* left; Node* right; int64_t balance; // 右の子の高さ - 左の子の高さ.+なら右の子の方が高い,-なら左の子の方が高い // leafのときに使用 uint64_t bits; // bit uint64_t bits_size; // bitのサイズ(debug用) bool is_leaf; Node(uint64_t bits, uint64_t bits_size, bool is_leaf) : no(NODE_NO++), num(0), ones(0), bits(bits), bits_size(bits_size), is_leaf(is_leaf), left(nullptr), right(nullptr), balance(0) {} }; class DynamicBitVector { public: Node* root; uint64_t size; // 全体のbitの数 uint64_t num_one; // 全体の1の数 const uint64_t bits_size_limit = 32; // 各ノードが管理するbitの長さ制限.2 * bits_size_limit以上になったらノードを分割し, 1/2*bits_size_limit以下になったらノードを結合する DynamicBitVector() : root(nullptr), size(0), num_one(0) {} DynamicBitVector(std::vector<uint64_t>& v) : root(nullptr), size(0), num_one(0) { if (v.size() == 0) { return; } std::deque<std::pair<Node*, uint64_t>> leaves; for (int i = 0; i < v.size(); i += this->bits_size_limit) { uint64_t bits = 0; const uint64_t bits_size = std::min(this->bits_size_limit, (uint64_t)v.size() - i); for (int j = 0; j < bits_size; ++j) { assert(v[i + j] == 0 or v[i + j] == 1); if (v[i + j] == 1) { bits |= (uint64_t)1 << j; } } leaves.emplace_back(std::make_pair(new Node(bits, bits_size, true), bits_size)); } std::deque<std::tuple<Node*, uint64_t, uint64_t, uint64_t>> nodes; // (node, 全体のbit数, 全体の1の数, 高さ) while (not leaves.empty()) { const auto node = leaves.front().first; const auto bits_size = leaves.front().second; leaves.pop_front(); nodes.emplace_back(std::make_tuple(node, bits_size, popCount(node->bits), 0)); } while (nodes.size() > 1) { std::deque<std::tuple<Node*, uint64_t, uint64_t, uint64_t>> next_nodes; while (not nodes.empty()) { // あまりがでたときは,最後に作った中間ノードと結合させるためにnodesに戻す if (nodes.size() == 1) { nodes.push_front(next_nodes.back()); next_nodes.pop_back(); } Node* left_node; uint64_t left_num, left_ones, left_height; std::tie(left_node, left_num, left_ones, left_height) = nodes.front(); nodes.pop_front(); Node* right_node; uint64_t right_num, right_ones, right_height; std::tie(right_node, right_num, right_ones, right_height) = nodes.front(); nodes.pop_front(); const auto internal_node = new Node(0, 0, false); internal_node->num = left_num; internal_node->ones = left_ones; internal_node->left = left_node; internal_node->right = right_node; internal_node->balance = right_height - left_height; next_nodes.emplace_back(std::make_tuple(internal_node, left_num + right_num, left_ones + right_ones, std::max(left_height, right_height) + 1)); } nodes = next_nodes; } uint64_t height; std::tie(this->root, this->size, this->num_one, height) = nodes.front(); nodes.pop_front(); assert(this->size == v.size()); } // B[pos] uint64_t access(uint64_t pos) { assert(pos < this->size); return access(this->root, pos); } // B[0, pos)にある指定されたbitの数 uint64_t rank(uint64_t bit, uint64_t pos) { assert(bit == 0 or bit == 1); assert(pos <= this->size); if (bit) { return rank1(this->root, pos, 0); } else { return pos - rank1(this->root, pos, 0); } } // rank番目の指定されたbitの位置 + 1(rankは1-origin) uint64_t select(uint64_t bit, uint64_t rank) { assert(bit == 0 or bit == 1); assert(rank > 0); if (bit == 0 and rank > this->size - this->num_one) { return NOTFOUND; } if (bit == 1 and rank > this->num_one) { return NOTFOUND; } if (bit) { return select1(this->root, 0, rank); } else { return select0(this->root, 0, rank); } } // posにbitを挿入する void insert(uint64_t pos, uint64_t bit) { assert(bit == 0 or bit == 1); assert(pos <= this->size); // 現在もってるbitsの末尾には挿入できる if (root == nullptr) { root = new Node(bit, 1, true); } else { insert(this->root, nullptr, bit, pos, this->size); } this->size++; this->num_one += (bit == 1); } // posを削除する void erase(uint64_t pos) { assert(pos < this->size); uint64_t bit = this->remove(this->root, nullptr, pos, this->size, 0, true).first; this->size--; this->num_one -= (bit == 1); } private: uint64_t access(const Node* node, uint64_t pos) { if (node->is_leaf) { assert(pos <= 2 * this->bits_size_limit); return (node->bits >> pos) & (uint64_t)1; } if (pos < node->num) { return this->access(node->left, pos); } else { return this->access(node->right, pos - node->num); } } uint64_t rank1(const Node* node, uint64_t pos, uint64_t ones) { if (node->is_leaf) { assert(node->bits_size >= pos); const uint64_t mask = ((uint64_t)1 << pos) - 1; return ones + popCount(node->bits & mask); } if (pos < node->num) { return this->rank1(node->left, pos, ones); } else { return this->rank1(node->right, pos - node->num, ones + node->ones); } } uint64_t select1(const Node* node, uint64_t pos, uint64_t rank) { if (node->is_leaf) { return pos + this->selectInBlock(node->bits, rank) + 1; } if (rank <= node->ones) { return this->select1(node->left, pos, rank); } else { return this->select1(node->right, pos + node->num, rank - node->ones); } } uint64_t select0(const Node* node, uint64_t pos, uint64_t rank) { if (node->is_leaf) { return pos + this->selectInBlock(~node->bits, rank) + 1; } if (rank <= (node->num - node->ones)) { return this->select0(node->left, pos, rank); } else { return this->select0(node->right, pos + node->num, rank - (node->num - node->ones)); } } // bits(64bit)のrank番目(0-index)の1の数 uint64_t selectInBlock(uint64_t bits, uint64_t rank) { const uint64_t x1 = bits - ((bits & 0xAAAAAAAAAAAAAAAALLU) >> (uint64_t)1); const uint64_t x2 = (x1 & 0x3333333333333333LLU) + ((x1 >> (uint64_t)2) & 0x3333333333333333LLU); const uint64_t x3 = (x2 + (x2 >> (uint64_t)4)) & 0x0F0F0F0F0F0F0F0FLLU; uint64_t pos = 0; for (;; pos += 8) { const uint64_t rank_next = (x3 >> pos) & 0xFFLLU; if (rank <= rank_next) break; rank -= rank_next; } const uint64_t v2 = (x2 >> pos) & 0xFLLU; if (rank > v2) { rank -= v2; pos += 4; } const uint64_t v1 = (x1 >> pos) & 0x3LLU; if (rank > v1) { rank -= v1; pos += 2; } const uint64_t v0 = (bits >> pos) & 0x1LLU; if (v0 < rank) { pos += 1; } return pos; } // nodeから辿れる葉のpos番目にbitをいれる(葉のbitの長さはlen) // 高さの変化を返す int64_t insert(Node* node, Node* parent, uint64_t bit, uint64_t pos, uint64_t len) { assert(bit == 0 or bit == 1); if (node->is_leaf) { assert(pos <= 2 * this->bits_size_limit); // posより左をとりだす const uint64_t up_mask = (((uint64_t)1 << (len - pos)) - 1) << pos; const uint64_t up = (node->bits & up_mask); // posより右をとりだす const uint64_t down_mask = ((uint64_t)1 << pos) - 1; const uint64_t down = node->bits & down_mask; node->bits = (up << (uint64_t)1) | (bit << pos) | down; node->bits_size++; assert(node->bits_size == len + 1); // bitsのサイズが大きくなったので分割する if (len + 1 >= 2 * bits_size_limit) { this->splitNode(node, len + 1); // 引数のlenはinsert後の長さなので+1する return 1; } return 0; } if (pos < node->num) { const int64_t diff = this->insert(node->left, node, bit, pos, node->num); node->num += 1; node->ones += (bit == 1); return achieveBalance(parent, node, diff, 0); } else { const int64_t diff = this->insert(node->right, node, bit, pos - node->num, len - node->num); return achieveBalance(parent, node, 0, diff); } } // nodeのpos番目のbitを削除する // 消したbitと高さの変化のpairを返す std::pair<uint64_t, int64_t> remove(Node* node, Node* parent, uint64_t pos, uint64_t len, uint64_t ones, bool allow_under_flow) { if (node->is_leaf) { // 消すとunder flowになるので消さない if (node != this->root and len <= bits_size_limit / 2 and not allow_under_flow) { return std::make_pair(NOTFOUND, 0); } assert(pos <= 2 * this->bits_size_limit); assert(pos < len); const uint64_t bit = (node->bits >> pos) & (uint64_t)1; // posより左をとりだす(posを含まないようにする) const uint64_t up_mask = (((uint64_t)1 << (len - pos - 1)) - 1) << (pos + 1); const uint64_t up = (node->bits & up_mask); // posより右をとりだす const uint64_t down_mask = ((uint64_t)1 << pos) - 1; const uint64_t down = node->bits & down_mask; node->bits = (up >> (uint64_t)1) | down; node->bits_size--; assert(node->bits_size == len - 1); return std::make_pair(bit, 0); } if (pos < node->num) { const auto bit_diff = this->remove(node->left, node, pos, node->num, node->ones, allow_under_flow); if (bit_diff.first == NOTFOUND) { return bit_diff; } node->ones -= (bit_diff.first == 1); // 左の子の葉のbitを1つ消したのでunder flowが発生している if (node->num == bits_size_limit / 2) { const auto b_d = remove(node->right, node, 0, len - bits_size_limit / 2, 0, false); // 右の葉の先頭bitを削る // 右の葉もunder flowになって消せない場合は2つの葉を統合する if (b_d.first == NOTFOUND) { assert(node->left->is_leaf); assert(node->left->bits_size == bits_size_limit / 2 - 1); // 右の子から辿れる一番左の葉の先頭にleftのbitsを追加する mergeNodes(node->right, 0, len - bits_size_limit / 2, node->left->bits, bits_size_limit / 2 - 1, node->ones, true); this->replace(parent, node, node->right); // parentの子のnodeをnode->rightに置き換える return std::make_pair(bit_diff.first, -1); } // 右の葉からとった先頭bitを左の葉の末尾にいれる assert(node->left->bits_size == bits_size_limit / 2 - 1); insert(node->left, node, b_d.first, bits_size_limit / 2 - 1, bits_size_limit / 2 - 1); node->ones += (b_d.first == 1); } else { node->num -= 1; } const int64_t diff = achieveBalance(parent, node, bit_diff.second, 0); return std::make_pair(bit_diff.first, diff); } else { const auto bit_diff = this->remove(node->right, node, pos - node->num, len - node->num, ones - node->ones, allow_under_flow); if (bit_diff.first == NOTFOUND) { return bit_diff; } ones -= (bit_diff.first == 1); // 右の子の葉のbitを1つ消したのでunder flowが発生する if ((len - node->num) == bits_size_limit / 2) { const auto b_d = remove(node->left, node, node->num - 1, node->num, 0, false); // 左の葉の末尾をbitを削る // 左の葉もunder flowになって消せない場合は2つの葉を統合する if (b_d.first == NOTFOUND) { assert(node->right->is_leaf); assert(node->right->bits_size == bits_size_limit / 2 - 1); // 左の子から辿れる一番右の葉の末尾にleftのbitsを追加する mergeNodes(node->left, node->num, node->num, node->right->bits, bits_size_limit / 2 - 1, ones - node->ones, false); this->replace(parent, node, node->left); // parentの子のnodeをnode->leftに置き換える return std::make_pair(bit_diff.first, -1); } // 左の葉からとった最後尾bitを右の葉の先頭にいれる insert(node->right, node, b_d.first, 0, bits_size_limit / 2 - 1); node->num -= 1; node->ones -= (b_d.first == 1); } const int64_t diff = achieveBalance(parent, node, 0, bit_diff.second); return std::make_pair(bit_diff.first, diff); } } // nodeを2つの葉に分割する void splitNode(Node* node, uint64_t len) { assert(node->is_leaf); assert(node->bits_size == len); // 左の葉 const uint64_t left_size = len / 2; const uint64_t left_bits = node->bits & (((uint64_t)1 << left_size) - 1); node->left = new Node(left_bits, left_size, true); // 右の葉 const uint64_t right_size = len - left_size; const uint64_t right_bits = node->bits >> left_size; node->right = new Node(right_bits, right_size, true); // nodeを内部ノードにする node->is_leaf = false; node->num = left_size; node->ones = this->popCount(left_bits); node->bits = 0; node->bits_size = 0; } // nodeから辿れる葉のpos番目にbitsを格納する void mergeNodes(Node* node, uint64_t pos, uint64_t len, uint64_t bits, uint64_t s, uint64_t ones, bool left) { if (node->is_leaf) { if (left) { node->bits = (node->bits << s) | bits; } else { assert(len == node->bits_size); node->bits = node->bits | (bits << len); } node->bits_size += s; return; } if (pos < node->num) { node->num += s; node->ones += ones; mergeNodes(node->left, pos, node->num, bits, s, ones, left); } else { mergeNodes(node->right, pos, len - node->num, bits, s, ones, left); } } // nodeの左の高さがleftHeightDiffだけ変わり,右の高さがrightHeightDiffだけ変わったときにnodeを中心に回転させる // 高さの変化を返す int64_t achieveBalance(Node* parent, Node* node, int64_t leftHeightDiff, int64_t rightHeightDiff) { assert(-1 <= node->balance and node->balance <= 1); assert(-1 <= leftHeightDiff and leftHeightDiff <= 1); assert(-1 <= rightHeightDiff and rightHeightDiff <= 1); if (leftHeightDiff == 0 and rightHeightDiff == 0) { return 0; } int64_t heightDiff = 0; // 左が高いときに,左が高くなる or 右が高いときに右が高くなる if ((node->balance <= 0 and leftHeightDiff > 0) or (node->balance >= 0 and rightHeightDiff > 0)) { ++heightDiff; } // 左が高いときに左が低くなる or 右が高いときに右が低くなる if ((node->balance < 0 and leftHeightDiff < 0) or (node->balance > 0 and rightHeightDiff < 0)) { --heightDiff; } node->balance += -leftHeightDiff + rightHeightDiff; assert(-2 <= node->balance and node->balance <= 2); // 左が2高い if (node->balance == -2) { assert(-1 <= node->left->balance and node->left->balance <= 1); if (node->left->balance != 0) { heightDiff--; } if (node->left->balance == 1) { replace(node, node->left, rotateLeft(node->left)); } replace(parent, node, rotateRight(node)); } // 右が2高い else if (node->balance == 2) { assert(-1 <= node->right->balance and node->right->balance <= 1); if (node->right->balance != 0) { heightDiff--; } if (node->right->balance == -1) { replace(node, node->right, rotateRight(node->right)); } replace(parent, node, rotateLeft(node)); } return heightDiff; } // node Bを中心に左回転する.新しい親を返す Node* rotateLeft(Node* B) { Node* D = B->right; const int64_t heightC = 0; const int64_t heightE = heightC + D->balance; const int64_t heightA = std::max(heightC, heightE) + 1 - B->balance; B->right = D->left; D->left = B; B->balance = heightC - heightA; D->num += B->num; D->ones += B->ones; D->balance = heightE - (std::max(heightA, heightC) + 1); assert(-2 <= B->balance and B->balance <= 2); assert(-2 <= D->balance and D->balance <= 2); return D; } // node Dを中心に右回転する.新しい親を返す Node* rotateRight(Node* D) { Node* B = D->left; const int64_t heightC = 0; const int64_t heightA = heightC - B->balance; const int64_t heightE = std::max(heightA, heightC) + 1 + D->balance; D->left = B->right; B->right = D; D->num -= B->num; D->ones -= B->ones; D->balance = heightE - heightC; B->balance = std::max(heightC, heightE) + 1 - heightA; assert(-2 <= B->balance and B->balance <= 2); assert(-2 <= D->balance and D->balance <= 2); return B; } // parentの子のoldNodeをnewNodeに置き換える void replace(Node* parent, Node* oldNode, Node* newNode) { if (parent == nullptr) { this->root = newNode; return; } if (parent->left == oldNode) { parent->left = newNode; } else if (parent->right == oldNode) { parent->right = newNode; } else { throw "old node is not child"; } } uint64_t popCount(uint64_t x) { return popcount(x); } }; class DynamicWaveletMatrix { public: std::vector<DynamicBitVector> bit_arrays; std::vector<uint64_t> begin_one; // 各bitの1の開始位置 uint64_t size; // 与えられた配列のサイズ uint64_t maximum_element; // 最大の文字 uint64_t bit_size; // 文字を表すのに必要なbit数 public: // max_element: 入ってくる中で一番大きい数値 DynamicWaveletMatrix(uint64_t maximum_element) : size(0), maximum_element(maximum_element + 1) { this->bit_size = this->get_num_of_bit(maximum_element); if (bit_size == 0) { bit_size = 1; } this->begin_one.resize(bit_size); for (uint64_t i = 0; i < bit_size; ++i) { DynamicBitVector sv; bit_arrays.push_back(sv); } } DynamicWaveletMatrix(uint64_t num_of_alphabet, const std::vector<uint64_t>& array) : size(0), maximum_element(num_of_alphabet + 1) { this->bit_size = this->get_num_of_bit(num_of_alphabet); if (bit_size == 0) { bit_size = 1; } this->begin_one.resize(bit_size); if (array.size() == 0) { for (uint64_t i = 0; i < bit_size; ++i) { DynamicBitVector sv; bit_arrays.push_back(sv); } return; } size = array.size(); std::vector<uint64_t> v(array), b(array.size(), 0); for (uint64_t i = 0; i < bit_size; ++i) { std::vector<uint64_t> temp; // 0をtempにいれてく for (uint64_t j = 0; j < v.size(); ++j) { uint64_t c = v.at(j); uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit if (bit == 0) { temp.push_back(c); b[j] = 0; } } this->begin_one.at(i) = temp.size(); // 1をtempにいれてく for (uint64_t j = 0; j < v.size(); ++j) { uint64_t c = v.at(j); uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit if (bit == 1) { temp.push_back(c); b[j] = 1; } } DynamicBitVector dbv(b); bit_arrays.emplace_back(dbv); v = temp; } } // v[pos] uint64_t access(uint64_t pos) { if (pos >= this->size) { return NOTFOUND; } uint64_t c = 0; for (uint64_t i = 0; i < bit_arrays.size(); ++i) { uint64_t bit = bit_arrays.at(i).access(pos); // もとの数値がのi番目のbit c = (c <<= 1) | bit; pos = bit_arrays.at(i).rank(bit, pos); if (bit) { pos += this->begin_one.at(i); } } return c; } // i番目のcの位置 + 1を返す。rankは1-origin uint64_t select(uint64_t c, uint64_t rank) { assert(rank > 0); if (c >= maximum_element) { return NOTFOUND; } uint64_t left = 0; for (uint64_t i = 0; i < bit_size; ++i) { const uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit left = bit_arrays.at(i).rank(bit, left); // cのi番目のbitと同じ数値の数 if (bit) { left += this->begin_one.at(i); } } uint64_t index = left + rank; for (uint64_t i = 0; i < bit_arrays.size(); ++i) { uint64_t bit = ((c >> i) & 1); // 下からi番目のbit if (bit == 1) { index -= this->begin_one.at(bit_size - i - 1); } index = this->bit_arrays.at(bit_size - i - 1).select(bit, index); } return index; } // posにcを挿入する void insert(uint64_t pos, uint64_t c) { assert(pos <= this->size); for (uint64_t i = 0; i < bit_arrays.size(); ++i) { const uint64_t bit = (c >> (bit_size - i - 1)) & 1; // 上からi番目のbit bit_arrays.at(i).insert(pos, bit); pos = bit_arrays.at(i).rank(bit, pos); if (bit) { pos += this->begin_one.at(i); } else { this->begin_one.at(i)++; } } this->size++; } // posを削除する void erase(uint64_t pos) { assert(pos < this->size); if (pos >= this->size) { throw "Segmentation fault"; } for (uint64_t i = 0; i < bit_arrays.size(); ++i) { uint64_t bit = bit_arrays.at(i).access(pos); // もとの数値のi番目のbit auto next_pos = bit_arrays.at(i).rank(bit, pos); bit_arrays.at(i).erase(pos); if (bit) { next_pos += this->begin_one.at(i); } else { this->begin_one.at(i)--; } pos = next_pos; } this->size--; } // posにcをセットする void update(uint64_t pos, uint64_t c) { assert(pos < this->size); this->erase(pos); this->insert(pos, c); } // v[begin_pos, end_pos)で最小値のindexを返す uint64_t minRange(uint64_t begin_pos, uint64_t end_pos) { return quantileRange(begin_pos, end_pos, 0); } // v[begin_pos, end_pos)でk番目に小さい数値のindexを返す(kは0-origin) // つまり小さい順に並べてk番目の値 uint64_t quantileRange(uint64_t begin_pos, uint64_t end_pos, uint64_t k) { if ((end_pos > size || begin_pos >= end_pos) || (k >= end_pos - begin_pos)) { return NOTFOUND; } uint64_t val = 0; for (uint64_t i = 0; i < bit_size; ++i) { uint64_t num_of_zero_begin = bit_arrays.at(i).rank(0, begin_pos); uint64_t num_of_zero_end = bit_arrays.at(i).rank(0, end_pos); uint64_t num_of_zero = num_of_zero_end - num_of_zero_begin; // beginからendまでにある0の数 uint64_t bit = (k < num_of_zero) ? 0 : 1; // k番目の値の上からi番目のbitが0か1か if (bit) { k -= num_of_zero; begin_pos = this->begin_one.at(i) + begin_pos - num_of_zero_begin; end_pos = this->begin_one.at(i) + end_pos - num_of_zero_end; } else { begin_pos = num_of_zero_begin; end_pos = num_of_zero_begin + num_of_zero; } val = ((val << 1) | bit); } uint64_t left = 0; for (uint64_t i = 0; i < bit_size; ++i) { const uint64_t bit = (val >> (bit_size - i - 1)) & 1; // 上からi番目のbit left = bit_arrays.at(i).rank(bit, left); // cのi番目のbitと同じ数値の数 if (bit) { left += this->begin_one.at(i); } } uint64_t rank = begin_pos + k - left + 1; return select(val, rank) - 1; } private: uint64_t get_num_of_bit(uint64_t x) { if (x == 0) return 0; x--; uint64_t bit_num = 0; while (x >> bit_num) { ++bit_num; } return bit_num; } }; int main() { constexpr ll MAX = 1LL << 40; std::cin.tie(nullptr), std::ios::sync_with_stdio(false); using node = bbst_node::key_node<ull, bbst_node::lazy_node<sum_plus<ull, ull>>, std::less<ull>>; using tree = base_rbstree<node>; const auto [n, q] = reads<usize, usize>(); const auto m = ceil2(n); auto a = read<uint64_t>(n); std::vector<tree> sorted(2 * m); DynamicWaveletMatrix wm((1UL << 40) - 1, a); auto median = [&](const usize l, const usize r) { return wm.access(wm.quantileRange(l, r, (r - l) / 2)); }; // stopwatch sw; segments segs{n}; for (usize i = 1; i < 2 * m; i++) { const auto [l, r] = segs[i]; for (usize j = l; j < std::min(r, n); j++) { sorted[i].insert({a[j], a[j]}); } } // std::cerr << sw.rap() << "ms\n"; ull s = 0; constexpr ull pmask = (1ULL << 16) - 1; constexpr ull vmask = (1ULL << 40) - 1; for (usize i = 0; i < q; i++) { const usize t = read<usize>(); if (t == 1) { const usize sp = s & pmask; const ull sv = s & vmask; const auto x = (read<usize>() ^ (sp)) - 1; const auto y = (read<ull>() ^ sv); const auto inds = segs.over(x); for (const usize i : inds) { auto& lst = sorted[i]; auto rst = lst.split_lower(a[x]); auto rst2 = rst.split_at(1); lst.merge(std::move(rst2)); lst.insert({y, y}); } wm.update(x, y); a[x] = y; // std::cerr << "Q1: " << sw.rap<std::chrono::microseconds>() << "us\n"; } else { const usize sp = s & pmask; auto l = (read<usize>() ^ (sp)), r = (read<usize>() ^ (sp)); if (l > r) { std::swap(l, r); } l--; const usize sz = r - l; const auto inds = segs.under(l, r); // auto less = [&](const ll x) -> usize { // usize ans = 0; // for (const usize i : inds) { ans += sorted[i].fold_lower(x).data().sz; } // return ans; // }; // ll inf = -1LL, sup = MAX; // while (sup - inf > 1) { // const ll mid = (inf + sup) / 2LL; // (less(mid) < (sz + 1) / 2 ? inf : sup) = mid; // } // const ll m = inf; //std::cerr << "Q2 binsearch: " << sw.rap<std::chrono::microseconds>() << "us\n"; const ull m = median(l, r); ull ans = 0; for (const usize i : inds) { auto& lst = sorted[i]; auto rst = sorted[i].split_lower(m); const usize l = lst.top().data().sz; const usize r = rst.top().data().sz; const ull lsum = (ull)l * m - lst.top().data().merged; const ull rsum = rst.top().data().merged - (ull)r * m; ans += (lsum + rsum); lst.merge(std::move(rst)); } s ^= ans; // std::cerr << "Q2 query: " << sw.rap<std::chrono::microseconds>() << "us\n"; std::cout << ans << "\n"; } } // std::cerr << sw.total() << "ms" << std::endl; return 0; }