1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include <iostream> #include <algorithm> #define maxn 100010 #define ll long long #define INF 1000000000 using namespace std;
int n, m; char s[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { int a, b, c, ab, bc, abc; } T[maxn * 4]; inline Seg maintain(const Seg &ls, const Seg &rs) { Seg o; o.a = ls.a + rs.a; o.b = ls.b + rs.b; o.c = ls.c + rs.c; o.ab = min(ls.a + rs.ab, ls.ab + rs.b); o.bc = min(ls.b + rs.bc, ls.bc + rs.c); o.abc = min({ ls.abc + rs.c, ls.ab + rs.bc, ls.a + rs.abc }); return o; }
void build(int i, int l, int r) { if (l == r) { T[i] = { 0, 0, 0, 0, 0, 0 }; if (s[l] == 'a') T[i].a = 1; else if (s[l] == 'b') T[i].b = 1; else T[i].c = 1; return ; } int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); T[i] = maintain(T[lc], T[rc]); }
void update(int i, int l, int r, int k, char ch) { if (l == r) { T[i] = { 0, 0, 0, 0, 0, 0 }; if (ch == 'a') T[i].a = 1; else if (ch == 'b') T[i].b = 1; else T[i].c = 1; return ; } int m = l + r >> 1; if (k <= m) update(lc, l, m, k, ch); else update(rc, m + 1, r, k, ch); T[i] = maintain(T[lc], T[rc]); }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m >> s + 1; build(1, 1, n); for (int i = 1; i <= m; ++i) { int x; char c[3]; cin >> x >> c; update(1, 1, n, x, c[0]); cout << T[1].abc << "\n"; } return 0; }
|