// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); }
template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); }
#ifdef LOCAL
const bool debug = true;
#else
const bool debug = false;
#define cerr if (true) {} else cout
#endif
// clang-format on
#define int long long
#define double long double
const int INF = 1e18;
const int mod = 998244353;
const int BASE = 1 << 16;
const int N_CHARS = 6;
const int DIM = N_CHARS + 1; // {T, +1}
int iden[DIM][DIM], zeros[DIM][DIM];
set<int> posistions[N_CHARS];
string s;
int tree[2 * BASE][2][DIM][DIM];
void print_matrix(int m[][DIM], bool only_first = false) {
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++)
cout << m[i][j] << ' ';
cout << '\n';
if (only_first)
break;
}
cout << '\n';
}
inline void matmul(int res[][DIM], int a[][DIM], int b[][DIM]) {
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
res[i][j] = 0;
for (int i = 0; i < DIM; i++)
for (int k = 0; k < DIM; k++)
for (int j = 0; j < DIM; j++)
res[i][j] += a[i][k] * b[k][j];
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
res[i][j] = res[i][j] % mod;
}
inline void assign(int res[][DIM], int val[][DIM]) {
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
res[i][j] = val[i][j];
}
void set_tree_val(int pos, int val[][DIM], int val2[][DIM]) {
pos += BASE;
assign(tree[pos][0], val);
assign(tree[pos][1], val2);
while (pos /= 2) {
matmul(tree[pos][0], tree[pos * 2][0], tree[pos * 2 + 1][0]);
matmul(tree[pos][1], tree[pos * 2][1], tree[pos * 2 + 1][1]);
}
}
void init() {
for (int i = 0; i < DIM; i++) {
iden[i][i] = 1;
}
for (int i = 0; i < 2 * BASE; i++) {
assign(tree[i][0], iden);
assign(tree[i][1], iden);
}
for (int i = 0; i < N_CHARS; i++) {
posistions[i].insert(-1);
}
int m[DIM][DIM];
assign(m, zeros);
m[0][N_CHARS] = 1;
set_tree_val(0, m, m);
}
void uddate_single_matrix(int pos) {
int c = s[pos] - 'a';
int m[2][DIM][DIM];
assign(m[0], iden);
assign(m[1], iden);
int prev_pos = *(--posistions[c].find(pos));
for (int i = 0; i < N_CHARS; i++) {
int i_pos = *(--posistions[i].lower_bound(pos));
if (prev_pos < i_pos) {
m[0][i][c] += 1; // t^c += t^i
}
}
if (prev_pos == -1) { // first occurance
m[0][N_CHARS][c] += 1; // +1
}
// T
m[1][N_CHARS][N_CHARS] = 2;
m[1][c][N_CHARS] = mod-1;
// Tc
m[1][N_CHARS][c] = 1;
m[1][c][c] = 0;
set_tree_val(pos, m[0], m[1]);
}
void update(int pos, char _c) {
int prev_c = s[pos] - 'a';
int c = _c - 'a';
if (prev_c == c)
return;
s[pos] = _c;
posistions[prev_c].erase(pos);
posistions[c].insert(pos);
uddate_single_matrix(pos);
for (int i = 0; i < N_CHARS; i++) {
auto it = posistions[i].upper_bound(pos);
if (it != posistions[i].end())
uddate_single_matrix(*it);
}
}
void print_result() {
int res = tree[1][1][0][N_CHARS], res1 = 0;
for (int i = 0; i < N_CHARS; i++) {
res1 += tree[1][0][0][i];
}
// cout << "result: ";
cout << (res - res1 - 1 + mod * mod) % mod;
// cout << " " << res << " " << res1;
cout << "\n";
}
void brut(int n) {
map<string, int> mapa;
for (int _mask = 1; _mask < (1 << n); _mask++) {
int mask = _mask << 1;
string xd;
for (int i = 1; i <= n; i++)
if ((1 << i) & mask)
xd.push_back(s[i]);
mapa[xd]++;
}
// res uwzględnia pusty podciąg
int res = 1, res1 = 0;
for (auto &[s, c] : mapa) {
if (c == 1) {
res1++;
}
res++;
}
cout << "brut: " << res - res1 - 1 << " " << res << " " << res1 << "\n";
for (int i = 0; i < N_CHARS; i++) {
int res = 0;
for (auto &[xd, c] : mapa)
if (xd[xd.size() - 1] - 'a' == i && c == 1)
res++;
cout << res << " ";
}
cout << "\n";
}
void brut2(int n) {
vector<int> t(BASE), T(BASE);
t[0] = 1;
T[0] = 1;
set<int> pos[10];
for (int i = 0; i < 10; i++)
pos[i].insert(0);
for (int i = 1; i <= n; i++) {
int c = s[i] - 'a';
int last_pos = *(--pos[c].insert(i).first);
T[i] = 2 * T[i - 1];
if (last_pos > 0)
T[i] += mod - T[last_pos - 1];
t[i] = t[last_pos];
for (int y = 0; y < N_CHARS; y++) {
int pos_y = *(--pos[y].lower_bound(i));
if (last_pos < pos_y)
t[i] += t[pos_y];
}
T[i] %= mod;
t[i] %= mod;
cout << T[i] << "," << t[i] << " ";
}
int res = 0;
for (int i = 0; i < N_CHARS; i++) {
int p = *pos[i].rbegin();
if (p != 0)
res += t[p];
}
cout << "brut2: " << T[n] - res - 1 << " " << T[n] << " " << res << "\n";
}
void dummy() {
s = "#";
int n = 50000;
for (int i = 1; i <= n; i++) {
s += rand() % N_CHARS + 'a';
int c = s[i] - 'a';
posistions[c].insert(i);
uddate_single_matrix(i);
}
print_result();
for (int i = 0; i < n; i++) {
int p = rand() % n + 1;
char c;
do {
c = rand() % N_CHARS + 'a';
} while (c == s[p]);
update(p, c);
print_result();
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
init();
int n, q;
cin >> n >> q >> s;
if (n == -1)
dummy();
s = "#" + s;
for (int i = 1; i <= n; i++) {
int c = s[i] - 'a';
posistions[c].insert(i);
uddate_single_matrix(i);
}
print_result();
// brut(n);
// print_matrix(tree[1][0], true);
// brut2(n);
// print_matrix(tree[1][1], true);
// int m[DIM][DIM], m2[DIM][DIM];
// assign(m, iden);
// for (int i = 0; i <= n; i++) {
// matmul(m2, m, tree[i + BASE][1]);
// swap(m2, m);
// cout << s[i] << ":\n";
// // print_matrix(tree[i + BASE]);
// brut2(i);
// print_matrix(m, true);
// }
// cout << tree[1][0][N_CHARS] << "\n";
while (q--) {
int p;
char c;
cin >> p >> c;
update(p, c);
print_result();
// brut(n);
// print_matrix(tree[1][0], true);
// brut2(n);
}
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 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 | // clang-format off #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); } template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif // clang-format on #define int long long #define double long double const int INF = 1e18; const int mod = 998244353; const int BASE = 1 << 16; const int N_CHARS = 6; const int DIM = N_CHARS + 1; // {T, +1} int iden[DIM][DIM], zeros[DIM][DIM]; set<int> posistions[N_CHARS]; string s; int tree[2 * BASE][2][DIM][DIM]; void print_matrix(int m[][DIM], bool only_first = false) { for (int i = 0; i < DIM; i++) { for (int j = 0; j < DIM; j++) cout << m[i][j] << ' '; cout << '\n'; if (only_first) break; } cout << '\n'; } inline void matmul(int res[][DIM], int a[][DIM], int b[][DIM]) { for (int i = 0; i < DIM; i++) for (int j = 0; j < DIM; j++) res[i][j] = 0; for (int i = 0; i < DIM; i++) for (int k = 0; k < DIM; k++) for (int j = 0; j < DIM; j++) res[i][j] += a[i][k] * b[k][j]; for (int i = 0; i < DIM; i++) for (int j = 0; j < DIM; j++) res[i][j] = res[i][j] % mod; } inline void assign(int res[][DIM], int val[][DIM]) { for (int i = 0; i < DIM; i++) for (int j = 0; j < DIM; j++) res[i][j] = val[i][j]; } void set_tree_val(int pos, int val[][DIM], int val2[][DIM]) { pos += BASE; assign(tree[pos][0], val); assign(tree[pos][1], val2); while (pos /= 2) { matmul(tree[pos][0], tree[pos * 2][0], tree[pos * 2 + 1][0]); matmul(tree[pos][1], tree[pos * 2][1], tree[pos * 2 + 1][1]); } } void init() { for (int i = 0; i < DIM; i++) { iden[i][i] = 1; } for (int i = 0; i < 2 * BASE; i++) { assign(tree[i][0], iden); assign(tree[i][1], iden); } for (int i = 0; i < N_CHARS; i++) { posistions[i].insert(-1); } int m[DIM][DIM]; assign(m, zeros); m[0][N_CHARS] = 1; set_tree_val(0, m, m); } void uddate_single_matrix(int pos) { int c = s[pos] - 'a'; int m[2][DIM][DIM]; assign(m[0], iden); assign(m[1], iden); int prev_pos = *(--posistions[c].find(pos)); for (int i = 0; i < N_CHARS; i++) { int i_pos = *(--posistions[i].lower_bound(pos)); if (prev_pos < i_pos) { m[0][i][c] += 1; // t^c += t^i } } if (prev_pos == -1) { // first occurance m[0][N_CHARS][c] += 1; // +1 } // T m[1][N_CHARS][N_CHARS] = 2; m[1][c][N_CHARS] = mod-1; // Tc m[1][N_CHARS][c] = 1; m[1][c][c] = 0; set_tree_val(pos, m[0], m[1]); } void update(int pos, char _c) { int prev_c = s[pos] - 'a'; int c = _c - 'a'; if (prev_c == c) return; s[pos] = _c; posistions[prev_c].erase(pos); posistions[c].insert(pos); uddate_single_matrix(pos); for (int i = 0; i < N_CHARS; i++) { auto it = posistions[i].upper_bound(pos); if (it != posistions[i].end()) uddate_single_matrix(*it); } } void print_result() { int res = tree[1][1][0][N_CHARS], res1 = 0; for (int i = 0; i < N_CHARS; i++) { res1 += tree[1][0][0][i]; } // cout << "result: "; cout << (res - res1 - 1 + mod * mod) % mod; // cout << " " << res << " " << res1; cout << "\n"; } void brut(int n) { map<string, int> mapa; for (int _mask = 1; _mask < (1 << n); _mask++) { int mask = _mask << 1; string xd; for (int i = 1; i <= n; i++) if ((1 << i) & mask) xd.push_back(s[i]); mapa[xd]++; } // res uwzględnia pusty podciąg int res = 1, res1 = 0; for (auto &[s, c] : mapa) { if (c == 1) { res1++; } res++; } cout << "brut: " << res - res1 - 1 << " " << res << " " << res1 << "\n"; for (int i = 0; i < N_CHARS; i++) { int res = 0; for (auto &[xd, c] : mapa) if (xd[xd.size() - 1] - 'a' == i && c == 1) res++; cout << res << " "; } cout << "\n"; } void brut2(int n) { vector<int> t(BASE), T(BASE); t[0] = 1; T[0] = 1; set<int> pos[10]; for (int i = 0; i < 10; i++) pos[i].insert(0); for (int i = 1; i <= n; i++) { int c = s[i] - 'a'; int last_pos = *(--pos[c].insert(i).first); T[i] = 2 * T[i - 1]; if (last_pos > 0) T[i] += mod - T[last_pos - 1]; t[i] = t[last_pos]; for (int y = 0; y < N_CHARS; y++) { int pos_y = *(--pos[y].lower_bound(i)); if (last_pos < pos_y) t[i] += t[pos_y]; } T[i] %= mod; t[i] %= mod; cout << T[i] << "," << t[i] << " "; } int res = 0; for (int i = 0; i < N_CHARS; i++) { int p = *pos[i].rbegin(); if (p != 0) res += t[p]; } cout << "brut2: " << T[n] - res - 1 << " " << T[n] << " " << res << "\n"; } void dummy() { s = "#"; int n = 50000; for (int i = 1; i <= n; i++) { s += rand() % N_CHARS + 'a'; int c = s[i] - 'a'; posistions[c].insert(i); uddate_single_matrix(i); } print_result(); for (int i = 0; i < n; i++) { int p = rand() % n + 1; char c; do { c = rand() % N_CHARS + 'a'; } while (c == s[p]); update(p, c); print_result(); } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); init(); int n, q; cin >> n >> q >> s; if (n == -1) dummy(); s = "#" + s; for (int i = 1; i <= n; i++) { int c = s[i] - 'a'; posistions[c].insert(i); uddate_single_matrix(i); } print_result(); // brut(n); // print_matrix(tree[1][0], true); // brut2(n); // print_matrix(tree[1][1], true); // int m[DIM][DIM], m2[DIM][DIM]; // assign(m, iden); // for (int i = 0; i <= n; i++) { // matmul(m2, m, tree[i + BASE][1]); // swap(m2, m); // cout << s[i] << ":\n"; // // print_matrix(tree[i + BASE]); // brut2(i); // print_matrix(m, true); // } // cout << tree[1][0][N_CHARS] << "\n"; while (q--) { int p; char c; cin >> p >> c; update(p, c); print_result(); // brut(n); // print_matrix(tree[1][0], true); // brut2(n); } return 0; } /* */ |
English