// Library Codes are in: https://github.com/reinsirk/sirkit-cpp-library // GCC バグ対策 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128217 #pragma GCC optimize("no-tree-loop-vectorize") #include #include using namespace std; using namespace atcoder; #define SKIP_RUNTIME_CHECKS constexpr int64_t INF = static_cast(1) << 60; namespace sirkit { #ifndef SIRKIT_INF_DEFINED #define SIRKIT_INF_DEFINED inline constexpr std::int64_t kInf = std::int64_t{1} << 60; #endif #ifndef SIRKIT_UNWEIGHTED_GRAPH_DEFINED #define SIRKIT_UNWEIGHTED_GRAPH_DEFINED using Graph = std::vector>; #endif #ifndef SIRKIT_GRAPH_TYPES_DEFINED #define SIRKIT_GRAPH_TYPES_DEFINED struct Edge { int to; std::int64_t w; Edge(int destination, std::int64_t weight) : to(destination), w(weight) { } }; using WGraph = std::vector>; #endif #ifndef SIRKIT_CHMIN_DEFINED #define SIRKIT_CHMIN_DEFINED #define SIRKIT_TEMP_DEFINE_CHMIN template constexpr bool chmin(T &a, T b); #endif #ifndef SIRKIT_CHMAX_DEFINED #define SIRKIT_CHMAX_DEFINED #define SIRKIT_TEMP_DEFINE_CHMAX template constexpr bool chmax(T &a, T b); #endif #ifndef SIRKIT_POW64I_DEFINED #define SIRKIT_POW64I_DEFINED #define SIRKIT_TEMP_DEFINE_POW64I constexpr std::uint64_t pow64i(std::uint64_t x, std::uint64_t n); #endif #ifndef SIRKIT_IS_MUL_OVERFLOW_DEFINED #define SIRKIT_IS_MUL_OVERFLOW_DEFINED #define SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW constexpr bool is_mul_overflow(std::int64_t a, std::int64_t b); #endif } // namespace sirkit using namespace sirkit; struct S { int s; int id; }; using F = int64_t; S op(S a, S b) { if (a.s <= b.s) { return b; } else { return a; } } S e() { return {-10000000, 0}; } S mapping(F f, S x) { x.s += f; return x; } F composition(F f, F g) { return f + g; } F id() { return 0; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, q; cin >> n >> q; vector> v(4, vector(n)); string s; cin >> s; vector> fx(4, fenwick_tree(n)); for (int i = 0; i < 4; i++) { for (int j = 0; j < n; j++) { int t = -1; if (s.at(j) == 'A' + i) { t = 1; fx.at(i).add(j, 1); } else if (s.at(j) < 'A' + i) { t = 0; } v.at(i).at(j) = {t, j}; } for (int j = 1; j < n; j++) { v.at(i).at(j).s += v.at(i).at(j - 1).s; } } vector> sg(4); for (int i = 0; i < 4; i++) { sg.at(i) = lazy_segtree(v.at(i)); } for (; q > 0; q--) { int t; cin >> t; if (t == 1) { int x; char c; cin >> x >> c; x--; sg.at(s.at(x) - 'A').apply(x, n, -2); fx.at(s.at(x) - 'A').add(x, -1); for (int j = s.at(x) - 'A' + 1; j < 4; j++) { sg.at(j).apply(x, n, -1); } sg.at(c - 'A').apply(x, n, +2); fx.at(c - 'A').add(x, 1); for (int j = c - 'A' + 1; j < 4; j++) { sg.at(j).apply(x, n, 1); } s.at(x) = c; } else { int l, r; cin >> l >> r; l--; int lx = l; int ans = 0; for (int i = 0; i < 3; i++) { auto x = sg.at(i).prod(l, r); if (l != 0) { x.s -= sg.at(i).get(l - 1).s; } if (x.s >= 0) { ans += fx.at(i).sum(l, x.id + 1); l = x.id + 1; } } ans += fx.back().sum(l, r); cout << r - lx - ans << endl; } } return 0; } namespace sirkit { #ifdef SIRKIT_TEMP_DEFINE_CHMIN #undef SIRKIT_TEMP_DEFINE_CHMIN template constexpr bool chmin(T &a, T b) { if (b < a) { a = b; return true; } return false; } #endif #ifdef SIRKIT_TEMP_DEFINE_CHMAX #undef SIRKIT_TEMP_DEFINE_CHMAX template constexpr bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } #endif #ifdef SIRKIT_TEMP_DEFINE_POW64I #undef SIRKIT_TEMP_DEFINE_POW64I constexpr std::uint64_t pow64i(std::uint64_t x, std::uint64_t n) { std::uint64_t result = 1; while (n != 0) { if ((n & 1) != 0) { result *= x; } n >>= 1; if (n != 0) { x *= x; } } return result; } #endif #ifdef SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW #undef SIRKIT_TEMP_DEFINE_IS_MUL_OVERFLOW constexpr bool is_mul_overflow(std::int64_t a, std::int64_t b) { #if (defined(__GNUC__) || defined(__clang__)) && !defined(SIRKIT_FORCE_STANDARD_MUL_OVERFLOW) std::int64_t product; return __builtin_mul_overflow(a, b, &product); #else // 最小負数の絶対値も符号なし演算で安全に求める。 const std::uint64_t ua = a < 0 ? std::uint64_t{0} - static_cast(a) : static_cast(a); const std::uint64_t ub = b < 0 ? std::uint64_t{0} - static_cast(b) : static_cast(b); const std::uint64_t limit = (a < 0) != (b < 0) ? std::uint64_t{1} << 63 : static_cast(std::numeric_limits::max()); return ub != 0 && ua > limit / ub; #endif } #endif } // namespace sirkit