#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m)>>(i) & 1)
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); }
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); }
const int maxN = 1 << 17, mod = 998'244'353;
struct Uniqs
{
static constexpr int S = 6;
int visMask, left[S], right[S];
long long M[S][S];
Uniqs()
{
FOR(i, 0, S)
{
std::fill(M[i], M[i] + S, 0);
left[i] = right[i] = 1;
}
}
void setup(int ind)
{
FOR(i, 0, S)
M[i][i] = i == ind;
visMask = 1 << ind;
}
void merge(const Uniqs& A, const Uniqs& B)
{
FOR(i, 0, S) FOR(j, 0, S)
{
M[i][j] = 0;
if (not ithBit(A.visMask, i) or not ithBit(B.visMask, j))
M[i][j] = (A.M[i][j] + B.M[i][j]) % mod;
else FOR(a, 0, S)
{
FOR(b, 0, S)
if (A.right[a] <= A.right[b] and B.left[b] <= B.left[a])
M[i][j] += A.M[i][a] * B.M[b][j];
M[i][j] %= mod;
}
}
FOR(i, 0, S)
{
left[i] = A.left[i] + (!ithBit(A.visMask, i) * B.left[i]);
right[i] = B.right[i] + (!ithBit(B.visMask, i) * A.right[i]);
}
visMask = A.visMask | B.visMask;
}
long long eval()
{
long long ret = 1;
FOR(i, 0, S)
{
FOR(j, 0, S)
ret += M[i][j];
ret %= mod;
}
return ret;
}
};
struct Alls
{
static constexpr int S = 7;
long long M[S][S];
Alls()
{
FOR(i, 0, S) FOR(j, 0, S)
M[i][j] = i == j;
}
void setup(int ind)
{
FOR(i, 0, S) FOR(j, 0, S)
M[i][j] = i == j or j == ind;
}
void merge(const Alls& A, const Alls& B)
{
FOR(i, 0, S) FOR(j, 0, S)
{
M[i][j] = 0;
FOR(k, 0, S)
M[i][j] += A.M[i][k] * B.M[k][j];
M[i][j] %= mod;
}
}
long long eval()
{
long long ret = 0;
FOR(j, 0, S)
ret += M[S-1][j];
return ret % mod;
}
};
struct Tree
{
int offset;
Uniqs U[maxN];
Alls A[maxN];
void init(char* T, int n)
{
for (offset = 1; offset < n; offset *= 2) ;
FOR(i, 0, n)
{
U[offset + i].setup(T[i] - 'a');
A[offset + i].setup(T[i] - 'a');
}
FORD(i, offset-1, 0)
{
U[i].merge(U[i * 2], U[i*2+1]);
A[i].merge(A[i*2+1], A[i * 2]);
}
}
void update(int v, char x)
{
v += offset;
U[v].setup(x - 'a');
A[v].setup(x - 'a');
for (v /= 2; v != 0; v /= 2)
{
U[v].merge(U[v * 2], U[v*2+1]);
A[v].merge(A[v*2+1], A[v * 2]);
}
}
long long eval()
{
return (A[1].eval() - U[1].eval() + mod) % mod;
}
} tree;
char T[maxN];
void solve()
{
int n, q;
scanf ("%d%d%s", &n, &q, T);
tree.init(T, n);
printf("%lld\n", tree.eval());
while (q--)
{
int i;
char x;
scanf ("%d %c", &i, &x);
tree.update(--i, x);
printf("%lld\n", tree.eval());
}
}
int main()
{
int tc = 1;
// scanf ("%d", &tc);
FOR(cid, 1, tc+1)
solve();
return 0;
}
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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (auto i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m)>>(i) & 1) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); } const int maxN = 1 << 17, mod = 998'244'353; struct Uniqs { static constexpr int S = 6; int visMask, left[S], right[S]; long long M[S][S]; Uniqs() { FOR(i, 0, S) { std::fill(M[i], M[i] + S, 0); left[i] = right[i] = 1; } } void setup(int ind) { FOR(i, 0, S) M[i][i] = i == ind; visMask = 1 << ind; } void merge(const Uniqs& A, const Uniqs& B) { FOR(i, 0, S) FOR(j, 0, S) { M[i][j] = 0; if (not ithBit(A.visMask, i) or not ithBit(B.visMask, j)) M[i][j] = (A.M[i][j] + B.M[i][j]) % mod; else FOR(a, 0, S) { FOR(b, 0, S) if (A.right[a] <= A.right[b] and B.left[b] <= B.left[a]) M[i][j] += A.M[i][a] * B.M[b][j]; M[i][j] %= mod; } } FOR(i, 0, S) { left[i] = A.left[i] + (!ithBit(A.visMask, i) * B.left[i]); right[i] = B.right[i] + (!ithBit(B.visMask, i) * A.right[i]); } visMask = A.visMask | B.visMask; } long long eval() { long long ret = 1; FOR(i, 0, S) { FOR(j, 0, S) ret += M[i][j]; ret %= mod; } return ret; } }; struct Alls { static constexpr int S = 7; long long M[S][S]; Alls() { FOR(i, 0, S) FOR(j, 0, S) M[i][j] = i == j; } void setup(int ind) { FOR(i, 0, S) FOR(j, 0, S) M[i][j] = i == j or j == ind; } void merge(const Alls& A, const Alls& B) { FOR(i, 0, S) FOR(j, 0, S) { M[i][j] = 0; FOR(k, 0, S) M[i][j] += A.M[i][k] * B.M[k][j]; M[i][j] %= mod; } } long long eval() { long long ret = 0; FOR(j, 0, S) ret += M[S-1][j]; return ret % mod; } }; struct Tree { int offset; Uniqs U[maxN]; Alls A[maxN]; void init(char* T, int n) { for (offset = 1; offset < n; offset *= 2) ; FOR(i, 0, n) { U[offset + i].setup(T[i] - 'a'); A[offset + i].setup(T[i] - 'a'); } FORD(i, offset-1, 0) { U[i].merge(U[i * 2], U[i*2+1]); A[i].merge(A[i*2+1], A[i * 2]); } } void update(int v, char x) { v += offset; U[v].setup(x - 'a'); A[v].setup(x - 'a'); for (v /= 2; v != 0; v /= 2) { U[v].merge(U[v * 2], U[v*2+1]); A[v].merge(A[v*2+1], A[v * 2]); } } long long eval() { return (A[1].eval() - U[1].eval() + mod) % mod; } } tree; char T[maxN]; void solve() { int n, q; scanf ("%d%d%s", &n, &q, T); tree.init(T, n); printf("%lld\n", tree.eval()); while (q--) { int i; char x; scanf ("%d %c", &i, &x); tree.update(--i, x); printf("%lld\n", tree.eval()); } } int main() { int tc = 1; // scanf ("%d", &tc); FOR(cid, 1, tc+1) solve(); return 0; } |
English