/* -*- coding: utf-8 -*- * * 2992.cc: No.2992 Range ABCD String Query - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int INF = 1 << 30; /* typedef */ struct Elm { int ss[4][4]; Elm(): ss() {} Elm(int d) { for (int i = 0; i < 4; i++) for (int j = i; j < 4; j++) ss[i][j] = (d < i || j < d) ? 1 : 0; } Elm operator+(const Elm &e) const { Elm r; for (int i = 0; i <= 4; i++) for (int j = i; j < 4; j++) { r.ss[i][j] = INF; for (int k = i; k <= j; k++) r.ss[i][j] = min(r.ss[i][j], ss[i][k] + e.ss[k][j]); } return r; } int val() const { return ss[0][3]; } }; template struct SegTreeSum { int e2; vector nodes; T defv; SegTreeSum() {} void init(int n, T _defv) { defv = _defv; for (e2 = 1; e2 < n; e2 <<= 1); nodes.assign(e2 * 2, defv); } T &geti(int i) { return nodes[e2 - 1 + i]; } void seti(int i, T v) { geti(i) = v; } void setall() { for (int j = e2 - 2; j >= 0; j--) nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; } void set(int i, T v) { int j = e2 - 1 + i; nodes[j] = v; while (j > 0) { j = (j - 1) / 2; nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; } } T sum_range(int r0, int r1, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return defv; if (r0 <= i0 && i1 <= r1) return nodes[k]; int im = (i0 + i1) / 2; T v0 = sum_range(r0, r1, k * 2 + 1, i0, im); T v1 = sum_range(r0, r1, k * 2 + 2, im, i1); return v0 + v1; } T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); } }; /* global variables */ char s[MAX_N + 4]; SegTreeSum st; /* subroutines */ /* main */ int main() { int n, qn; scanf("%d%d%s", &n, &qn, s); st.init(n, Elm()); for (int i = 0; i < n; i++) st.seti(i, Elm(s[i] - 'A')); st.setall(); while (qn--) { int op; scanf("%d", &op); if (op == 1) { int x; char c[4]; scanf("%d%s", &x, c), x--; st.set(x, Elm(c[0] - 'A')); } else { int l, r; scanf("%d%d", &l, &r), l--; auto e = st.sum_range(l, r); printf("%d\n", e.val()); } } return 0; }