#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif constexpr LL mod = 998'244'353; LL add(LL a, LL b) { a += b; return a >= mod ? a - mod : a; } LL sub(LL a, LL b) { return add(a, mod - b); } LL mul(LL a, LL b) { return a * b % mod; } constexpr int A = 6; using TM = array<array<LL, A + 1>, A + 1>; void clear_matrix(TM& m) { REP(i, A + 1) { REP(j, A + 1) { m[i][j] = 0; } } } TM operator*(TM a, TM b) { TM c; clear_matrix(c); REP(k, A + 1) { REP(i, A + 1) { REP(j, A + 1) { c[i][j] += a[i][k] * b[k][j]; } } } REP(i, A + 1) { REP(j, A + 1) { c[i][j] %= mod; } } return c; } TM id() { TM ret; REP(i, A + 1) REP(j, A + 1) ret[i][j] = i == j; return ret; } struct Tree { int sz = 1; vector<TM> tree; Tree(vector<TM> v) { const int n = ssize(v); while (sz < n) sz *= 2; tree.resize(2 * sz); REP(i, n) tree[sz + i] = v[i]; FOR(i, n, sz - 1) tree[sz + i] = id(); for (int i = sz - 1; i >= 1; --i) tree[i] = tree[i * 2] * tree[i * 2 + 1]; } void update(int w, TM m) { w += sz; if (tree[w] == m) return; tree[w] = m; while (w /= 2) { const auto new_m = tree[w * 2] * tree[w * 2 + 1]; if (tree[w] == new_m) return; tree[w] = new_m; } } TM query() { return tree[1]; } }; vector<TM> tm_matrices(A); constexpr int N = 1 << A; vector um_matrices(A, vector<TM>(N)); void solve() { int n, q; cin >> n >> q; vector<int> v(n); REP(i, n) { char x; cin >> x; v[i] = int(x - 'a'); } vector<TM> initial_tm(n); REP(i, n) initial_tm[i] = tm_matrices[v[i]]; Tree tm_tree(initial_tm); vector<set<int>> s(A); REP(i, A) s[i].emplace(-1); REP(i, n) s[v[i]].emplace(i); auto get_mask_for_position = [&](int pos) -> int { const int letter = v[pos]; auto it = s[letter].find(pos); int left = -1; if (it != s[letter].begin()) { left = *prev(it); } int mask = 0; REP(i, A) { if (i == letter) continue; auto h = s[i].upper_bound(left); if (h != s[i].end() and *h < pos) { mask |= (1 << i); } } return mask; }; auto get_um_for_position = [&](int pos) -> TM { const auto mask = get_mask_for_position(pos); const int letter = v[pos]; auto ret = um_matrices[letter][mask]; if (prev(s[letter].find(pos)) == s[letter].begin()) { ret[letter][A] = 1; ret[letter][letter] = 0; } return ret; }; vector<TM> initial_um(n); REP(i, n) initial_um[i] = get_um_for_position(i); reverse(initial_um.begin(), initial_um.end()); Tree um_tree(initial_um); auto get_total = [&]() { auto m = tm_tree.query(); return sub(m[A][A], 1); }; auto get_unique = [&]() { auto m = um_tree.query(); int ret = 0; REP(i, A) ret = int(add(ret, m[i][A])); return ret; }; auto get_answer = [&]() { return sub(get_total(), get_unique()); }; cout << get_answer() << '\n'; REP(i, q) { int p; char x; cin >> p >> x; --p; s[v[p]].erase(p); v[p] = int(x - 'a'); s[v[p]].emplace(p); tm_tree.update(p, tm_matrices[v[p]]); um_tree.update(n - 1 - p, get_um_for_position(p)); REP(j, A) { auto it = s[j].upper_bound(p); if (it == s[j].end()) continue; const int h = *it; um_tree.update(n - 1 - h, get_um_for_position(h)); } cout << get_answer() << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); REP(i, A) { tm_matrices[i] = id(); tm_matrices[i][i][i] = 0; tm_matrices[i][i][A] = 1; tm_matrices[i][A][A] = 2; tm_matrices[i][A][i] = mod - 1; } REP(i, A) { REP(mask, N) { if ((mask >> i) & 1) continue; um_matrices[i][mask] = id(); REP(j, A) { if (i == j) continue; um_matrices[i][mask][i][j] = (mask >> j) & 1; } } } solve(); }
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif constexpr LL mod = 998'244'353; LL add(LL a, LL b) { a += b; return a >= mod ? a - mod : a; } LL sub(LL a, LL b) { return add(a, mod - b); } LL mul(LL a, LL b) { return a * b % mod; } constexpr int A = 6; using TM = array<array<LL, A + 1>, A + 1>; void clear_matrix(TM& m) { REP(i, A + 1) { REP(j, A + 1) { m[i][j] = 0; } } } TM operator*(TM a, TM b) { TM c; clear_matrix(c); REP(k, A + 1) { REP(i, A + 1) { REP(j, A + 1) { c[i][j] += a[i][k] * b[k][j]; } } } REP(i, A + 1) { REP(j, A + 1) { c[i][j] %= mod; } } return c; } TM id() { TM ret; REP(i, A + 1) REP(j, A + 1) ret[i][j] = i == j; return ret; } struct Tree { int sz = 1; vector<TM> tree; Tree(vector<TM> v) { const int n = ssize(v); while (sz < n) sz *= 2; tree.resize(2 * sz); REP(i, n) tree[sz + i] = v[i]; FOR(i, n, sz - 1) tree[sz + i] = id(); for (int i = sz - 1; i >= 1; --i) tree[i] = tree[i * 2] * tree[i * 2 + 1]; } void update(int w, TM m) { w += sz; if (tree[w] == m) return; tree[w] = m; while (w /= 2) { const auto new_m = tree[w * 2] * tree[w * 2 + 1]; if (tree[w] == new_m) return; tree[w] = new_m; } } TM query() { return tree[1]; } }; vector<TM> tm_matrices(A); constexpr int N = 1 << A; vector um_matrices(A, vector<TM>(N)); void solve() { int n, q; cin >> n >> q; vector<int> v(n); REP(i, n) { char x; cin >> x; v[i] = int(x - 'a'); } vector<TM> initial_tm(n); REP(i, n) initial_tm[i] = tm_matrices[v[i]]; Tree tm_tree(initial_tm); vector<set<int>> s(A); REP(i, A) s[i].emplace(-1); REP(i, n) s[v[i]].emplace(i); auto get_mask_for_position = [&](int pos) -> int { const int letter = v[pos]; auto it = s[letter].find(pos); int left = -1; if (it != s[letter].begin()) { left = *prev(it); } int mask = 0; REP(i, A) { if (i == letter) continue; auto h = s[i].upper_bound(left); if (h != s[i].end() and *h < pos) { mask |= (1 << i); } } return mask; }; auto get_um_for_position = [&](int pos) -> TM { const auto mask = get_mask_for_position(pos); const int letter = v[pos]; auto ret = um_matrices[letter][mask]; if (prev(s[letter].find(pos)) == s[letter].begin()) { ret[letter][A] = 1; ret[letter][letter] = 0; } return ret; }; vector<TM> initial_um(n); REP(i, n) initial_um[i] = get_um_for_position(i); reverse(initial_um.begin(), initial_um.end()); Tree um_tree(initial_um); auto get_total = [&]() { auto m = tm_tree.query(); return sub(m[A][A], 1); }; auto get_unique = [&]() { auto m = um_tree.query(); int ret = 0; REP(i, A) ret = int(add(ret, m[i][A])); return ret; }; auto get_answer = [&]() { return sub(get_total(), get_unique()); }; cout << get_answer() << '\n'; REP(i, q) { int p; char x; cin >> p >> x; --p; s[v[p]].erase(p); v[p] = int(x - 'a'); s[v[p]].emplace(p); tm_tree.update(p, tm_matrices[v[p]]); um_tree.update(n - 1 - p, get_um_for_position(p)); REP(j, A) { auto it = s[j].upper_bound(p); if (it == s[j].end()) continue; const int h = *it; um_tree.update(n - 1 - h, get_um_for_position(h)); } cout << get_answer() << '\n'; } } int main() { cin.tie(0)->sync_with_stdio(0); REP(i, A) { tm_matrices[i] = id(); tm_matrices[i][i][i] = 0; tm_matrices[i][i][A] = 1; tm_matrices[i][A][A] = 2; tm_matrices[i][A][i] = mod - 1; } REP(i, A) { REP(mask, N) { if ((mask >> i) & 1) continue; um_matrices[i][mask] = id(); REP(j, A) { if (i == j) continue; um_matrices[i][mask][i][j] = (mask >> j) & 1; } } } solve(); } |