// #include // #include "template.hpp" #include /* #expanded [utility/functional.hpp] */ #include #include namespace lib {namespace internal {template constexpr T plus(const T a, const T b) { return std::plus{}(a, b); }template constexpr T minus(const T a, const T b) { return std::minus{}(a, b); }template constexpr T bitxor(const T a, const T b) { return a xor b; }}template inline auto mod(T1 x, T2 r) { return (x%r+r)%r; }template inline bool chmax(T1 &a, T2 b) { return (a inline bool chmin(T1 &a, T2 b) { return (a>b ? a=b, true : false); }template inline constexpr T sign(const T x) {return (x > 0) - (x < 0);}template inline constexpr T mapping(const T x) {return (x - FROM_MIN) * (TO_MAX - TO_MIN) / (FROM_MAX - FROM_MIN) + TO_MIN;}template inline constexpr T mapping(const T x, const T from_min, const T from_max, const T to_min, const T to_max) {return (x - from_min) * (to_max - to_min) / (from_max - from_min) + to_min;}} /* [utility/functional.hpp] */ /* #expanded [snippet/iterations.hpp] */ #include /* #expanded [snippet/internal/overload.hpp] */ #define $OVERLOAD2(arg0, arg1, cmd, ...) cmd #define $OVERLOAD3(arg0, arg1, arg2, cmd, ...) cmd #define $OVERLOAD4(arg0, arg1, arg2, arg3, cmd, ...) cmd #define $OVERLOAD5(arg0, arg1, arg2, arg3, arg4, cmd, ...) cmd /* [snippet/internal/overload.hpp] */ #define LOOP(n) REPI($_, (n)) #define REPI(i,n) for(int i=0, i##_length=static_cast(n); i i=(l), i##_last=(r); i i=(l), i##_last=(r); i=0;) #define REPB(i,l,r) for(std::common_type_t i=(r), i##_last=(l); --i>=i##_last;) #define REPRS(i,l,r,s) for(std::common_type_t i=(l)+((r)-(l)-1)/(s)*(s), i##_last=(l); i>=i##_last; (i-=(s))) #define REP(...) $OVERLOAD4(__VA_ARGS__, REPIS, REPF, REPI, LOOP)(__VA_ARGS__) #define REPD(...) $OVERLOAD4(__VA_ARGS__, REPRS, REPB, REPR)(__VA_ARGS__) #define FORO(i,n) for(int i=0, i##_last=static_cast(n); i<=i##_last; ++i) #define FORI(i,l,r) for(std::common_type_t i=(l), i##_last=(r); i<=i##_last; ++i) #define FORIS(i,l,r,s) for(std::common_type_t i=(l), i##_last=(r); i<=i##_last; i+=(s)) #define FORRO(i,n) for(auto i=(n); i>=0; --i) #define FORR(i,l,r) for(std::common_type_t i=(r), i##_last=(l); i>=i##_last; --i) #define FORRS(i,l,r,s) for(std::common_type_t i=(l)+((r)-(l))/(s)*(s), i##_last=(l); i>=i##_last; i-=(s)) #define FOR(...) $OVERLOAD4(__VA_ARGS__, FORIS, FORI, FORO)(__VA_ARGS__) #define FORD(...) $OVERLOAD4(__VA_ARGS__, FORRS, FORR, FORRO)(__VA_ARGS__) #define ITR1(e0,v) for(const auto &e0 : (v)) #define ITRP1(e0,v) for(auto e0 : (v)) #define ITRR1(e0,v) for(auto &e0 : (v)) #define ITR2(e0,e1,v) for(const auto [e0, e1] : (v)) #define ITRP2(e0,e1,v) for(auto [e0, e1] : (v)) #define ITRR2(e0,e1,v) for(auto &[e0, e1] : (v)) #define ITR3(e0,e1,e2,v) for(const auto [e0, e1, e2] : (v)) #define ITRP3(e0,e1,e2,v) for(auto [e0, e1, e2] : (v)) #define ITRR3(e0,e1,e2,v) for(auto &[e0, e1, e2] : (v)) #define ITR4(e0,e1,e2,e3,v) for(const auto [e0, e1, e2, e3] : (v)) #define ITRP4(e0,e1,e2,e3,v) for(auto [e0, e1, e2, e3] : (v)) #define ITRR4(e0,e1,e2,e3,v) for(auto &[e0, e1, e2, e3] : (v)) #define ITR(...) $OVERLOAD5(__VA_ARGS__, ITR4, ITR3, ITR2, ITR1)(__VA_ARGS__) #define ITRP(...) $OVERLOAD5(__VA_ARGS__, ITRP4, ITRP3, ITRP2, ITRP1)(__VA_ARGS__) #define ITRR(...) $OVERLOAD5(__VA_ARGS__, ITRR4, ITRR3, ITRR2, ITRR1)(__VA_ARGS__) /* [snippet/iterations.hpp] */ /* #expanded [data_structure/wavelet_matrix.hpp] */ #include #include #include #include #include #include #include #include #include /* #expanded [internal/dev_env.hpp] */ #ifdef LOCAL_JUDGE constexpr bool DEV_ENV = true;constexpr bool NO_EXCEPT = false; #else constexpr bool DEV_ENV = false;constexpr bool NO_EXCEPT = true; #endif /* [internal/dev_env.hpp] */ /* #expanded [internal/types.hpp] */ namespace lib {namespace internal {using size_t = std::int32_t;using int128_t = __int128_t;using uint128_t = __uint128_t;}} /* [internal/types.hpp] */ /* #expanded [internal/iterator.hpp] */ namespace lib {namespace internal {templatestruct iterator_interface {using iterator_category = std::output_iterator_tag;using difference_type = size_t;using value_type = T;using pointer = T*;using reference = T&;};templatestruct forward_iterator : iterator_interface {using iterator_category = std::forward_iterator_tag;};templatestruct bidirectional_iterator_interface : forward_iterator {using iterator_category = std::bidirectional_iterator_tag;};templatestruct random_access_iterator_base : bidirectional_iterator_interface {using iterator_category = std::random_access_iterator_tag;using difference_type = typename bidirectional_iterator_interface::difference_type;public:};templatestruct container_iterator_interface : public random_access_iterator_base {using difference_type = typename bidirectional_iterator_interface::difference_type;protected:const container *const _ref;difference_type _pos;container_iterator_interface(const container *const ref, const difference_type& pos) noexcept(NO_EXCEPT) : _ref(ref), _pos(pos) {}public:inline const container* ref() const noexcept(NO_EXCEPT) { return this->_ref; }inline difference_type pos() const noexcept(NO_EXCEPT) { return this->_pos; }inline difference_type& pos() { return this->_pos; }inline container_iterator_interface& operator++() noexcept(NO_EXCEPT) { return ++this->pos(), *this; }inline container_iterator_interface& operator--() noexcept(NO_EXCEPT) { return --this->pos(), *this; }inline container_iterator_interface& operator+=(const difference_type count) noexcept(NO_EXCEPT) { return this->pos() += count, *this; }inline container_iterator_interface& operator-=(const difference_type count) noexcept(NO_EXCEPT) { return this->pos() -= count, *this; }inline difference_type operator-(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return this->pos() - other.pos(); }inline bool operator<(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return *this - other < 0; }inline bool operator>(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return *this - other > 0; }inline bool operator<=(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return not (*this > other); }inline bool operator>=(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return not (*this < other); }inline bool operator!=(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return this->ref() != other.ref() or *this < other or *this > other; }inline bool operator==(const container_iterator_interface& other) const noexcept(NO_EXCEPT) { return not (*this != other); }};template,I>>* = nullptr>inline I operator+(I itr, const typename I::difference_type count) noexcept(NO_EXCEPT) { return itr += count, itr; }template,I>>* = nullptr>inline I operator-(I itr, const typename I::difference_type count) noexcept(NO_EXCEPT) { return itr -= count, itr; }}} /* [internal/iterator.hpp] */ /* #expanded [internal/range_reference.hpp] */ /* #expanded [constants.hpp] */ /* #expanded [snippet/aliases.hpp] */ /* #expanded [snippet/internal/types.hpp] */ namespace lib {using i16 = std::int16_t;using u16 = std::uint16_t;using i32 = std::int32_t;using u32 = std::uint32_t;using i64 = std::int64_t;using u64 = std::uint64_t; #ifdef __GNUC__ using i128 = __int128_t;using u128 = __uint128_t; #endif using uint = unsigned;using ll = long long;using ull = unsigned long long;using ld = long double;} /* [snippet/internal/types.hpp] */ #define until(...) while(!(__VA_ARGS__)) #define ALL(x) std::begin((x)),std::end((x)) #define RALL(x) std::rbegin((x)),std::rend((x)) #define $F first #define $S second namespace lib {template using spair = std::pair;}namespace std {using bit_reference = std::vector::reference;bit_reference operator |= (bit_reference a, const bool b) noexcept(NO_EXCEPT) { return a = a | b; }bit_reference operator &= (bit_reference a, const bool b) noexcept(NO_EXCEPT) { return a = a & b; }} /* [snippet/aliases.hpp] */ namespace lib {i32 INF32 = (std::numeric_limits::max() >> 1) - 1;i64 INF64 = (std::numeric_limits::max() >> 1) - 1;constexpr char LN = '\n';constexpr char SPC = ' ';constexpr std::pair DIRS4[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };constexpr std::pair DIRS8[] = { { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 }, { -1, -1 } };enum class comp : std::uint8_t {equal_to,not_equal_to,equals = equal_to,eq = equal_to,under,over,or_under,or_over,less = under,more = over,less_than = under,more_than = over,not_less_than = or_over,not_more_than = or_under,leq = or_under,geq = or_over};enum class interval : std::uint8_t {right_open,left_open,open,closed,};} /* [constants.hpp] */ namespace lib {namespace internal {template struct range_reference {using size_type = typename Super::size_type;using iterator = typename Super::iterator;protected:Super *const _super;const size_type _begin, _end;range_reference(Super *const super, const size_type begin, const size_type end) noexcept(NO_EXCEPT) : _super(super), _begin(begin), _end(end) {}public:inline iterator begin() const noexcept(NO_EXCEPT) { return std::next(_super->begin(), this->_begin); }inline iterator end() const noexcept(NO_EXCEPT) { return std::next(_super->begin(), this->_end); }inline size_type size() const noexcept(NO_EXCEPT) { return this->_end - this->_begin; }protected:inline range_reference sub_range(size_type l, size_type r) const noexcept(NO_EXCEPT) {l = _super->_positivize_index(l), r = _super->_positivize_index(r);assert(0 <= l and l <= r and r <= this->size());return range_reference(_super, this->_begin + l, this->_begin + r);}public:templateinline range_reference range(const size_type l, const size_type r) const noexcept(NO_EXCEPT) {if constexpr(rng == lib::interval::right_open) return this->sub_range(l, r);if constexpr(rng == lib::interval::left_open) return this->sub_range(l+1, r+1);if constexpr(rng == lib::interval::open) return this->sub_range(l+1, r);if constexpr(rng == lib::interval::closed) return this->sub_range(l, r+1);}inline range_reference range() const noexcept(NO_EXCEPT) { return range_reference(this->_begin, this->_end); }inline range_reference operator()(const size_type l, const size_type r) const noexcept(NO_EXCEPT) { return this->sub_range(l, r); }inline range_reference subseq(const size_type p, const size_type c) const noexcept(NO_EXCEPT) { return this->sub_range(p, p+c); }inline range_reference subseq(const size_type p) const noexcept(NO_EXCEPT) { return this->sub_range(p, this->size()); }};}} /* [internal/range_reference.hpp] */ /* #expanded [iterable/compression.hpp] */ #include namespace lib {template>struct compression : container {using size_type = internal::size_t;protected:std::vector values;public:explicit compression() noexcept(NO_EXCEPT) {}template compression(const I first, const I last) noexcept(NO_EXCEPT) {this->values.assign(first, last);std::sort(this->values.begin(), this->values.end());this->values.erase(std::unique(this->values.begin(), this->values.end()), this->values.end());this->resize(std::distance(first, last));auto itr = std::begin(*this);auto e = first;for(; e!=last; ++itr, ++e) {*itr = this->rank(*e);}}inline size_type rank(const T& val) const noexcept(NO_EXCEPT) {return static_cast(std::distance(this->values.begin(), std::lower_bound(this->values.begin(), this->values.end(), val)));}inline size_type rank2(const T& val) const noexcept(NO_EXCEPT) {return static_cast(std::distance(this->values.begin(), std::upper_bound(this->values.begin(), this->values.end(), val))) - 1;}inline T value(const size_type rank) const noexcept(NO_EXCEPT) { return this->values[rank]; }inline T operator()(const internal::size_t val) const noexcept(NO_EXCEPT) { return this->values[val]; }};} /* [iterable/compression.hpp] */ /* #expanded [numeric/bit.hpp] */ namespace lib { #define LIB_STATUC_ASSERT_UNSIGNED(T) static_assert(std::is_unsigned_v, "only unsigned type is supported") template__attribute__((target("bmi,bmi2,popcnt"))) inline constexpr int popcount(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);using u = unsigned int;using ul = unsigned long;using ull = unsigned long long;if constexpr(std::is_same_v) return __builtin_popcount(v);else if constexpr(std::is_same_v) return __builtin_popcountl(v);else if constexpr(std::is_same_v) return __builtin_popcountll(v);else return __builtin_popcountll(static_cast(v));}template__attribute__((target("bmi,bmi2,lzcnt"))) inline constexpr int countl_zero(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);using u = unsigned int;using ul = unsigned long;using ull = unsigned long long;constexpr int DIGITS = std::numeric_limits::digits;if(v == 0) return DIGITS;constexpr int DIGITS_U = std::numeric_limits::digits;constexpr int DIGITS_UL = std::numeric_limits
    ::digits;constexpr int DIGITS_ULL = std::numeric_limits::digits;if constexpr(DIGITS <= DIGITS_U) return __builtin_clz(v) - DIGITS_U + DIGITS;if constexpr(DIGITS <= DIGITS_UL) return __builtin_clzl(v) - DIGITS_UL + DIGITS;if constexpr(DIGITS <= DIGITS_ULL) return __builtin_clzll(v) - DIGITS_ULL + DIGITS;else {static_assert(DIGITS <= DIGITS_ULL << 1);constexpr ull MAX_ULL = std::numeric_limits::max();const ull high = v >> DIGITS_ULL;const ull low = v & MAX_ULL;if(high > 0) return __builtin_clzll(high) - (DIGITS_ULL << 1) + DIGITS;return __builtin_clzll(low) - DIGITS_ULL + DIGITS;}}templateinline constexpr int bit_width(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);return std::numeric_limits::digits - countl_zero(v);}templateinline constexpr int highest_bit_pos(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);return bit_width(v) - 1;}template__attribute__((target("bmi,bmi2,lzcnt")))inline constexpr int lowest_bit_pos(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);using u = unsigned int;using ul = unsigned long;using ull = unsigned long long;constexpr int DIGITS = std::numeric_limits::digits;constexpr int DIGITS_U = std::numeric_limits::digits;constexpr int DIGITS_UL = std::numeric_limits
      ::digits;constexpr int DIGITS_ULL = std::numeric_limits::digits;if constexpr(DIGITS <= DIGITS_U) return __builtin_ffs(v) - 1;if constexpr(DIGITS <= DIGITS_UL) return __builtin_ffsl(v) - 1;if constexpr(DIGITS <= DIGITS_ULL) return __builtin_ffsll(v) - 1;else {static_assert(DIGITS <= DIGITS_ULL << 1);constexpr ull MAX_ULL = std::numeric_limits::max();const ull high = v >> DIGITS_ULL;const ull low = v & MAX_ULL;if(low > 0) return __builtin_ffsll(low) - 1;return __builtin_ffsll(high) + DIGITS_ULL - 1;}}templateinline constexpr int countr_zero(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);if(v == 0) return std::numeric_limits::digits;return lowest_bit_pos(v);}templateinline constexpr T bit_ceil(const T v) noexcept(NO_EXCEPT) {LIB_STATUC_ASSERT_UNSIGNED(T);if(v <= 1U) return 1;if constexpr(std::is_same_v) return T{1} << bit_width(v - 1);else {constexpr int d = std::numeric_limits::digits - std::numeric_limits::digits;return T(1U << bit_width(v - 1) + d) >> d;}} #undef LIB_STATUC_ASSERT_UNSIGNED } /* [numeric/bit.hpp] */ /* #expanded [data_structure/bit_vector.hpp] */ #include namespace lib {struct bit_vector {using size_type = internal::size_t;private:static constexpr size_type w = 64;std::vector _block;std::vector _count;size_type _n, _zeros;public:bit_vector(const size_type _n = 0) noexcept(NO_EXCEPT) { this->init(_n); }template explicit bit_vector(const I first, const I last) noexcept(NO_EXCEPT) : bit_vector(std::distance(first, last)) {size_type pos = 0;for(auto itr=first; itr != last; ++pos, ++itr) if(*itr) this->set(pos);}template bit_vector(const std::initializer_list& init_list) noexcept(NO_EXCEPT) : bit_vector(std::begin(init_list), std::end(init_list)) {}inline size_type size() const noexcept(NO_EXCEPT) { return this->_n; }inline size_type zeros() const noexcept(NO_EXCEPT) { return this->_zeros; }inline size_type ones() const noexcept(NO_EXCEPT) { return this->_n - this->_zeros; }inline void set(const size_type k) noexcept(NO_EXCEPT) { this->_block[k / w] |= 1LL << (k % w); }inline bool get(const size_type k) const noexcept(NO_EXCEPT) { return std::uint32_t(this->_block[k / w] >> (k % w)) & 1U; }__attribute__((optimize("O3", "unroll-loops"))) void init(const int _n) noexcept(NO_EXCEPT) {this->_n = this->_zeros = _n;this->_block.resize(_n / w + 1, 0);this->_count.resize(this->_block.size(), 0);}__attribute__((target("popcnt"))) void build() noexcept(NO_EXCEPT) {for(auto k = 1UL; k < this->_block.size(); ++k) this->_count[k] = this->_count[k-1] + static_cast(_mm_popcnt_u64(this->_block[k-1]));this->_zeros = this->rank0(this->_n);}__attribute__((target("bmi2,popcnt"))) inline size_type rank1(const size_type k) const noexcept(NO_EXCEPT) {return this->_count[k / w] + static_cast(_mm_popcnt_u64(_bzhi_u64(this->_block[k / w], k % w)));}inline size_type rank0(size_type k) const noexcept(NO_EXCEPT) { return k - this->rank1(k); }inline size_type rank(const bool bit, const size_type k) const noexcept(NO_EXCEPT) {return bit ? this->rank1(k) : this->rank0(k);}__attribute__((target("bmi2,popcnt"))) inline size_type select(const bool bit, const size_type rank) const noexcept(NO_EXCEPT) {if (!bit and rank > this->zeros()) { return this->_n + 1; }if (bit and rank > this->ones()) { return this->_n + 1; }size_type block_pos = 0;{size_type ng = -1, ok = static_cast(this->_count.size());while(ok - ng > 1) {size_type mid = (ng + ok) / 2;size_type cnt = this->_count[mid];if(!bit) cnt = mid * w - cnt;(cnt >= rank ? ok : ng) = mid;}block_pos = ok;}const size_type base_index = (block_pos - 1) * w;const size_type count = this->_count[block_pos - 1];const std::uint64_t block = this->_block[block_pos - 1];size_type ng = -1, ok = w;while(ok - ng > 1) {const size_type mid = (ok + ng) / 2;size_type r = count + static_cast(_mm_popcnt_u64(_bzhi_u64(block, mid)));if(!bit) r = base_index + mid - r;(r >= rank ? ok : ng) = mid;}return base_index + ok;}inline size_type select0(const size_type k) const noexcept(NO_EXCEPT) { return this->select(0, k); }inline size_type select1(const size_type k) const noexcept(NO_EXCEPT) { return this->select(1, k); }struct iterator : virtual internal::container_iterator_interface {iterator(const bit_vector *const ref, const size_type pos) noexcept(NO_EXCEPT) : internal::container_iterator_interface(ref, pos) {}inline bool operator*() const noexcept(NO_EXCEPT) { return this->ref()->get(this->pos()); }};inline iterator begin() const noexcept(NO_EXCEPT) { return iterator(this, 0); }inline iterator end() const noexcept(NO_EXCEPT) { return iterator(this, this->size()); }};} /* [data_structure/bit_vector.hpp] */ namespace lib {namespace internal {namespace wavelet_matrix_impl {template struct base {using size_type = internal::size_t;private:size_type _n, _bits;std::vector _index;std::vector> _sum;dict_type _first_pos;T _max = 0;public:base() {}template base(const I first, const I last) noexcept(NO_EXCEPT) { this->build(first, last); }template base(const std::initializer_list& init_list) noexcept(NO_EXCEPT) : base(ALL(init_list)) {}inline size_type size() const noexcept(NO_EXCEPT) { return this->_n; }inline size_type bits() const noexcept(NO_EXCEPT) { return this->_bits; }template __attribute__((optimize("O3"))) void build(const I first, const I last) noexcept(NO_EXCEPT) {this->_n = static_cast(std::distance(first, last));this->_max = first == last ? -1 : *std::max_element(first, last);this->_bits = bit_width>(this->_max + 1);this->_index.assign(this->_bits, this->_n);std::vector bit(first, last), nxt(this->_n);this->_sum.assign(this->_bits+1, std::vector(this->_n+1));{size_type i = 0;for(auto itr=first; itr!=last; ++i, ++itr) {this->_sum[this->_bits][i+1] = this->_sum[this->_bits][i] + *itr;}}REPD(h, this->_bits) {std::vector vals;for(size_type i = 0; i < this->_n; ++i) {if((bit[i] >> h) & 1) this->_index[h].set(i);}this->_index[h].build();std::array::iterator,2> itrs{ std::begin(nxt), std::next(std::begin(nxt), this->_index[h].zeros()) };for(size_type i=0; i_n; ++i) *itrs[this->_index[h].get(i)]++ = bit[i];REP(i, this->_n) this->_sum[h][i+1] = this->_sum[h][i] + nxt[i];std::swap(bit, nxt);}REPD(i, this->_n) this->_first_pos[bit[i]] = i;}protected:inline T get(const size_type k) const noexcept(NO_EXCEPT) { return this->_sum[this->_bits][k+1] - this->_sum[this->_bits][k]; }size_type select(const T& v, const size_type rank) const noexcept(NO_EXCEPT) {if (v > this->_max) return this->_n;if (this->_first_pos.count(v) == 0) return this->_n;size_type pos = this->_first_pos.at(v) + rank + 1;REP(h, this->_bits) {const bool bit = (v >> h) & 1;if(bit) pos -= this->_index[h].zeros();pos = this->_index[h].select(bit, pos);}return pos - 1;}inline T kth_smallest(size_type l, size_type r, size_type k) const noexcept(NO_EXCEPT) {T val = 0;for(size_type h = this->_bits - 1; h >= 0; --h) {size_type l0 = this->_index[h].rank0(l), r0 = this->_index[h].rank0(r);if(k < r0 - l0) {l = l0, r = r0;}else {k -= r0 - l0;val |= T{1} << h;l += this->_index[h].zeros() - l0;r += this->_index[h].zeros() - r0;}}return val;}inline size_type kth_smallest_index(size_type l, size_type r, size_type k) const noexcept(NO_EXCEPT) {T val = 0;for(size_type h = this->_bits - 1; h >= 0; --h) {size_type l0 = this->_index[h].rank0(l), r0 = this->_index[h].rank0(r);if(k < r0 - l0) {l = l0, r = r0;}else {k -= r0 - l0;val |= T{1} << h;l += this->_index[h].zeros() - l0;r += this->_index[h].zeros() - r0;}}size_type left = 0;REPD(h, this->_bits) {const bool bit = (val >> h) & 1;left = this->_index[h].rank(bit, left);if(bit) left += this->_index[h].zeros();}return this->select(val, l + k - left);}inline T kth_largest(const size_type l, const size_type r, const size_type k) const noexcept(NO_EXCEPT) {return this->kth_smallest(l, r, r-l-k-1);}inline size_type kth_largest_index(const size_type l, const size_type r, const size_type k) const noexcept(NO_EXCEPT) {return this->kth_smallest_index(l, r, r-l-k-1);}inline std::pair succ0(const size_type l, const size_type r, const size_type h) const noexcept(NO_EXCEPT) {return std::make_pair(this->_index[h].rank0(l), this->_index[h].rank0(r));}inline std::pair succ1(const size_type l, const size_type r, const size_type h) const noexcept(NO_EXCEPT) {size_type l0 = this->_index[h].rank0(l);size_type r0 = this->_index[h].rank0(r);size_type vals = this->_index[h].zeros();return std::make_pair(l + vals - l0, r + vals - r0);}T sum_in_range(const size_type l, const size_type r, const T& x, const T& y, const T& cur, const size_type bit) const noexcept(NO_EXCEPT) {if(l == r) return 0;if(bit == -1) {if(x <= cur and cur < y) return cur * (r - l);return 0;}const T& nxt = (T{1} << bit) | cur;const T& ones = ((T{1} << bit) - 1) | nxt;if(ones < x or y <= cur) return 0;if(x <= cur and ones < y) return this->_sum[bit+1][r] - this->_sum[bit+1][l];const size_type l0 = this->_index[bit].rank0(l), r0 = this->_index[bit].rank0(r);const size_type l1 = l - l0, r1 = r - r0;return this->sum_in_range(l0, r0, x, y, cur, bit - 1) + this->sum_in_range(this->_index[bit].zeros()+l1, this->_index[bit].zeros()+r1, x, y, nxt, bit - 1);}inline T sum_in_range(const size_type l, const size_type r, const T& x, const T& y) const noexcept(NO_EXCEPT) {return this->sum_in_range(l, r, x, y, 0, this->_bits-1);}inline T sum_under(const size_type l, const size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->sum_in_range(l, r, 0, v);}inline T sum_over(const size_type l, const size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->sum_in_range(l, r, v+1, std::numeric_limits::max());}inline T sum_or_under(const size_type l, const size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->sum_in_range(l, r, 0, v+1);}inline T sum_or_over(const size_type l, const size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->sum_in_range(l, r, v, std::numeric_limits::max());}inline T sum(const size_type l, const size_type r) const noexcept(NO_EXCEPT) {return this->_sum[this->_bits][r] - this->_sum[this->_bits][l];}inline size_type count_under(size_type l, size_type r, const T& y) const noexcept(NO_EXCEPT) {if(y >= (T{1} << this->_bits)) return r - l;size_type res = 0;REPD(h, this->_bits) {bool f = (y >> h) & 1;size_type l0 = this->_index[h].rank0(l), r0 = this->_index[h].rank0(r);if(f) {res += r0 - l0;l += this->_index[h].zeros() - l0;r += this->_index[h].zeros() - r0;} else {l = l0;r = r0;}}return res;}inline size_type count_in_range(const size_type l, const size_type r, const T& x, const T& y) const noexcept(NO_EXCEPT) {return this->count_under(l, r, y) - this->count_under(l, r, x);}inline size_type count_or_under(size_type l, size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->count_under(l, r, v+1);}inline size_type count_over(size_type l, size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->count_or_over(l, r, v+1);}inline size_type count_or_over(size_type l, size_type r, const T& v) const noexcept(NO_EXCEPT) {return r - l - this->count_under(l, r, v);}inline size_type count_equal_to(const size_type l, const size_type r, const T& v) const noexcept(NO_EXCEPT) {return this->count_under(l, r, v+1) - this->count_under(l, r, v);}inline std::optional next(const size_type l, const size_type r, const T& v, const size_type k) const noexcept(NO_EXCEPT) {const size_type rank = this->count_under(l, r, v) + k;return (rank < 0 or rank >= r - l) ? std::optional{} : std::optional(this->kth_smallest(l, r, rank));}inline std::optional prev(const size_type l, const size_type r, const T& v, const size_type k) const noexcept(NO_EXCEPT) {const size_type rank = this->count_over(l, r, v) + k;return (rank < 0 or rank >= r - l) ? std::optional{} : std::optional(this->kth_largest(l, r, rank));}};}}template> struct compressed_wavelet_matrix;template>struct wavelet_matrix : internal::wavelet_matrix_impl::base {using compressed = compressed_wavelet_matrix;private:using base = typename internal::wavelet_matrix_impl::base;public:using value_type = T;using size_type = typename base::size_type;protected:inline size_type _positivize_index(const size_type p) const noexcept(NO_EXCEPT) {return p < 0 ? this->size() + p : p;}public:using base::base;bool empty() const noexcept(NO_EXCEPT) { return this->size() == 0; }inline T get(size_type p) const noexcept(NO_EXCEPT) {p = this->_positivize_index(p), assert(0 <= p && p < this->size());return this->base::get(p);}inline T operator[](const size_type p) const noexcept(NO_EXCEPT) { return this->get(p); }inline size_type select(const T& v, const size_type p) const noexcept(NO_EXCEPT) { return this->base::select(v, p); }struct iterator;struct range_reference;templateinline range_reference range(const size_type l, const size_type r) const noexcept(NO_EXCEPT) {if constexpr(rng == lib::interval::right_open) return range_reference(this, l, r);if constexpr(rng == lib::interval::left_open) return range_reference(this, l+1, r+1);if constexpr(rng == lib::interval::open) return range_reference(this, l+1, r);if constexpr(rng == lib::interval::closed) return range_reference(this, l, r+1);}inline range_reference range() const noexcept(NO_EXCEPT) { return range_reference(this, 0, this->size()); }inline range_reference operator()(const size_type l, const size_type r) const noexcept(NO_EXCEPT) { return range_reference(this, l, r); }inline range_reference subseq(const size_type p, const size_type c) const noexcept(NO_EXCEPT) { return range_reference(this, p, p+c); }inline range_reference subseq(const size_type p) const noexcept(NO_EXCEPT) { return range_reference(this, p, this->size()); }struct range_reference : internal::range_reference {range_reference(const wavelet_matrix *const super, const size_type l, const size_type r) noexcept(NO_EXCEPT): internal::range_reference(super, super->_positivize_index(l), super->_positivize_index(r)){assert(0 <= this->_begin && this->_begin <= this->_end && this->_end <= this->_super->size());}inline T get(const size_type k) const noexcept(NO_EXCEPT) {k = this->_super->_positivize_index(k);assert(0 <= k and k < this->size());return this->_super->get(this->_begin + k);}inline T operator[](const size_type k) const noexcept(NO_EXCEPT) { return this->get(k); }inline T kth_smallest(const size_type k) const noexcept(NO_EXCEPT) {assert(0 <= k && k < this->size());return this->_super->base::kth_smallest(this->_begin, this->_end, k);}inline auto kth_smallest_element(const size_type k) const noexcept(NO_EXCEPT) {if(k == this->size()) return this->end();assert(0 <= k && k < this->size());return std::next(this->_super->begin(), this->_super->base::kth_smallest_index(this->_begin, this->_end, k));}inline T kth_largest(const size_type k) const noexcept(NO_EXCEPT) {assert(0 <= k && k < this->size());return this->_super->base::kth_largest(this->_begin, this->_end, k);}inline auto kth_largest_element(const size_type k) const noexcept(NO_EXCEPT) {if(k == this->size()) return this->end();assert(0 <= k && k < this->size());return std::next(this->_super->begin(), this->_super->base::kth_largest_index(this->_begin, this->_end, k));}inline T min() const noexcept(NO_EXCEPT) { return this->kth_smallest(0); }inline T max() const noexcept(NO_EXCEPT) { return this->kth_largest(0); }inline T median() const noexcept(NO_EXCEPT) { return this->kth_smallest(this->size() / 2); }inline T sum_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->_super->base::sum_in_range(this->_begin, this->_end, x, y); }inline T sum_under(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::sum_under(this->_begin, this->_end, v); }inline T sum_over(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::sum_over(this->_begin, this->_end, v); }inline T sum_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::sum_or_under(this->_begin, this->_end, v); }inline T sum_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::sum_or_over(this->_begin, this->_end, v); }inline T sum(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->_super->base::sum_in_range(this->_begin, this->_end, x, y); }inline T sum() const noexcept(NO_EXCEPT) { return this->_super->base::sum(this->_begin, this->_end); }templateinline size_type sum(const T& v) const noexcept(NO_EXCEPT) {if constexpr(com == comp::under) return this->sum_under(v);if constexpr(com == comp::over) return this->sum_over(v);if constexpr(com == comp::or_under) return this->sum_or_under(v);if constexpr(com == comp::or_over) return this->sum_or_over(v);assert(false);}inline size_type count_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->_super->base::count_in_range(this->_begin, this->_end, x, y); }inline size_type count_under(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::count_under(this->_begin, this->_end, v); }inline size_type count_over(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::count_over(this->_begin, this->_end, v); }inline size_type count_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::count_or_under(this->_begin, this->_end, v); }inline size_type count_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->_super->base::count_or_over(this->_begin, this->_end, v); }templateinline size_type count(const T& v) const noexcept(NO_EXCEPT) {if constexpr(com == comp::eq) return this->_super->count_equal_to(this->_begin, this->_end, v);if constexpr(com == comp::under) return this->count_under(v);if constexpr(com == comp::over) return this->count_over(v);if constexpr(com == comp::or_under) return this->count_or_under(v);if constexpr(com == comp::or_over) return this->count_or_over(v);assert(false);}inline auto next_element(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {return this->kth_smallest_element(std::clamp(this->count_under(v) + k, 0, this->size()));}inline auto prev_element(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {return this->kth_smallest_element(std::clamp(this->count_over(v) - k, 0, this->size()));}inline std::optional next(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->_super->base::next(this->_begin, this->_end, v, k); }inline std::optional prev(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->_super->base::prev(this->_begin, this->_end, v, k); }};inline T kth_smallest(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_smallest(k); }inline T kth_smallest_element(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_smallest_element(k); }inline T kth_largest(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_largest(k); }inline T kth_largest_element(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_largest_element(k); }inline T min() const noexcept(NO_EXCEPT) { return this->range().kth_smallest(0); }inline T max() const noexcept(NO_EXCEPT) { return this->range().kth_largest(0); }inline T median() const noexcept(NO_EXCEPT) { return this->range().median(); }inline T sum_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->range().sum_in_range(x, y); }inline T sum_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().sum_under(v); }inline T sum_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().sum_over(v); }inline T sum_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().sum_or_under(v); }inline T sum_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().sum_or_over(v); }inline T sum(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->range().sum_in_range(x, y); }inline T sum() const noexcept(NO_EXCEPT) { return this->range().sum(); }templateinline size_type sum(const T& v) const noexcept(NO_EXCEPT) { return this->range().template sum(v); }inline size_type count_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->range().count_in_range(x, y); }inline size_type count_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_under(v); }inline size_type count_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_over(v); }inline size_type count_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_or_under(v); }inline size_type count_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_or_over(v); }inline size_type count(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->range().count(x, y); }templateinline size_type count(const T& v) const noexcept(NO_EXCEPT) { return this->range().template count(v); }inline auto next_element(const T& v) const noexcept(NO_EXCEPT) { return this->range().next_element(v); }inline auto prev_element(const T& v) const noexcept(NO_EXCEPT) { return this->range().prev_element(v); }inline std::optional next(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->range().next(v, k); }inline std::optional prev(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->range().prev(v, k); }protected:using iterator_interface = internal::container_iterator_interface;public:struct iterator : virtual iterator_interface {iterator(const wavelet_matrix *const ref, const size_type pos) noexcept(NO_EXCEPT) : iterator_interface(ref, pos) {}inline T operator*() const noexcept(NO_EXCEPT) { return this->ref()->get(this->pos()); }inline T operator[](const typename iterator_interface::difference_type count) const noexcept(NO_EXCEPT) { return *(*this + count); }};inline iterator begin() const noexcept(NO_EXCEPT) { return iterator(this, 0); }inline iterator end() const noexcept(NO_EXCEPT) { return iterator(this, this->size()); }};templatestruct compressed_wavelet_matrix : protected wavelet_matrix::size_type,dict_type> {protected:using core = wavelet_matrix::size_type,dict_type>;compression compressed;public:using value_type = typename core::value_type;using size_type = typename core::size_type;template compressed_wavelet_matrix(const I first, const I last) noexcept(NO_EXCEPT) { this->build(first, last); }template void build(const I first, const I last) noexcept(NO_EXCEPT) {this->compressed = compression(first, last);this->core::build(ALL(this->compressed));}inline T get(const size_type k) const noexcept(NO_EXCEPT) { return this->compressed(this->core::get(k)); }inline size_type operator[](const size_type k) const noexcept(NO_EXCEPT) { return this->core::get(k); }struct iterator;struct range_reference;templateinline range_reference range(const size_type l, const size_type r) const noexcept(NO_EXCEPT) {if constexpr(rng == lib::interval::right_open) return range_reference(this, l, r);if constexpr(rng == lib::interval::left_open) return range_reference(this, l+1, r+1);if constexpr(rng == lib::interval::open) return range_reference(this, l+1, r);if constexpr(rng == lib::interval::closed) return range_reference(this, l, r+1);}inline range_reference range() const noexcept(NO_EXCEPT) { return range_reference(this, 0, this->size()); }inline range_reference operator()(const size_type l, const size_type r) const noexcept(NO_EXCEPT) { return range_reference(this, l, r); }inline range_reference subseq(const size_type p, const size_type c) const noexcept(NO_EXCEPT) { return range_reference(this, p, p+c); }inline range_reference subseq(const size_type p) const noexcept(NO_EXCEPT) { return range_reference(this, p, this->size()); }struct range_reference : internal::range_reference {range_reference(const compressed_wavelet_matrix *const super, const size_type l, const size_type r) noexcept(NO_EXCEPT): internal::range_reference(super, super->_positivize_index(l), super->_positivize_index(r)){assert(0 <= this->_begin && this->_begin <= this->_end && this->_end <= this->_super->size());}private:inline auto _range() const noexcept(NO_EXCEPT) { return this->_super->core::range(this->_begin, this->_end); }public:inline T get(const size_type k) const noexcept(NO_EXCEPT) { return this->_super->compressed(this->_range().get(k)); }inline T operator[](const size_type k) const noexcept(NO_EXCEPT) { return this->get(k); }inline T kth_smallest(const size_type k) const noexcept(NO_EXCEPT) { return this->_super->compressed(this->_range().kth_smallest(k)); }inline auto kth_smallest_element(const size_type k) const noexcept(NO_EXCEPT) {return std::next(this->_super->begin(), std::distance(this->_super->core::begin(), this->_range().kth_smallest_element(k)));}inline T kth_largest(const size_type k) const noexcept(NO_EXCEPT) { return this->_super->compressed(this->_range().kth_largest(k));}inline auto kth_largest_element(const size_type k) const noexcept(NO_EXCEPT) {return std::next(this->_super->begin(), std::distance(this->_super->core::begin(), this->_range().kth_largest_element(k)));}inline T min() const noexcept(NO_EXCEPT) { return this->kth_smallest(0); }inline T max() const noexcept(NO_EXCEPT) { return this->kth_largest(0); }inline T median() const noexcept(NO_EXCEPT) { return this->kth_smallest(this->size() / 2); }inline size_type count_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) {return this->_range().count_in_range(this->_super->compressed.rank(x), this->_super->compressed.rank(y));}inline size_type count_under(const T& v) const noexcept(NO_EXCEPT) { return this->_range().count_under(this->_super->compressed.rank(v)); }inline size_type count_over(const T& v) const noexcept(NO_EXCEPT) { return this->_range().count_over(this->_super->compressed.rank2(v)); }inline size_type count_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->_range().count_or_under(this->_super->compressed.rank2(v)); }inline size_type count_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->_range().count_or_over(this->_super->compressed.rank(v)); }templateinline size_type count(const T& v) const noexcept(NO_EXCEPT) { return this->_range().template count(this->_super->compressed.rank(v)); }inline auto next_element(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {return this->kth_smallest_element(std::clamp(this->_range().count_under(this->_super->compressed.rank(v) + k), 0, this->size()));}inline auto prev_element(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {return this->kth_largest_element(std::clamp(this->_range().count_over(this->_super->compressed.rank2(v) + k), 0, this->size()));}inline std::optional next(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {const std::optional res = this->_range().next(this->_super->compressed.rank(v), k);if(res.has_value()) return this->_super->compressed(res.value());return {};}inline std::optional prev(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) {const std::optional res = this->_range().prev(this->_super->compressed.rank2(v), k);if(res.has_value()) return this->_super->compressed(res.value());return {};}};inline T kth_smallest(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_smallest(k); }inline auto kth_smallest_element(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_smallest_element(k); }inline T kth_largest(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_largest(k); }inline auto kth_largest_element(const size_type k) const noexcept(NO_EXCEPT) { return this->range().kth_largest_element(k); }inline T min() const noexcept(NO_EXCEPT) { return this->range().kth_smallest(0); }inline T max() const noexcept(NO_EXCEPT) { return this->range().kth_largest(0); }inline T median() const noexcept(NO_EXCEPT) { return this->range().median(); }inline size_type count_in_range(const T& x, const T& y) const noexcept(NO_EXCEPT) { return this->range().count_in_range(x, y); }inline size_type count_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_under(v); }inline size_type count_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_over(v); }inline size_type count_or_under(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_or_under(v); }inline size_type count_or_over(const T& v) const noexcept(NO_EXCEPT) { return this->range().count_or_over(v); }template inline size_type count(const T& v) const noexcept(NO_EXCEPT) { return this->range().template count(v); }inline auto next_element(const T& v) const noexcept(NO_EXCEPT) { return this->range().next_element(v); }inline auto prev_element(const T& v) const noexcept(NO_EXCEPT) { return this->range().prev_element(v); }inline std::optional next(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->range().next(v, k); }inline std::optional prev(const T& v, const size_type k = 0) const noexcept(NO_EXCEPT) { return this->range().prev(v, k); }protected:using iterator_interface = internal::container_iterator_interface;public:struct iterator : virtual iterator_interface {iterator(const compressed_wavelet_matrix *const ref, const size_type pos) noexcept(NO_EXCEPT) : iterator_interface(ref, pos) {}inline T operator*() const noexcept(NO_EXCEPT) { return this->ref()->get(this->pos()); }inline T operator[](const typename iterator_interface::difference_type count) const noexcept(NO_EXCEPT) { return *(*this + count); }};inline iterator begin() const noexcept(NO_EXCEPT) { return iterator(this, 0); }inline iterator end() const noexcept(NO_EXCEPT) { return iterator(this, this->size()); }};} /* [data_structure/wavelet_matrix.hpp] */ signed main() { int n, k; std::cin >> n >> k; std::vector a(n); ITRR(v, a) std::cin >> v; lib::wavelet_matrix data(ALL(a)); long ans = lib::INF64; FOR(i, 0, n-k) { auto range = data.subseq(i, k); // debug(range); long med = range.median(); long costl = med * range.count_under(med) - range.sum_under(med); long costr = range.sum_or_over(med) - med * range.count_or_over(med); lib::chmin(ans, costl + costr); } std::cout << ans << "\n"; }