#include "bits/stdc++.h"
using namespace std;
#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define per(i, b, e) for (int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e)-1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
auto& operator<<(auto& o, pair<auto, auto> p) {
return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto& o, auto x) -> decltype(end(x), o) {
o << "{";
int i = 0;
for (auto e : x)
o << ", " + 2 * !i++ << e;
return o << "}";
}
#ifdef LOCAL
#define deb(x...) \
cerr << "[" #x "]: ", [](auto... $) { \
((cerr << $ << "; "), ...) << endl; \
}(x)
#else
#define deb(...)
#endif
const ll mod = 998244353;
struct Matrix {
int m[7][7] = {};
int sz;
Matrix() = default;
Matrix(int n) : sz(n) {}
Matrix operator*(const Matrix& other) const {
Matrix n(sz);
// assert(SZ(other.m) == SZ(m));
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
ll sum = 0;
for (int k = 0; k < sz; k++) {
sum += ((ll)m[i][k] * other.m[k][j]) % mod;
}
n.m[i][j] = sum % mod;
}
}
return n;
}
bool operator==(const Matrix& other) const {
for (int i = 0; i < sz; i++)
for (int j = 0; j < sz; j++)
if (m[i][j] != other.m[i][j])
return false;
return true;
}
void dump() {
#ifdef LOCAL
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
cerr << m[i][j] << " ";
}
cerr << "\n";
}
#endif
}
};
const int N = 50003;
string s;
int n;
const int ALF_SIZE = 6;
const int M = (1 << 16);
Matrix tree_end[M * 2];
Matrix tree_all[M * 2];
set<int> where[200];
Matrix create_all(char k) {
Matrix m(ALF_SIZE + 1);
for (int i = 0; i < ALF_SIZE + 1; i++) {
m.m[i][i] = 1;
m.m[k - 'a'][i] = 1;
}
return m;
}
int previous_occurance(char k, int i) {
if (where[k].size() == 0) {
return -1;
}
auto it = where[k].lower_bound(i);
if (it == where[k].begin()) {
return -1;
}
--it;
return *it;
}
int next_occurance(char k, int i) {
if (where[k].size() == 0) {
return n;
}
auto it = where[k].upper_bound(i);
if (it == where[k].end())
return n;
return *it;
}
Matrix create_end(char k, int idx) {
Matrix m(ALF_SIZE);
for (int i = 0; i < ALF_SIZE; i++) {
m.m[i][i] = 1;
}
int p = previous_occurance(k, idx);
for (char c = 'a'; c <= 'f'; c++) {
if (c != k && previous_occurance(c, idx) > p) {
m.m[k - 'a'][c - 'a'] = 1;
}
}
return m;
}
Matrix id(int k) {
Matrix res(k);
for (int i = 0; i < k; i++)
res.m[i][i] = 1;
return res;
}
void init() {
for (int i = 0; i < n; i++) {
where[s[i]].insert(i);
tree_all[i + M] = create_all(s[i]);
tree_end[i + M] = create_end(s[i], i);
// cerr << "Macierze dla " << i << "\n";
tree_all[i + M].dump();
// cerr << "end:\n";
tree_end[i + M].dump();
}
for (int i = n; i < M; i++) {
tree_all[i + M] = id(ALF_SIZE + 1);
tree_end[i + M] = id(ALF_SIZE);
}
for (int i = M - 1; i >= 1; i--) {
tree_all[i] = tree_all[i * 2 + 1] * tree_all[i * 2];
tree_end[i] = tree_end[i * 2 + 1] * tree_end[i * 2];
}
// cerr << "Wszystkie macierze:\n";
// for (int i = 1; i < n + n; i++) {
// cerr << i << ":\n";
// tree_all[i].dump();
// }
}
int get_ans() {
ll ans = 0;
for (int i = 0; i < ALF_SIZE + 1; i++) {
ans += tree_all[1].m[i][ALF_SIZE];
}
for (int i = 0; i < ALF_SIZE; i++) {
if (where[i + 'a'].size() > 0) {
for (int j = 0; j < ALF_SIZE; j++) {
ans = (ans - tree_end[1].m[i][j] + mod) % mod;
}
}
}
return (ans - 1) % mod;
}
bool set_(const Matrix& m, int idx, Matrix* tree) {
// cerr << "Chcom zebym zmienil " << idx << "\n";
idx += M;
if (tree[idx] == m)
return false;
tree[idx] = m;
return true;
}
void upd(const Matrix& m, int idx, Matrix* tree) {
idx += M;
if (tree[idx] == m)
return;
tree[idx] = m;
while (idx > 1) {
idx /= 2;
tree[idx] = tree[idx * 2 + 1] * tree[idx * 2];
}
}
void apply_changes(Matrix* tree, const vi& idx) {
priority_queue<int> wher;
// cerr << "Applying changes\n";
for (auto i : idx) {
// cerr << "Pushing " << i << "\n";
wher.push((i + M) / 2);
}
while (!wher.empty()) {
int i = wher.top();
wher.pop();
// cerr << "Mnoze " << i << "\n";
tree[i] = tree[i * 2 + 1] * tree[i * 2];
if (i > 1) {
wher.push(i / 2);
}
}
}
void update(int idx, char k) {
char prev_c = s[idx];
if (prev_c == k)
return;
s[idx] = k;
where[k].insert(idx);
where[prev_c].erase(idx);
vi changes;
// 1. Najpierw zmien idx-ta macierz
upd(create_all(k), idx, tree_all);
if (set_(create_end(k, idx), idx, tree_end)) {
changes.push_back(idx);
}
// Matrix m_all = create_all(k);
// Matrix m_end = create_end(k, idx);
for (int i = 0; i < 6; i++) {
char c = i + 'a';
int p = next_occurance(c, idx);
if (p != n) {
if (set_(create_end(c, p), p, tree_end)) {
changes.push_back(p);
}
}
}
apply_changes(tree_end, changes);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
int q;
cin >> n >> q;
cin >> s;
init();
// tree_all[1].dump();
// tree_end[1].dump();
cout << get_ans() << "\n";
for (int i = 0; i < q; i++) {
int k;
char c;
cin >> k >> c;
update(k - 1, c);
// tree_all[1].dump();
// tree_end[1].dump();
cout << get_ans() << "\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 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 | #include "bits/stdc++.h" using namespace std; #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define per(i, b, e) for (int i = (e); i >= (b); i--) #define FOR(i, b, e) rep(i, b, (e)-1) #define SZ(x) int(x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using vi = vector<int>; using pii = pair<int, int>; auto& operator<<(auto& o, pair<auto, auto> p) { return o << "(" << p.st << ", " << p.nd << ")"; } auto operator<<(auto& o, auto x) -> decltype(end(x), o) { o << "{"; int i = 0; for (auto e : x) o << ", " + 2 * !i++ << e; return o << "}"; } #ifdef LOCAL #define deb(x...) \ cerr << "[" #x "]: ", [](auto... $) { \ ((cerr << $ << "; "), ...) << endl; \ }(x) #else #define deb(...) #endif const ll mod = 998244353; struct Matrix { int m[7][7] = {}; int sz; Matrix() = default; Matrix(int n) : sz(n) {} Matrix operator*(const Matrix& other) const { Matrix n(sz); // assert(SZ(other.m) == SZ(m)); for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { ll sum = 0; for (int k = 0; k < sz; k++) { sum += ((ll)m[i][k] * other.m[k][j]) % mod; } n.m[i][j] = sum % mod; } } return n; } bool operator==(const Matrix& other) const { for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) if (m[i][j] != other.m[i][j]) return false; return true; } void dump() { #ifdef LOCAL for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { cerr << m[i][j] << " "; } cerr << "\n"; } #endif } }; const int N = 50003; string s; int n; const int ALF_SIZE = 6; const int M = (1 << 16); Matrix tree_end[M * 2]; Matrix tree_all[M * 2]; set<int> where[200]; Matrix create_all(char k) { Matrix m(ALF_SIZE + 1); for (int i = 0; i < ALF_SIZE + 1; i++) { m.m[i][i] = 1; m.m[k - 'a'][i] = 1; } return m; } int previous_occurance(char k, int i) { if (where[k].size() == 0) { return -1; } auto it = where[k].lower_bound(i); if (it == where[k].begin()) { return -1; } --it; return *it; } int next_occurance(char k, int i) { if (where[k].size() == 0) { return n; } auto it = where[k].upper_bound(i); if (it == where[k].end()) return n; return *it; } Matrix create_end(char k, int idx) { Matrix m(ALF_SIZE); for (int i = 0; i < ALF_SIZE; i++) { m.m[i][i] = 1; } int p = previous_occurance(k, idx); for (char c = 'a'; c <= 'f'; c++) { if (c != k && previous_occurance(c, idx) > p) { m.m[k - 'a'][c - 'a'] = 1; } } return m; } Matrix id(int k) { Matrix res(k); for (int i = 0; i < k; i++) res.m[i][i] = 1; return res; } void init() { for (int i = 0; i < n; i++) { where[s[i]].insert(i); tree_all[i + M] = create_all(s[i]); tree_end[i + M] = create_end(s[i], i); // cerr << "Macierze dla " << i << "\n"; tree_all[i + M].dump(); // cerr << "end:\n"; tree_end[i + M].dump(); } for (int i = n; i < M; i++) { tree_all[i + M] = id(ALF_SIZE + 1); tree_end[i + M] = id(ALF_SIZE); } for (int i = M - 1; i >= 1; i--) { tree_all[i] = tree_all[i * 2 + 1] * tree_all[i * 2]; tree_end[i] = tree_end[i * 2 + 1] * tree_end[i * 2]; } // cerr << "Wszystkie macierze:\n"; // for (int i = 1; i < n + n; i++) { // cerr << i << ":\n"; // tree_all[i].dump(); // } } int get_ans() { ll ans = 0; for (int i = 0; i < ALF_SIZE + 1; i++) { ans += tree_all[1].m[i][ALF_SIZE]; } for (int i = 0; i < ALF_SIZE; i++) { if (where[i + 'a'].size() > 0) { for (int j = 0; j < ALF_SIZE; j++) { ans = (ans - tree_end[1].m[i][j] + mod) % mod; } } } return (ans - 1) % mod; } bool set_(const Matrix& m, int idx, Matrix* tree) { // cerr << "Chcom zebym zmienil " << idx << "\n"; idx += M; if (tree[idx] == m) return false; tree[idx] = m; return true; } void upd(const Matrix& m, int idx, Matrix* tree) { idx += M; if (tree[idx] == m) return; tree[idx] = m; while (idx > 1) { idx /= 2; tree[idx] = tree[idx * 2 + 1] * tree[idx * 2]; } } void apply_changes(Matrix* tree, const vi& idx) { priority_queue<int> wher; // cerr << "Applying changes\n"; for (auto i : idx) { // cerr << "Pushing " << i << "\n"; wher.push((i + M) / 2); } while (!wher.empty()) { int i = wher.top(); wher.pop(); // cerr << "Mnoze " << i << "\n"; tree[i] = tree[i * 2 + 1] * tree[i * 2]; if (i > 1) { wher.push(i / 2); } } } void update(int idx, char k) { char prev_c = s[idx]; if (prev_c == k) return; s[idx] = k; where[k].insert(idx); where[prev_c].erase(idx); vi changes; // 1. Najpierw zmien idx-ta macierz upd(create_all(k), idx, tree_all); if (set_(create_end(k, idx), idx, tree_end)) { changes.push_back(idx); } // Matrix m_all = create_all(k); // Matrix m_end = create_end(k, idx); for (int i = 0; i < 6; i++) { char c = i + 'a'; int p = next_occurance(c, idx); if (p != n) { if (set_(create_end(c, p), p, tree_end)) { changes.push_back(p); } } } apply_changes(tree_end, changes); } int main() { ios_base::sync_with_stdio(false); cin.tie(); int q; cin >> n >> q; cin >> s; init(); // tree_all[1].dump(); // tree_end[1].dump(); cout << get_ans() << "\n"; for (int i = 0; i < q; i++) { int k; char c; cin >> k >> c; update(k - 1, c); // tree_all[1].dump(); // tree_end[1].dump(); cout << get_ans() << "\n"; } } |
English