#define SKIP_RUNTIME_CHECKS // Verify: https://yukicoder.me/problems/no/2992 // Library: DataStructure/Range_Nondecreasing_Subsequence.cpp // SPDX-License-Identifier: CC0-1.0 // 参考(問い合わせの用途): https://yukicoder.me/problems/no/2992 // ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/range_nondecreasing_subsequence.md // // API一覧 // RangeNondecreasingSubsequence(symbols): 値が0以上A未満の列を保持。1<=A<=256、n<=INT_MAX。 // 構築はO(A^3*(n+1))時間、O(A^2*(n+1))領域。空構築可。区間は0始まりの半開。 // size()/empty(): 長さ/空か。O(1)。set(i,x): 位置iへxを代入。 // longest(l,r): 最長広義単調増加部分列の長さ。longest(l,r,low,high): 値も閉区間[low,high]に限定。 // replacement_count(l,r): 広義単調増加列にする最少置換回数。上の更新・照会はO(A^3*log(n+1))。 // all(): 列全体の最長長さ。O(A^2)。空区間は長さ0。コピーは独立、ムーブ元は空。 // // [同梱のSegment Tree] // [segtree(ACL形式、sirkit内に定義)] // segtree()/segtree(int n)/segtree(vector): 空列/n個の単位元/列から構築。0<=n<=INT_MAX、O(n+1)時間・領域。 // S op(S,S)は結合的、S e()は左右の単位元。同じ呼び出し形式の関数オブジェクトも可。演算/コピーはO(1)。 // 点添字は0<=p(int l)/min_left(int r): bool pred(S)を使う境界探索。述語を第2引数に渡す形式も可。戻り値はint、O(log(n+1))。 // 探索はpred(e())==trueで、区間を延ばすと真から偽へ一度だけ変化すること。述語に副作用を持たせない。 // [追加機能] // size()/empty(): intの要素数/空か、O(1)。transform(int p,f): 点をf(現在値)へ更新、O(log(n+1))。 // values(): 列のコピー、O(n+1)。assign(vector)/reset(int n)/reset(): 列/n個/現在の長さの単位元へ再構築、O(旧n+n+1)。 // clear()/swap(other): 容量を保持して空へ/交換、O(旧n+1)/O(1)。コピーは独立、移動元は空。値の演算等が投げる場合は内容の強い保証なし。 // SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。 #include #include #include #include #include #include #include #include #include #include #include // SIRKIT_NAMESPACE_OPEN_V1 namespace sirkit { #ifndef SIRKIT_HLD_SEGMENT_ACCESS_FORWARD_DEFINED #define SIRKIT_HLD_SEGMENT_ACCESS_FORWARD_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { template struct HldSegmentAccess; } #endif #ifndef SIRKIT_SEGMENT_ARRAY_CAPACITY_DEFINED #define SIRKIT_SEGMENT_ARRAY_CAPACITY_DEFINED namespace detail { inline std::size_t CheckedSegmentLeafBase(std::size_t count, std::size_t limit, const char* error) { if (count == 0) return 1; if (count > std::bit_floor(limit)) throw std::length_error(error); return std::bit_ceil(count); } inline int CheckedSegmentArrayCount(std::size_t count, const char* error) { if (count > static_cast(std::numeric_limits::max())) throw std::length_error(error); return static_cast(count); } } #endif #ifndef SIRKIT_SEGMENT_TREE_DEFINED #define SIRKIT_SEGMENT_TREE_DEFINED template class segtree { static_assert(std::is_invocable_r_v, "segtree: op(S,S) must return S"); static_assert(std::is_invocable_r_v, "segtree: e() must return S"); using HldSegmentTag = void; template friend struct /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::HldSegmentAccess; S identity_; std::size_t n_ = 0, base_ = 1; std::vector data_; static std::size_t leaf_base(std::size_t n) { return detail::CheckedSegmentLeafBase(n, std::vector{}.max_size() / 2, "segtree: size"); } static std::size_t checked_size(int n) { #ifndef SKIP_RUNTIME_CHECKS if (n < 0) throw std::invalid_argument("segtree: negative size"); #endif return static_cast(n); } static int vector_size(const std::vector& values) { return detail::CheckedSegmentArrayCount(values.size(), "segtree: int size overflow"); } void check_index(int i) const { #ifndef SKIP_RUNTIME_CHECKS if (i < 0 || static_cast(i) >= n_) throw std::out_of_range("segtree: index"); #else (void)i; #endif } void check_range(int l, int r) const { #ifndef SKIP_RUNTIME_CHECKS if (l < 0 || l > r || static_cast(r) > n_) throw std::out_of_range("segtree: range"); #else (void)l; (void)r; #endif } static unsigned aligned_node(unsigned value) { if (value == 0) std::unreachable(); return value >> std::countr_zero(value); } void rebuild() { for (auto p = base_; p > 1;) { --p; data_[p] = std::invoke(op, data_[2 * p], data_[2 * p + 1]); } } void update_carried_ancestors(std::size_t p) { if (p <= 1) return; S value = std::as_const(data_)[p]; while (p > 1) { const S sibling = std::as_const(data_)[p ^ 1]; const S left = (p & 1) ? sibling : value; const S right = (p & 1) ? value : sibling; value = std::invoke(op, left, right); p >>= 1; data_[p] = std::as_const(value); } } void update_ancestors(std::size_t p) { if constexpr (std::is_trivially_copyable_v && std::is_copy_constructible_v && std::is_copy_assignable_v && sizeof(S) <= 16 && std::is_invocable_r_v) { #if defined(__clang__) if constexpr (sizeof(S) > 8) { // 照会を含む呼び出し側のコードが膨らむのを抑える。 [[clang::noinline]] update_carried_ancestors(p); return; } #endif update_carried_ancestors(p); } else { for (p >>= 1; p; p >>= 1) data_[p] = std::invoke(op, data_[2 * p], data_[2 * p + 1]); } } public: segtree() : segtree(0) {} explicit segtree(int n) : identity_(std::invoke(e)), n_(checked_size(n)), base_(leaf_base(n_)), data_(n_ ? 2 * base_ : 0, identity_) {} explicit segtree(const std::vector& values) : segtree(vector_size(values)) { if (!n_) return; std::copy(values.begin(), values.end(), data_.begin() + base_); rebuild(); } segtree(const segtree&) = default; segtree& operator=(const segtree& other) { if (this != &other) { segtree replacement(other); swap(replacement); } return *this; } segtree(segtree&& other) : identity_(other.identity_), n_(other.n_), base_(other.base_), data_(std::move(other.data_)) { other.n_ = 0; other.base_ = 1; other.data_.clear(); } segtree& operator=(segtree&& other) { if (this != &other) { segtree replacement(std::move(other)); swap(replacement); } return *this; } int size() const noexcept { return static_cast(n_); } bool empty() const noexcept { return n_ == 0; } S get(int i) const { check_index(i); return data_[base_ + static_cast(i)]; } void set(int i, S value) { check_index(i); const auto p = base_ + static_cast(i); data_[p] = std::move(value); update_ancestors(p); } template void transform(int i, F&& f) { check_index(i); const auto p = base_ + static_cast(i); data_[p] = std::invoke(std::forward(f), std::as_const(data_)[p]); update_ancestors(p); } S prod(int l, int r) const { check_range(l, r); if (l == r) return identity_; // n<=INT_MAXなので葉番号はunsignedに収まり、両端は常に正。 unsigned first = static_cast(base_) + static_cast(l); unsigned last = static_cast(base_) + static_cast(r); auto step = first & (~first + 1); if (step > last - first) { // firstの整列幅より残りが短ければ、右端の下位bitを落としてfirstへ戻れる。 S result = data_[aligned_node(last) - 1]; last -= last & (~last + 1); while (first < last) { result = std::invoke(op, data_[aligned_node(last) - 1], result); last -= last & (~last + 1); } return result; } // 左から最大の整列ブロックを選ぶ。最初は節点をコピーし、単位元との積を省く。 S left = data_[aligned_node(first)]; first += step; if (first == last) return left; // 短い残区間では分割点の計算を省く。 if (last - first <= 32) { while (first < last && (step = first & (~first + 1)) <= last - first) { left = std::invoke(op, left, data_[aligned_node(first)]); first += step; } if (first == last) return left; S right = data_[aligned_node(last) - 1]; last -= last & (~last + 1); while (first < last) { right = std::invoke(op, data_[aligned_node(last) - 1], right); last -= last & (~last + 1); } return std::invoke(op, left, right); } // 左走査が止まる整列境界を先に求め、独立な左右の積を交互に進める。 const unsigned stop = last & (0U - std::bit_floor((first - 1) ^ (last - 1))); if (stop == last) { while (first < stop) { left = std::invoke(op, left, data_[aligned_node(first)]); first += first & (~first + 1); } return left; } S right = data_[aligned_node(last) - 1]; last -= last & (~last + 1); while (first < stop && last > stop) { left = std::invoke(op, left, data_[aligned_node(first)]); first += first & (~first + 1); right = std::invoke(op, data_[aligned_node(last) - 1], right); last -= last & (~last + 1); } while (first < stop) { left = std::invoke(op, left, data_[aligned_node(first)]); first += first & (~first + 1); } while (last > stop) { right = std::invoke(op, data_[aligned_node(last) - 1], right); last -= last & (~last + 1); } return std::invoke(op, left, right); } S all_prod() const { return n_ ? data_[1] : identity_; } template int max_right(int l) const { return max_right(l, Pred); } template int max_right(int l, Pred pred) const { check_range(l, l); #ifndef SKIP_RUNTIME_CHECKS if (!std::invoke(pred, identity_)) throw std::invalid_argument("segtree: predicate(e())"); #endif if (static_cast(l) == n_) return size(); S accumulated = identity_; auto node = base_ + static_cast(l); while (true) { while (!(node & 1)) node >>= 1; S candidate = std::invoke(op, accumulated, data_[node]); if (!std::invoke(pred, candidate)) { while (node < base_) { node *= 2; S next = std::invoke(op, accumulated, data_[node]); if (std::invoke(pred, next)) { accumulated = std::move(next); ++node; } } return static_cast(std::min(n_, node - base_)); } accumulated = std::move(candidate); ++node; if ((node & (node - 1)) == 0) return size(); } } template int min_left(int r) const { return min_left(r, Pred); } template int min_left(int r, Pred pred) const { check_range(r, r); #ifndef SKIP_RUNTIME_CHECKS if (!std::invoke(pred, identity_)) throw std::invalid_argument("segtree: predicate(e())"); #endif if (r == 0) return 0; S accumulated = identity_; auto node = base_ + static_cast(r); while (true) { --node; while (node > 1 && (node & 1)) node >>= 1; S candidate = std::invoke(op, data_[node], accumulated); if (!std::invoke(pred, candidate)) { while (node < base_) { node = 2 * node + 1; S next = std::invoke(op, data_[node], accumulated); if (std::invoke(pred, next)) { accumulated = std::move(next); --node; } } return static_cast(node + 1 - base_); } accumulated = std::move(candidate); if ((node & (node - 1)) == 0) return 0; } } std::vector values() const { if (!n_) return {}; return std::vector(data_.begin() + base_, data_.begin() + base_ + n_); } void assign(const std::vector& values) { segtree replacement(values); swap(replacement); } void reset(int n) { segtree replacement(n); swap(replacement); } void reset() { std::fill(data_.begin(), data_.end(), identity_); } void clear() noexcept { data_.clear(); n_ = 0; base_ = 1; } void swap(segtree& other) noexcept(std::is_nothrow_swappable_v) { using std::swap; swap(identity_, other.identity_); swap(n_, other.n_); swap(base_, other.base_); data_.swap(other.data_); } }; #endif #ifndef SIRKIT_RANGE_NONDECREASING_SUBSEQUENCE_DEFINED #define SIRKIT_RANGE_NONDECREASING_SUBSEQUENCE_DEFINED namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/ { template struct MonotoneSubsequenceSummary { std::array lengths{}; static constexpr std::size_t index(std::size_t low, std::size_t high) noexcept { return high * (high + 1) / 2 + low; } [[nodiscard]] constexpr int get(std::size_t low, std::size_t high) const noexcept { return lengths[index(low, high)]; } }; template constexpr MonotoneSubsequenceSummary EmptyMonotoneSubsequence() noexcept { return {}; } template constexpr MonotoneSubsequenceSummary SingleMonotoneSubsequence(std::size_t symbol) noexcept { MonotoneSubsequenceSummary result{}; for (std::size_t high = symbol; high < Alphabet; ++high) { for (std::size_t low = 0; low <= symbol; ++low) { result.lengths[result.index(low, high)] = 1; } } return result; } template constexpr MonotoneSubsequenceSummary JoinMonotoneSubsequences( const MonotoneSubsequenceSummary &left, const MonotoneSubsequenceSummary &right) noexcept { MonotoneSubsequenceSummary result{}; for (std::size_t high = 0; high < Alphabet; ++high) { for (std::size_t low = 0; low <= high; ++low) { // 境界値cutを共有し、前半はcut以下、後半はcut以上の値を選ぶ。 auto best = left.get(low, low) + right.get(low, high); for (std::size_t cut = low + 1; cut <= high; ++cut) { best = std::max(best, left.get(low, cut) + right.get(cut, high)); } result.lengths[result.index(low, high)] = best; } } return result; } } // namespace reinsirk::detail template requires (1 <= Alphabet && Alphabet <= 256) class RangeNondecreasingSubsequence { using Summary = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::MonotoneSubsequenceSummary; using Tree = segtree, /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::EmptyMonotoneSubsequence>; Tree tree_; static void check_symbol([[maybe_unused]] int symbol) { #ifndef SKIP_RUNTIME_CHECKS if (symbol < 0 || symbol >= static_cast(Alphabet)) { throw std::out_of_range("RangeNondecreasingSubsequence: symbol"); } #endif } static Summary leaf(int symbol) { check_symbol(symbol); return /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::SingleMonotoneSubsequence(static_cast(symbol)); } static std::vector build(std::span symbols) { static_cast(/*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::CheckedSegmentArrayCount(symbols.size(), "RangeNondecreasingSubsequence: size")); std::vector nodes; nodes.reserve(symbols.size()); for (const auto symbol : symbols) { nodes.push_back(leaf(symbol)); } return nodes; } public: RangeNondecreasingSubsequence() = default; explicit RangeNondecreasingSubsequence(std::span symbols) : tree_(build(symbols)) { } [[nodiscard]] int size() const noexcept { return tree_.size(); } [[nodiscard]] bool empty() const noexcept { return tree_.empty(); } void set(int position, int symbol) { tree_.set(position, leaf(symbol)); } [[nodiscard]] int longest(int left, int right) const { return tree_.prod(left, right).get(0, Alphabet - 1); } [[nodiscard]] int longest(int left, int right, int low, int high) const { #ifndef SKIP_RUNTIME_CHECKS check_symbol(low); check_symbol(high); if (low > high) { throw std::invalid_argument("RangeNondecreasingSubsequence: symbol interval"); } #endif return tree_.prod(left, right).get(static_cast(low), static_cast(high)); } [[nodiscard]] int replacement_count(int left, int right) const { const auto keep = longest(left, right); return right - left - keep; } [[nodiscard]] int all() const { return tree_.all_prod().get(0, Alphabet - 1); } }; #endif } // namespace sirkit // SIRKIT_NAMESPACE_CLOSE_V1 // SIRKIT_NAMESPACE_END_V1 namespace sirkit {} using namespace sirkit; // SPDX-License-Identifier: CC0-1.0 // Verify I/O: Verify/current/common/fast_io.hpp // SPDX-License-Identifier: CC0-1.0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef SIRKIT_VERIFY_FAST_IO_DEFINED #define SIRKIT_VERIFY_FAST_IO_DEFINED // VerifyInput(file=stdin): 非所有のFILEから読む。別の入力方式と混用しない。 // >> 整数: 64bit以下の十進整数を読む。検査省略時は値域内の正しい入力を前提とする。 // >> char/string: 空白を飛ばし、1文字/空白までの文字列を読む。 // >> 浮動小数点: 十進文字列を変換する。通常のCロケールで使う。 // bool / !: 直前までの読み取りが成功しているか/失敗したかを返す。 class VerifyInput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t position_ = 0, size_ = 0; bool good_ = true; int peek() { if (position_ == size_) { size_ = std::fread(buffer_.data(), 1, buffer_size, file_); buffer_[size_] = 0; // 補充後の末尾は常に数字でない番兵。 position_ = 0; if (size_ == 0) return EOF; } return buffer_[position_]; } int skip_space() { int c; while ((c = peek()) != EOF && c <= ' ') ++position_; if (c == EOF) good_ = false; return c; } public: explicit VerifyInput(std::FILE* file = stdin) : file_(file) {} VerifyInput(const VerifyInput&) = delete; VerifyInput& operator=(const VerifyInput&) = delete; explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyInput& operator>>(T& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; bool negative = false; if (c == '-' || c == '+') { negative = c == '-'; ++position_; c = peek(); } if (c == EOF) { good_ = false; return *this; } #ifndef SKIP_RUNTIME_CHECKS if (c < '0' || c > '9') { good_ = false; return *this; } // 先頭の0を除けば、20桁以下の末尾直前の値はuint64_tに収まる。 while (c == '0') { ++position_; c = peek(); } std::uint64_t previous = 0; unsigned last_digit = 0; std::size_t digits = 0; #endif std::uint64_t value = 0; #ifdef SKIP_RUNTIME_CHECKS // バッファ末尾の番兵で内側の範囲判定を省き、数字が境界をまたぐときだけ補充する。 for (;;) { const unsigned char* first = buffer_.data() + position_; const unsigned char* cursor = first; while (static_cast(*cursor - '0') < 10) { value = value * 10 + static_cast(*cursor - '0'); ++cursor; } position_ += static_cast(cursor - first); if (position_ != size_) break; c = peek(); if (c == EOF) break; } #else while (c >= '0' && c <= '9') { previous = value; last_digit = static_cast(c - '0'); if (digits <= 20) ++digits; value = value * 10 + static_cast(c - '0'); ++position_; c = peek(); } #endif #ifndef SKIP_RUNTIME_CHECKS constexpr auto maximum = std::numeric_limits::max(); if ((c != EOF && c > ' ') || digits > 20 || (digits == 20 && (previous > maximum / 10 || (previous == maximum / 10 && last_digit > maximum % 10)))) { good_ = false; return *this; } std::uint64_t limit = static_cast(std::numeric_limits::max()); if constexpr (std::is_signed_v) limit += negative; else if (negative && value != 0) good_ = false; if (value > limit) good_ = false; if (!good_) return *this; #endif if constexpr (std::is_signed_v) { constexpr std::uint64_t minimum_magnitude = static_cast(std::numeric_limits::max()) + 1; // -(最小値)は評価せず、絶対値が最大値+1のときだけ最小値を使う。 result = negative ? (value == minimum_magnitude ? std::numeric_limits::min() : static_cast(-static_cast(value))) : static_cast(value); } else result = static_cast(value); return *this; } VerifyInput& operator>>(char& result) { if (!good_) return *this; const int c = skip_space(); if (c != EOF) { result = static_cast(c); ++position_; } return *this; } VerifyInput& operator>>(std::string& result) { if (!good_) return *this; int c = skip_space(); if (c == EOF) return *this; result.clear(); for (;;) { const std::size_t first = position_; while (position_ < size_ && buffer_[position_] > ' ') ++position_; result.append(reinterpret_cast(buffer_.data() + first), position_ - first); if (position_ != size_ || peek() == EOF) break; } return *this; } template requires std::is_floating_point_v VerifyInput& operator>>(T& result) { std::string token; *this >> token; if (!good_) return *this; #ifndef SKIP_RUNTIME_CHECKS char* end = nullptr; const long double value = std::strtold(token.c_str(), &end); if (end != token.c_str() + token.size()) good_ = false; else result = static_cast(value); #else result = static_cast(std::strtold(token.c_str(), nullptr)); #endif return *this; } }; // VerifyOutput(file=stdout): 非所有のFILEへ書く。破棄時にflushし、FILEは閉じない。 // << 整数/char/文字列/浮動小数点: 値を出力する。整数は64bit以下に対応する。 // fixed(digits): 浮動小数点を小数点以下digits桁で出力する(digits>=0)。 // flush(): バッファの内容をFILEへ渡す。bool / !で書き込み失敗を確認できる。 class VerifyOutput { static constexpr std::size_t buffer_size = 1 << 17; std::FILE* file_; std::array buffer_; std::size_t size_ = 0; int precision_ = 6; bool fixed_ = false, good_ = true; void write(std::string_view text) { while (!text.empty() && good_) { if (size_ == buffer_.size()) flush(); const auto count = std::min(text.size(), buffer_.size() - size_); std::memcpy(buffer_.data() + size_, text.data(), count); size_ += count; text.remove_prefix(count); } } public: explicit VerifyOutput(std::FILE* file = stdout) : file_(file) {} VerifyOutput(const VerifyOutput&) = delete; VerifyOutput& operator=(const VerifyOutput&) = delete; ~VerifyOutput() { flush(); } explicit operator bool() const noexcept { return good_; } bool operator!() const noexcept { return !good_; } VerifyOutput& fixed(int digits) { if (digits < 0) throw std::invalid_argument("negative output precision"); fixed_ = true; precision_ = digits; return *this; } void flush() noexcept { std::size_t written = 0; while (written < size_ && good_) { const auto count = std::fwrite(buffer_.data() + written, 1, size_ - written, file_); if (count == 0) good_ = false; written += count; } size_ = 0; } template requires (std::is_integral_v && sizeof(T) <= sizeof(std::uint64_t) && !std::is_same_v) VerifyOutput& operator<<(T value) { if constexpr (std::is_same_v) return *this << static_cast('0' + value); else { constexpr std::size_t capacity = std::numeric_limits::digits10 + 3; if (buffer_.size() - size_ < capacity) flush(); if (!good_) return *this; const auto end = std::to_chars(buffer_.data() + size_, buffer_.data() + size_ + capacity, value).ptr; size_ = static_cast(end - buffer_.data()); return *this; } } VerifyOutput& operator<<(char value) { if (size_ == buffer_.size()) flush(); if (good_) buffer_[size_++] = value; return *this; } VerifyOutput& operator<<(std::string_view value) { write(value); return *this; } VerifyOutput& operator<<(const std::string& value) { write(value); return *this; } VerifyOutput& operator<<(const char* value) { write(value); return *this; } template requires std::is_floating_point_v VerifyOutput& operator<<(T value) { char local[128]; const char* format = fixed_ ? "%.*Lf" : "%.*Lg"; const int count = std::snprintf(local, sizeof(local), format, precision_, static_cast(value)); if (count < 0) good_ = false; else if (static_cast(count) < sizeof(local)) write(std::string_view(local, static_cast(count))); else { std::vector text(static_cast(count) + 1); std::snprintf(text.data(), text.size(), format, precision_, static_cast(value)); write(std::string_view(text.data(), static_cast(count))); } return *this; } }; inline VerifyInput verify_in; inline VerifyOutput verify_out; #endif #include #include #include int main() { int n{}, q{}; std::string text; verify_in >> n >> q >> text; std::vector symbols(static_cast(n)); for (int i = 0; i < n; ++i) { symbols[static_cast(i)] = text[static_cast(i)] - 'A'; } sirkit::RangeNondecreasingSubsequence<4> range(symbols); for (int query = 0; query < q; ++query) { int type{}, left{}; verify_in >> type >> left; if (type == 1) { char symbol{}; verify_in >> symbol; range.set(left - 1, symbol - 'A'); } else { int right{}; verify_in >> right; verify_out << range.replacement_count(left - 1, right) << '\n'; } } }