#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 5e4 + 5, M = 7, mod = 998244353; int ms = 7; set<int> s[M]; using Matrix = vector<vector<int>>; Matrix makeId(){ Matrix x(ms, vector<int>(ms)); for(int i=0; i<ms; i++) x[i][i] = 1; return x; } Matrix Id; vector<int> base; // ostream& operator<<(ostream& os, const Matrix& matrix) { // for (const auto& row : matrix) { // os << "["; // for (size_t i = 0; i < row.size(); ++i) { // os << row[i]; // if (i < row.size() - 1) { // os << ", "; // } // } // os << "]" << endl; // } // return os; // } inline int add(int a, int b){ a += b; if(a >= mod) a -= mod; return a; } inline int sub(int a, int b){ return (a-b+mod) %mod; } // int cnt = 0; inline int mul(int a, int b){ // cnt++; return (ll)a*b %mod; } Matrix operator*(const Matrix& a, const Matrix& b) { if(a.empty() || b.empty()) return {{}}; Matrix c(ms, vector<int>(ms)); c[0][0] = 1; for (int i = 1; i < ms; i++) { for (int j = 0; j < ms; j++) { for (int k = 0; k < ms; k++) { c[i][j] = add(c[i][j], mul(a[i][k], b[k][j])); } } } return c; } vector<int> operator*(const Matrix& a, const vector<int>& b) { vector<int> c(ms); for (int i = 0; i < ms; i++) { for (int j = 0; j < ms; j++) { c[i] = add(c[i], mul(a[i][j], b[j])); } } return c; } struct tree{ int pot; vector<Matrix> t; tree(int n){ pot = 1; while(pot <= n) pot *= 2; t = vector<Matrix>(pot * 2); for(int i=0; i<pot*2; i++){ t[i] = Id; } } void build(){ for(int i=pot-1; i>=1; i--){ t[i] = t[i*2+1] * t[i*2]; } } void modify(int x, Matrix a){ t[x += pot] = a; for(x/=2; x; x/=2){ t[x] = t[x*2+1] * t[x*2]; } } void maybe(int x, Matrix a){ if(a != t[x+pot]) modify(x, a); } }; int right(int i, int x){ auto it = s[x].upper_bound(i); if(it == s[x].end()) return -1; return *it; } int left(int i, int x){ auto it = s[x].lower_bound(i); if(it == s[x].begin()) return -1; return *--it; } int t[N]; int main(){ BOOST; int n, q; cin >> n >> q; string dat; cin >> dat; vector<pair<int, char>> query(q); bool ab = 1; s[0].insert(0); for(int i=1; i<=n; i++){ t[i] = dat[i-1] - 'a' + 1; s[t[i]].insert(i); if(t[i] != 1 && t[i] != 2) ab = 0; } for(int i=0; i<q; i++){ int a; char b; cin >> a >> b; query[i] = {a, b}; if(b != 'a' && b != 'b') ab = 0; } if(ab){ ms = 3; // cout << "ab only!\n"; // exit(0); } Id = makeId(); base = Id[0]; tree two(n), one(n); auto m1 = [&](int i) -> Matrix { Matrix x = Id; for(int j=0; j<ms; j++){ x[t[i]][j] = 1; } return x; }; auto m2 = [&](int i) -> Matrix { Matrix x = Id; for(int j=0; j<ms; j++){ if(left(i, t[i]) <= left(i, j)){ x[t[i]][j] = 1; } } return x; }; auto res = [&]() -> void { int two_res = 0, one_res = 0; auto a = two.t[1]*base; // t[1]? auto b = one.t[1]*base; for(int i=1; i<ms; i++){ two_res = add(two_res, a[i]); one_res = add(one_res, b[i]); } cout << sub(two_res, one_res) << "\n"; }; for(int i=1; i<=n; i++){ two.t[i+two.pot] = m1(i); one.t[i+one.pot] = m2(i); } two.build(); one.build(); res(); for(auto it : query){ int i = it.st; int c = it.nd; s[t[i]].erase(i); t[i] = c - 'a' + 1; s[t[i]].insert(i); two.modify(i, m1(i)); one.modify(i, m2(i)); for(int j=0; j<ms; j++){ int r = right(i, j); if(r != -1) one.maybe(r, m2(r)); } res(); } // printf("cnt = %d\n", cnt); } // precomp macierzy z 4 liter // opt z ab
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) typedef long long ll; typedef long double ld; typedef pair<int, int> ii; const int N = 5e4 + 5, M = 7, mod = 998244353; int ms = 7; set<int> s[M]; using Matrix = vector<vector<int>>; Matrix makeId(){ Matrix x(ms, vector<int>(ms)); for(int i=0; i<ms; i++) x[i][i] = 1; return x; } Matrix Id; vector<int> base; // ostream& operator<<(ostream& os, const Matrix& matrix) { // for (const auto& row : matrix) { // os << "["; // for (size_t i = 0; i < row.size(); ++i) { // os << row[i]; // if (i < row.size() - 1) { // os << ", "; // } // } // os << "]" << endl; // } // return os; // } inline int add(int a, int b){ a += b; if(a >= mod) a -= mod; return a; } inline int sub(int a, int b){ return (a-b+mod) %mod; } // int cnt = 0; inline int mul(int a, int b){ // cnt++; return (ll)a*b %mod; } Matrix operator*(const Matrix& a, const Matrix& b) { if(a.empty() || b.empty()) return {{}}; Matrix c(ms, vector<int>(ms)); c[0][0] = 1; for (int i = 1; i < ms; i++) { for (int j = 0; j < ms; j++) { for (int k = 0; k < ms; k++) { c[i][j] = add(c[i][j], mul(a[i][k], b[k][j])); } } } return c; } vector<int> operator*(const Matrix& a, const vector<int>& b) { vector<int> c(ms); for (int i = 0; i < ms; i++) { for (int j = 0; j < ms; j++) { c[i] = add(c[i], mul(a[i][j], b[j])); } } return c; } struct tree{ int pot; vector<Matrix> t; tree(int n){ pot = 1; while(pot <= n) pot *= 2; t = vector<Matrix>(pot * 2); for(int i=0; i<pot*2; i++){ t[i] = Id; } } void build(){ for(int i=pot-1; i>=1; i--){ t[i] = t[i*2+1] * t[i*2]; } } void modify(int x, Matrix a){ t[x += pot] = a; for(x/=2; x; x/=2){ t[x] = t[x*2+1] * t[x*2]; } } void maybe(int x, Matrix a){ if(a != t[x+pot]) modify(x, a); } }; int right(int i, int x){ auto it = s[x].upper_bound(i); if(it == s[x].end()) return -1; return *it; } int left(int i, int x){ auto it = s[x].lower_bound(i); if(it == s[x].begin()) return -1; return *--it; } int t[N]; int main(){ BOOST; int n, q; cin >> n >> q; string dat; cin >> dat; vector<pair<int, char>> query(q); bool ab = 1; s[0].insert(0); for(int i=1; i<=n; i++){ t[i] = dat[i-1] - 'a' + 1; s[t[i]].insert(i); if(t[i] != 1 && t[i] != 2) ab = 0; } for(int i=0; i<q; i++){ int a; char b; cin >> a >> b; query[i] = {a, b}; if(b != 'a' && b != 'b') ab = 0; } if(ab){ ms = 3; // cout << "ab only!\n"; // exit(0); } Id = makeId(); base = Id[0]; tree two(n), one(n); auto m1 = [&](int i) -> Matrix { Matrix x = Id; for(int j=0; j<ms; j++){ x[t[i]][j] = 1; } return x; }; auto m2 = [&](int i) -> Matrix { Matrix x = Id; for(int j=0; j<ms; j++){ if(left(i, t[i]) <= left(i, j)){ x[t[i]][j] = 1; } } return x; }; auto res = [&]() -> void { int two_res = 0, one_res = 0; auto a = two.t[1]*base; // t[1]? auto b = one.t[1]*base; for(int i=1; i<ms; i++){ two_res = add(two_res, a[i]); one_res = add(one_res, b[i]); } cout << sub(two_res, one_res) << "\n"; }; for(int i=1; i<=n; i++){ two.t[i+two.pot] = m1(i); one.t[i+one.pot] = m2(i); } two.build(); one.build(); res(); for(auto it : query){ int i = it.st; int c = it.nd; s[t[i]].erase(i); t[i] = c - 'a' + 1; s[t[i]].insert(i); two.modify(i, m1(i)); one.modify(i, m2(i)); for(int j=0; j<ms; j++){ int r = right(i, j); if(r != -1) one.maybe(r, m2(r)); } res(); } // printf("cnt = %d\n", cnt); } // precomp macierzy z 4 liter // opt z ab |