#include <bits/stdc++.h>
using namespace std;
namespace atcoder {
namespace internal {
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
#endif
// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
#if __cplusplus >= 201703L
template <class S, auto op, auto e> struct segtree {
static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
"op must work as S(S, S)");
static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
"e must work as S()");
#else
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
#endif
public:
segtree() : segtree(0) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
size = (int)internal::bit_ceil((unsigned int)(_n));
log = internal::countr_zero((unsigned int)size);
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) const {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() const { return d[1]; }
template <bool (*f)(S)> int max_right(int l) const {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) const {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) const {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) const {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
template <typename T> T mod_inv_in_range(T a, T m) {
// assert(0 <= a && a < m);
T x = a, y = m;
// coeff of a in x and y
T vx = 1, vy = 0;
while (x) {
T k = y / x;
y %= x;
vy -= k * vx;
std::swap(x, y);
std::swap(vx, vy);
}
assert(y == 1);
return vy < 0 ? m + vy : vy;
}
template <typename T> struct extended_gcd_result {
T gcd;
T coeff_a, coeff_b;
};
template <typename T> extended_gcd_result<T> extended_gcd(T a, T b) {
T x = a, y = b;
// coeff of a and b in x and y
T ax = 1, ay = 0;
T bx = 0, by = 1;
while (x) {
T k = y / x;
y %= x;
ay -= k * ax;
by -= k * bx;
std::swap(x, y);
std::swap(ax, ay);
std::swap(bx, by);
}
return {y, ay, by};
}
template <typename T> T mod_inv(T a, T m) {
a %= m;
a = a < 0 ? a + m : a;
return mod_inv_in_range(a, m);
}
template <int MOD_> struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
int v;
public:
modnum() : v(0) {}
modnum(int64_t v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modnum& n) { int64_t v_; in >> v_; n = modnum(v_); return in; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
modnum inv() const {
modnum res;
res.v = mod_inv_in_range(v, MOD);
return res;
}
friend modnum inv(const modnum& m) { return m.inv(); }
modnum neg() const {
modnum res;
res.v = v ? MOD-v : 0;
return res;
}
friend modnum neg(const modnum& m) { return m.neg(); }
modnum operator- () const {
return neg();
}
modnum operator+ () const {
return modnum(*this);
}
modnum& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modnum& operator += (const modnum& o) {
v -= MOD-o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(int64_t(v) * int64_t(o.v) % MOD);
return *this;
}
modnum& operator /= (const modnum& o) {
return *this *= o.inv();
}
friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
using num = modnum<998244353>;
template<int L> using mat = array<array<num, L>, L>;
template<int L> mat<L> mul(mat<L> a, mat<L> b){
mat<L> c;
for(int i = 0; i < L; i++) for(int j = 0; j < L; j++) c[i][j] = 0;
for(int i = 0; i < L; i++){
for(int j = 0; j < L; j++){
for(int k = 0; k < L; k++){
c[i][k] += a[i][j] * b[j][k];
}
}
}
return c;
}
template<int A2> mat<A2> op(mat<A2> a, mat<A2> b){
return mul<A2>(a, b);
}
template<int A2> mat<A2> e(){
mat<A2> r;
for(int i = 0; i < A2; i++) for(int j = 0; j < A2; j++) r[i][j] = int(i == j);
return r;
}
template<int A> struct info {
int all_msk; // always one way to use no
array<int, A> pref_mask;
array<int, A> suff_mask;
array<array<num, A>, A> ways;
};
template<int A> info<A> combine(info<A> a, info<A> b){
info<A> res;
for(int i = 0; i < A; i++) res.pref_mask[i] = 0;
for(int i = 0; i < A; i++) res.suff_mask[i] = 0;
for(int i = 0; i < A; i++) for(int j = 0; j < A; j++) res.ways[i][j] = 0;
res.all_msk = a.all_msk | b.all_msk;
for(int x = 0; x < A; x++){
if(a.all_msk & (1 << x)){
res.pref_mask[x] = a.pref_mask[x];
} else {
res.pref_mask[x] = a.all_msk | b.pref_mask[x];
for(int y = 0; y < A; y++){
res.ways[x][y] += b.ways[x][y];
}
}
if(b.all_msk & (1 << x)){
res.suff_mask[x] = b.suff_mask[x];
} else {
res.suff_mask[x] = b.all_msk | a.suff_mask[x];
for(int y = 0; y < A; y++){
res.ways[y][x] += a.ways[y][x];
}
}
}
for(int x = 0; x < A; x++){
for(int y = 0; y < A; y++){
if(a.suff_mask[x] & (1 << y)) continue;
if(b.pref_mask[y] & (1 << x)) continue;
for(int c = 0; c < A; c++){
for(int d = 0; d < A; d++){
res.ways[c][d] += a.ways[c][x] * b.ways[y][d];
}
}
}
}
return res;
}
template<int A> using seg_info = optional<info<A>>;
template<int A> seg_info<A> info_op(seg_info<A> a, seg_info<A> b){
if(!a) return b;
if(!b) return a;
return combine<A>(a.value(), b.value());
}
template<int A> seg_info<A> info_e(){
return {};
}
template<int A> void solve(string str, vector<pair<int, char> > queries){
int N = (int)str.size();
int Q = (int)queries.size();
// find a matrix
// number of subsequences that end with b:
// a, b, c -> a+b=c+1, b, c
// 1, a, b, c
// number of subsequences that appear exactly once:
// a, b, c
// all are ok except those which
// take letters: if you skipped a letter: track that
// [not a] a [not a, b] b [not b, a] a [not a, c] c [not c]
// [noa] A [noabw]
// A2 by A2
// A2^3 multiplication
vector<info<A>> letters(A);
for(int c = 0; c < A; c++){
letters[c].all_msk = (1 << c);
for(int x = 0; x < A; x++) letters[c].pref_mask[x] = 0;
for(int x = 0; x < A; x++) letters[c].suff_mask[x] = 0;
for(int x = 0; x < A; x++) {
for(int y = 0; y < A; y++){
letters[c].ways[x][y] = 0;
}
}
letters[c].ways[c][c] = 1;
}
vector<seg_info<A>> seg_init(N);
for(int i = 0; i < N; i++) seg_init[i] = letters[str[i]-'a'];
atcoder::segtree<seg_info<A>, info_op<A>, info_e<A>> seg(seg_init);
constexpr int B2 = A+1;
mat<B2> B_empty;
for(int i = 0; i < B2; i++){
for(int j = 0; j < B2; j++){
B_empty[i][j] = 0;
}
}
vector<mat<B2> > B_tr(A, B_empty);
for(int i = 0; i < A; i++){
for(int j = 0; j <= A; j++){
B_tr[i][j][j] = 1;
B_tr[i][j][i] = 1;
}
}
vector<mat<B2>> B_init(N);
for(int i = 0; i < N; i++){
B_init[i] = B_tr[str[i]-'a'];
}
atcoder::segtree<mat<B2>, op<B2>, e<B2>> B_seg(B_init);
auto get_cnt = [&]() -> num {
info<A> res = seg.all_prod().value();
num ans = 0;
for(int x = 0; x < A; x++){
for(int y = 0; y < A; y++){
ans += res.ways[x][y];
}
}
mat<B2> B_res = B_seg.all_prod();
num all_cnt = 0;
for(int i = 0; i < A; i++){
all_cnt += B_res[A][i];
}
return all_cnt - ans;
};
for(int q = 0; q <= Q; q++){
num x = get_cnt();
cout << x << '\n';
if(q < Q){
auto [idx, c] = queries[q];
seg.set(idx, letters[c-'a']);
B_seg.set(idx, B_tr[c-'a']);
}
}
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
string str;
cin >> str;
vector<pair<int, char> > queries(Q);
char maxval = 'a';
for(char c : str) maxval = max(maxval, c);
for(int q = 0; q < Q; q++){
int idx;
cin >> idx;
idx--;
char c;
cin >> c;
queries[q] = {idx, c};
maxval = max(maxval, c);
}
if(maxval == 'a' || maxval == 'b'){
solve<2>(str, queries);
} else if(maxval == 'c'){
solve<3>(str, queries);
} else if(maxval == 'd'){
solve<4>(str, queries);
} else if(maxval == 'e'){
solve<5>(str, queries);
} else {
solve<6>(str, queries);
}
}
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | #include <bits/stdc++.h> using namespace std; namespace atcoder { namespace internal { #if __cplusplus >= 202002L using std::bit_ceil; #else // @return same with std::bit::bit_ceil unsigned int bit_ceil(unsigned int n) { unsigned int x = 1; while (x < (unsigned int)(n)) x *= 2; return x; } #endif // @param n `1 <= n` // @return same with std::bit::countr_zero int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } // @param n `1 <= n` // @return same with std::bit::countr_zero constexpr int countr_zero_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } } // namespace internal } // namespace atcoder namespace atcoder { #if __cplusplus >= 201703L template <class S, auto op, auto e> struct segtree { static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>, "op must work as S(S, S)"); static_assert(std::is_convertible_v<decltype(e), std::function<S()>>, "e must work as S()"); #else template <class S, S (*op)(S, S), S (*e)()> struct segtree { #endif public: segtree() : segtree(0) {} explicit segtree(int n) : segtree(std::vector<S>(n, e())) {} explicit segtree(const std::vector<S>& v) : _n(int(v.size())) { size = (int)internal::bit_ceil((unsigned int)(_n)); log = internal::countr_zero((unsigned int)size); d = std::vector<S>(2 * size, e()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) const { assert(0 <= p && p < _n); return d[p + size]; } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= _n); S sml = e(), smr = e(); l += size; r += size; while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() const { return d[1]; } template <bool (*f)(S)> int max_right(int l) const { return max_right(l, [](S x) { return f(x); }); } template <class F> int max_right(int l, F f) const { assert(0 <= l && l <= _n); assert(f(e())); if (l == _n) return _n; l += size; S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!f(op(sm, d[l]))) { while (l < size) { l = (2 * l); if (f(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*f)(S)> int min_left(int r) const { return min_left(r, [](S x) { return f(x); }); } template <class F> int min_left(int r, F f) const { assert(0 <= r && r <= _n); assert(f(e())); if (r == 0) return 0; r += size; S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!f(op(d[r], sm))) { while (r < size) { r = (2 * r + 1); if (f(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } }; } // namespace atcoder template <typename T> T mod_inv_in_range(T a, T m) { // assert(0 <= a && a < m); T x = a, y = m; // coeff of a in x and y T vx = 1, vy = 0; while (x) { T k = y / x; y %= x; vy -= k * vx; std::swap(x, y); std::swap(vx, vy); } assert(y == 1); return vy < 0 ? m + vy : vy; } template <typename T> struct extended_gcd_result { T gcd; T coeff_a, coeff_b; }; template <typename T> extended_gcd_result<T> extended_gcd(T a, T b) { T x = a, y = b; // coeff of a and b in x and y T ax = 1, ay = 0; T bx = 0, by = 1; while (x) { T k = y / x; y %= x; ay -= k * ax; by -= k * bx; std::swap(x, y); std::swap(ax, ay); std::swap(bx, by); } return {y, ay, by}; } template <typename T> T mod_inv(T a, T m) { a %= m; a = a < 0 ? a + m : a; return mod_inv_in_range(a, m); } template <int MOD_> struct modnum { static constexpr int MOD = MOD_; static_assert(MOD_ > 0, "MOD must be positive"); private: int v; public: modnum() : v(0) {} modnum(int64_t v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } friend std::istream& operator >> (std::istream& in, modnum& n) { int64_t v_; in >> v_; n = modnum(v_); return in; } friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } modnum inv() const { modnum res; res.v = mod_inv_in_range(v, MOD); return res; } friend modnum inv(const modnum& m) { return m.inv(); } modnum neg() const { modnum res; res.v = v ? MOD-v : 0; return res; } friend modnum neg(const modnum& m) { return m.neg(); } modnum operator- () const { return neg(); } modnum operator+ () const { return modnum(*this); } modnum& operator ++ () { v ++; if (v == MOD) v = 0; return *this; } modnum& operator -- () { if (v == 0) v = MOD; v --; return *this; } modnum& operator += (const modnum& o) { v -= MOD-o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator -= (const modnum& o) { v -= o.v; v = (v < 0) ? v + MOD : v; return *this; } modnum& operator *= (const modnum& o) { v = int(int64_t(v) * int64_t(o.v) % MOD); return *this; } modnum& operator /= (const modnum& o) { return *this *= o.inv(); } friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } }; template <typename T> T pow(T a, long long b) { assert(b >= 0); T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; } using num = modnum<998244353>; template<int L> using mat = array<array<num, L>, L>; template<int L> mat<L> mul(mat<L> a, mat<L> b){ mat<L> c; for(int i = 0; i < L; i++) for(int j = 0; j < L; j++) c[i][j] = 0; for(int i = 0; i < L; i++){ for(int j = 0; j < L; j++){ for(int k = 0; k < L; k++){ c[i][k] += a[i][j] * b[j][k]; } } } return c; } template<int A2> mat<A2> op(mat<A2> a, mat<A2> b){ return mul<A2>(a, b); } template<int A2> mat<A2> e(){ mat<A2> r; for(int i = 0; i < A2; i++) for(int j = 0; j < A2; j++) r[i][j] = int(i == j); return r; } template<int A> struct info { int all_msk; // always one way to use no array<int, A> pref_mask; array<int, A> suff_mask; array<array<num, A>, A> ways; }; template<int A> info<A> combine(info<A> a, info<A> b){ info<A> res; for(int i = 0; i < A; i++) res.pref_mask[i] = 0; for(int i = 0; i < A; i++) res.suff_mask[i] = 0; for(int i = 0; i < A; i++) for(int j = 0; j < A; j++) res.ways[i][j] = 0; res.all_msk = a.all_msk | b.all_msk; for(int x = 0; x < A; x++){ if(a.all_msk & (1 << x)){ res.pref_mask[x] = a.pref_mask[x]; } else { res.pref_mask[x] = a.all_msk | b.pref_mask[x]; for(int y = 0; y < A; y++){ res.ways[x][y] += b.ways[x][y]; } } if(b.all_msk & (1 << x)){ res.suff_mask[x] = b.suff_mask[x]; } else { res.suff_mask[x] = b.all_msk | a.suff_mask[x]; for(int y = 0; y < A; y++){ res.ways[y][x] += a.ways[y][x]; } } } for(int x = 0; x < A; x++){ for(int y = 0; y < A; y++){ if(a.suff_mask[x] & (1 << y)) continue; if(b.pref_mask[y] & (1 << x)) continue; for(int c = 0; c < A; c++){ for(int d = 0; d < A; d++){ res.ways[c][d] += a.ways[c][x] * b.ways[y][d]; } } } } return res; } template<int A> using seg_info = optional<info<A>>; template<int A> seg_info<A> info_op(seg_info<A> a, seg_info<A> b){ if(!a) return b; if(!b) return a; return combine<A>(a.value(), b.value()); } template<int A> seg_info<A> info_e(){ return {}; } template<int A> void solve(string str, vector<pair<int, char> > queries){ int N = (int)str.size(); int Q = (int)queries.size(); // find a matrix // number of subsequences that end with b: // a, b, c -> a+b=c+1, b, c // 1, a, b, c // number of subsequences that appear exactly once: // a, b, c // all are ok except those which // take letters: if you skipped a letter: track that // [not a] a [not a, b] b [not b, a] a [not a, c] c [not c] // [noa] A [noabw] // A2 by A2 // A2^3 multiplication vector<info<A>> letters(A); for(int c = 0; c < A; c++){ letters[c].all_msk = (1 << c); for(int x = 0; x < A; x++) letters[c].pref_mask[x] = 0; for(int x = 0; x < A; x++) letters[c].suff_mask[x] = 0; for(int x = 0; x < A; x++) { for(int y = 0; y < A; y++){ letters[c].ways[x][y] = 0; } } letters[c].ways[c][c] = 1; } vector<seg_info<A>> seg_init(N); for(int i = 0; i < N; i++) seg_init[i] = letters[str[i]-'a']; atcoder::segtree<seg_info<A>, info_op<A>, info_e<A>> seg(seg_init); constexpr int B2 = A+1; mat<B2> B_empty; for(int i = 0; i < B2; i++){ for(int j = 0; j < B2; j++){ B_empty[i][j] = 0; } } vector<mat<B2> > B_tr(A, B_empty); for(int i = 0; i < A; i++){ for(int j = 0; j <= A; j++){ B_tr[i][j][j] = 1; B_tr[i][j][i] = 1; } } vector<mat<B2>> B_init(N); for(int i = 0; i < N; i++){ B_init[i] = B_tr[str[i]-'a']; } atcoder::segtree<mat<B2>, op<B2>, e<B2>> B_seg(B_init); auto get_cnt = [&]() -> num { info<A> res = seg.all_prod().value(); num ans = 0; for(int x = 0; x < A; x++){ for(int y = 0; y < A; y++){ ans += res.ways[x][y]; } } mat<B2> B_res = B_seg.all_prod(); num all_cnt = 0; for(int i = 0; i < A; i++){ all_cnt += B_res[A][i]; } return all_cnt - ans; }; for(int q = 0; q <= Q; q++){ num x = get_cnt(); cout << x << '\n'; if(q < Q){ auto [idx, c] = queries[q]; seg.set(idx, letters[c-'a']); B_seg.set(idx, B_tr[c-'a']); } } } int main(){ ios_base::sync_with_stdio(false), cin.tie(nullptr); int N, Q; cin >> N >> Q; string str; cin >> str; vector<pair<int, char> > queries(Q); char maxval = 'a'; for(char c : str) maxval = max(maxval, c); for(int q = 0; q < Q; q++){ int idx; cin >> idx; idx--; char c; cin >> c; queries[q] = {idx, c}; maxval = max(maxval, c); } if(maxval == 'a' || maxval == 'b'){ solve<2>(str, queries); } else if(maxval == 'c'){ solve<3>(str, queries); } else if(maxval == 'd'){ solve<4>(str, queries); } else if(maxval == 'e'){ solve<5>(str, queries); } else { solve<6>(str, queries); } } |
English