#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif template<int M, int R> struct Mod { static const int mod = M, rt = R; int x; Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; } Mod& operator+=(Mod o) { if ((x += o.x) >= M) x -= M; return *this; } Mod& operator-=(Mod o) { if ((x -= o.x) < 0) x += M; return *this; } Mod& operator*=(Mod o) { x = 1ll * x * o.x % M; return *this; } Mod& operator/=(Mod o) { return *this *= o.inv(); } friend Mod operator+(Mod a, Mod b) { return a += b; } friend Mod operator-(Mod a, Mod b) { return a -= b; } friend Mod operator*(Mod a, Mod b) { return a *= b; } friend Mod operator/(Mod a, Mod b) { return a /= b; } auto operator<=>(const Mod&) const = default; Mod pow(ll n) const { Mod a = x, b = 1; for (; n; n /= 2, a *= a) if (n % 2) b *= a; return b; } Mod inv() const { assert(x); return pow(M - 2); } friend ostream& operator<<(ostream& os, Mod x) { return os << x.x; } }; using mint = Mod<998244353, 3>; mint comp(string& s) { const int num = 6; int n = s.length(); vector<mint> dp_dist(n + 1), dp_unq(n + 1); dp_dist[0] = dp_unq[0] = 1; vector<int> last(num + 1, -1); last[num] = 0; for(int i = 1; i <= n; i++) { int ind = s[i - 1] - 'a'; int lst = last[ind]; for(int j = 0; j <= num; j++) { if(last[j] >= lst && last[j] != -1) { dp_unq[i] += dp_unq[last[j]]; } } dp_dist[i] = 2 * dp_dist[i - 1]; if(lst != -1) dp_dist[i] -= dp_dist[lst - 1]; last[ind] = i; } mint res = dp_dist[n]; for(int i = 0; i <= num; i++) { int lst = last[i]; if(lst != -1) res -= dp_unq[lst]; } return res; } template<int num> struct seg_dist { using mat = array<array<mint, num + 1>, num + 1>; vector<mat> t; int n; seg_dist(int nn) { n = 1; while(n < nn) n *= 2; mat id = {}; for(int i = 0; i <= num; i++) id[i][i] = 1; t.assign(2 * n, id); } void init(int i, int x) { mat& m = t[i]; m = {}; for(int j = 0; j < num; j++) m[j][j] = 1; m[x][x] = 0; m[x][num] = 1; m[num][num] = 2; m[num][x] = -1; } void build(string& s) { for(int i = 0; i < s.length(); i++) { init(i + n, s[i] - 'a'); } for(int i = n - 1; i > 0; i--) { t[i] = merge(t[2 * i + 1], t[2 * i]); } } void upd(int i, int x) { i += n; init(i, x); while(i /= 2) t[i] = merge(t[2 * i + 1], t[2 * i]); } mint get() { return t[1][num][num]; } mat merge(const mat& a, const mat& b) { mat c = {}; for(int i = 0; i <= num; i++) { for(int k = 0; k <= num; k++) { for(int j = 0; j <= num; j++) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } }; template<int num> struct seg_unq { static constexpr int sz = (num + 1) * (num + 1); using mat = array<array<mint, sz>, sz>; vector<mat> t; int n; seg_unq(int nn) { n = 1; while(n < nn) n *= 2; mat id = {}; for(int i = 0; i < sz; i++) id[i][i] = 1; t.assign(2 * n, id); } void init(int i, int x) { mat& m = t[i]; m = {}; for(int j = 0; j < sz; j++) { m[j][j] = 1; } int ind_unq = num * (num + 1) + x; m[ind_unq][ind_unq] = 0; for(int j = 0; j <= num; j++) { m[ind_unq][x * (num + 1) + j] = 1; } for(int j = 0; j < num; j++) { if(j != x) { int id = x * (num + 1) + j; m[id][id] = 0; } int id = j * (num + 1) + x; m[id][id] = 0; for(int k = 0; k <= num; k++) { m[id][x * (num + 1) + k] = 1; } } int id = x * (num + 1) + num; m[id][id] = 0; } void build(string& s) { for(int i = 0; i < s.length(); i++) { init(i + n, s[i] - 'a'); } for(int i = n - 1; i > 0; i--) { t[i] = merge(t[2 * i + 1], t[2 * i]); } } void upd(int i, int x) { i += n; init(i, x); while(i /= 2) t[i] = merge(t[2 * i + 1], t[2 * i]); } mint get() { mint res = 0; for(int i = 0; i <= num; i++) { int id1 = num * (num + 1) + i; for(int j = 0; j <= num; j++) { int id2 = j * (num + 1) + num; res += t[1][id1][id2]; } } return res; } mat merge(const mat& a, const mat& b) { mat c = {}; for(int i = 0; i < sz; i++) { for(int k = 0; k < sz; k++) { for(int j = 0; j < sz; j++) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; string s; cin >> s; bool f = 1; for(char c : s) { if(c != 'a' && c != 'b') f = 0; } vector<pair<int, int>> op(q); for(int i = 0; i < q; i++) { int id; char c; cin >> id >> c; op[i] = {id - 1, c - 'a'}; if(c != 'a' && c != 'b') f = 0; } if(f) { seg_dist<2> dist(n); seg_unq<2> unq(n); dist.build(s); unq.build(s); cout << dist.get() - unq.get() << '\n'; for(int i = 0; i < q; i++) { auto [id, x] = op[i]; dist.upd(id, x); unq.upd(id, x); cout << dist.get() - unq.get() << '\n'; } } else { cout << comp(s) << '\n'; for(int i = 0; i < q; i++) { auto [id, x] = op[i]; s[id] = 'a' + x; cout << comp(s) << '\n'; } } }
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif template<int M, int R> struct Mod { static const int mod = M, rt = R; int x; Mod(ll y = 0) : x(y % M) { x += (x < 0) * M; } Mod& operator+=(Mod o) { if ((x += o.x) >= M) x -= M; return *this; } Mod& operator-=(Mod o) { if ((x -= o.x) < 0) x += M; return *this; } Mod& operator*=(Mod o) { x = 1ll * x * o.x % M; return *this; } Mod& operator/=(Mod o) { return *this *= o.inv(); } friend Mod operator+(Mod a, Mod b) { return a += b; } friend Mod operator-(Mod a, Mod b) { return a -= b; } friend Mod operator*(Mod a, Mod b) { return a *= b; } friend Mod operator/(Mod a, Mod b) { return a /= b; } auto operator<=>(const Mod&) const = default; Mod pow(ll n) const { Mod a = x, b = 1; for (; n; n /= 2, a *= a) if (n % 2) b *= a; return b; } Mod inv() const { assert(x); return pow(M - 2); } friend ostream& operator<<(ostream& os, Mod x) { return os << x.x; } }; using mint = Mod<998244353, 3>; mint comp(string& s) { const int num = 6; int n = s.length(); vector<mint> dp_dist(n + 1), dp_unq(n + 1); dp_dist[0] = dp_unq[0] = 1; vector<int> last(num + 1, -1); last[num] = 0; for(int i = 1; i <= n; i++) { int ind = s[i - 1] - 'a'; int lst = last[ind]; for(int j = 0; j <= num; j++) { if(last[j] >= lst && last[j] != -1) { dp_unq[i] += dp_unq[last[j]]; } } dp_dist[i] = 2 * dp_dist[i - 1]; if(lst != -1) dp_dist[i] -= dp_dist[lst - 1]; last[ind] = i; } mint res = dp_dist[n]; for(int i = 0; i <= num; i++) { int lst = last[i]; if(lst != -1) res -= dp_unq[lst]; } return res; } template<int num> struct seg_dist { using mat = array<array<mint, num + 1>, num + 1>; vector<mat> t; int n; seg_dist(int nn) { n = 1; while(n < nn) n *= 2; mat id = {}; for(int i = 0; i <= num; i++) id[i][i] = 1; t.assign(2 * n, id); } void init(int i, int x) { mat& m = t[i]; m = {}; for(int j = 0; j < num; j++) m[j][j] = 1; m[x][x] = 0; m[x][num] = 1; m[num][num] = 2; m[num][x] = -1; } void build(string& s) { for(int i = 0; i < s.length(); i++) { init(i + n, s[i] - 'a'); } for(int i = n - 1; i > 0; i--) { t[i] = merge(t[2 * i + 1], t[2 * i]); } } void upd(int i, int x) { i += n; init(i, x); while(i /= 2) t[i] = merge(t[2 * i + 1], t[2 * i]); } mint get() { return t[1][num][num]; } mat merge(const mat& a, const mat& b) { mat c = {}; for(int i = 0; i <= num; i++) { for(int k = 0; k <= num; k++) { for(int j = 0; j <= num; j++) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } }; template<int num> struct seg_unq { static constexpr int sz = (num + 1) * (num + 1); using mat = array<array<mint, sz>, sz>; vector<mat> t; int n; seg_unq(int nn) { n = 1; while(n < nn) n *= 2; mat id = {}; for(int i = 0; i < sz; i++) id[i][i] = 1; t.assign(2 * n, id); } void init(int i, int x) { mat& m = t[i]; m = {}; for(int j = 0; j < sz; j++) { m[j][j] = 1; } int ind_unq = num * (num + 1) + x; m[ind_unq][ind_unq] = 0; for(int j = 0; j <= num; j++) { m[ind_unq][x * (num + 1) + j] = 1; } for(int j = 0; j < num; j++) { if(j != x) { int id = x * (num + 1) + j; m[id][id] = 0; } int id = j * (num + 1) + x; m[id][id] = 0; for(int k = 0; k <= num; k++) { m[id][x * (num + 1) + k] = 1; } } int id = x * (num + 1) + num; m[id][id] = 0; } void build(string& s) { for(int i = 0; i < s.length(); i++) { init(i + n, s[i] - 'a'); } for(int i = n - 1; i > 0; i--) { t[i] = merge(t[2 * i + 1], t[2 * i]); } } void upd(int i, int x) { i += n; init(i, x); while(i /= 2) t[i] = merge(t[2 * i + 1], t[2 * i]); } mint get() { mint res = 0; for(int i = 0; i <= num; i++) { int id1 = num * (num + 1) + i; for(int j = 0; j <= num; j++) { int id2 = j * (num + 1) + num; res += t[1][id1][id2]; } } return res; } mat merge(const mat& a, const mat& b) { mat c = {}; for(int i = 0; i < sz; i++) { for(int k = 0; k < sz; k++) { for(int j = 0; j < sz; j++) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; string s; cin >> s; bool f = 1; for(char c : s) { if(c != 'a' && c != 'b') f = 0; } vector<pair<int, int>> op(q); for(int i = 0; i < q; i++) { int id; char c; cin >> id >> c; op[i] = {id - 1, c - 'a'}; if(c != 'a' && c != 'b') f = 0; } if(f) { seg_dist<2> dist(n); seg_unq<2> unq(n); dist.build(s); unq.build(s); cout << dist.get() - unq.get() << '\n'; for(int i = 0; i < q; i++) { auto [id, x] = op[i]; dist.upd(id, x); unq.upd(id, x); cout << dist.get() - unq.get() << '\n'; } } else { cout << comp(s) << '\n'; for(int i = 0; i < q; i++) { auto [id, x] = op[i]; s[id] = 'a' + x; cout << comp(s) << '\n'; } } } |