#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998'244'353;
const int nax = 5e4 + 10;
struct Matrix {
long long m[3][3];
Matrix()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
m[i][j] = (i == j) ? 1 : 0;
}
Matrix(long long values[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
m[i][j] = values[i][j] % mod;
}
Matrix operator*(const Matrix &other) const
{
Matrix res;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
res.m[i][j] = 0;
for (int k = 0; k < 3; k++)
res.m[i][j] = (res.m[i][j] + m[i][k] * other.m[k][j]) % mod;
}
}
return res;
}
};
long long A_values[3][3] = { {2, -1, 0}, {1, 0, 0}, {0, 0, 1} };
long long B_values[3][3] = { {2, 0, -1}, {0, 1, 0}, {1, 0, 0} };
Matrix T_a(A_values);
Matrix T_b(B_values);
class SegmentTree {
private:
int size;
vector<Matrix> tree;
string S;
void build(int node, int start, int end)
{
if (start == end)
{
tree[node] = (S[start] == 'a') ? T_a : T_b;
}
else
{
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
tree[node] = tree[2 * node] * tree[2 * node + 1];
}
}
void update(int node, int start, int end, int idx)
{
if (start == end)
{
tree[node] = (S[idx] == 'a') ? T_a : T_b;
}
else
{
int mid = (start + end) / 2;
if (idx <= mid)
update(2 * node, start, mid, idx);
else
update(2 * node + 1, mid + 1, end, idx);
tree[node] = tree[2 * node] * tree[2 * node + 1];
}
}
public:
SegmentTree(string &str) : S(str)
{
size = str.size();
tree.resize(4 * size);
build(1, 0, size - 1);
}
void flip(int pos)
{
S[pos] = (S[pos] == 'a') ? 'b' : 'a';
update(1, 0, size - 1, pos);
}
long long distinctSubsequences()
{
Matrix result = tree[1];
return (result.m[0][0] - 1 + mod) % mod;
}
};
void solve_only_ab(int n, int q, string s, vector<pair<int, char>> queries)
{
vector<LL> fib(nax);
fib[0] = 0; fib[1] = 1;
for (int i = 2; i < nax; i++)
fib[i] = (fib[i-1] + fib[i-2]) % mod;
SegmentTree segTree(s);
int parts = 1;
for (int i = 0; i < s.size()-1; i++)
parts += (s[i] != s[i+1]);
LL distinct = segTree.distinctSubsequences();
LL unique = (parts == 1 ? 1 : fib[parts+2]);
auto answer = [&](){
return (distinct - unique + mod) % mod;
};
cout << answer() << "\n";
for (auto &[a, c] : queries)
{
if (s[a] != c)
{
segTree.flip(a);
parts -= (a > 0 && s[a-1] != s[a]);
parts -= (a < s.size()-1 && s[a+1] != s[a]);
s[a] = c;
parts += (a > 0 && s[a-1] != s[a]);
parts += (a < s.size()-1 && s[a+1] != s[a]);
distinct = segTree.distinctSubsequences();
unique = (parts == 1 ? 1 : fib[parts+2]);
}
cout << answer() << "\n";
}
}
LL answer_bruteforce(const string &s)
{
int n = s.size();
vector<LL> dp(n+1);
vector<int> last(10, -1);
dp[0] = 1;
for (int i = 0; i < n; i++)
{
int c = s[i] - 'a';
dp[i+1] = 2*dp[i] % mod;
if (last[c] != -1)
dp[i+1] += mod - dp[last[c]];
dp[i+1] %= mod;
last[c] = i;
}
LL all_subsequences = dp[n]-1;
dp = vector<LL>(n);
last = vector<int>(10, -1);
dp[0] = 1;
last[s[0] - 'a'] = 0;
for (int i = 1; i < n; i++)
{
int c = s[i] - 'a';
if (last[c] == -1)
dp[i] = 1;
for (auto &j : last)
{
if (j != -1 && j >= last[c])
{
dp[i] += dp[j];
dp[i] %= mod;
}
}
last[c] = i;
}
LL unique_subsequences = 0;
for (auto &j : last)
{
if (j != -1)
{
unique_subsequences += dp[j];
unique_subsequences %= mod;
}
}
return (all_subsequences - unique_subsequences + mod) % mod;
}
void solve_bruteforce(int n, int q, string s, vector<pair<int, char>> queries)
{
cout << answer_bruteforce(s) << "\n";
for (auto &[a, c] : queries)
{
s[a] = c;
cout << answer_bruteforce(s) << "\n";
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<pair<int, char>> queries(q);
for (auto &[a, c] : queries)
{
cin >> a >> c;
a--;
}
bool only_ab = 1;
for (auto &c : s)
{
if (c != 'a' && c != 'b')
only_ab = 0;
}
for (auto &[a, c] : queries)
{
if (c != 'a' && c != 'b')
only_ab = 0;
}
if (only_ab)
solve_only_ab(n, q, s, queries);
else
solve_bruteforce(n, q, s, 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL mod = 998'244'353; const int nax = 5e4 + 10; struct Matrix { long long m[3][3]; Matrix() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = (i == j) ? 1 : 0; } Matrix(long long values[3][3]) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = values[i][j] % mod; } Matrix operator*(const Matrix &other) const { Matrix res; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { res.m[i][j] = 0; for (int k = 0; k < 3; k++) res.m[i][j] = (res.m[i][j] + m[i][k] * other.m[k][j]) % mod; } } return res; } }; long long A_values[3][3] = { {2, -1, 0}, {1, 0, 0}, {0, 0, 1} }; long long B_values[3][3] = { {2, 0, -1}, {0, 1, 0}, {1, 0, 0} }; Matrix T_a(A_values); Matrix T_b(B_values); class SegmentTree { private: int size; vector<Matrix> tree; string S; void build(int node, int start, int end) { if (start == end) { tree[node] = (S[start] == 'a') ? T_a : T_b; } else { int mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); tree[node] = tree[2 * node] * tree[2 * node + 1]; } } void update(int node, int start, int end, int idx) { if (start == end) { tree[node] = (S[idx] == 'a') ? T_a : T_b; } else { int mid = (start + end) / 2; if (idx <= mid) update(2 * node, start, mid, idx); else update(2 * node + 1, mid + 1, end, idx); tree[node] = tree[2 * node] * tree[2 * node + 1]; } } public: SegmentTree(string &str) : S(str) { size = str.size(); tree.resize(4 * size); build(1, 0, size - 1); } void flip(int pos) { S[pos] = (S[pos] == 'a') ? 'b' : 'a'; update(1, 0, size - 1, pos); } long long distinctSubsequences() { Matrix result = tree[1]; return (result.m[0][0] - 1 + mod) % mod; } }; void solve_only_ab(int n, int q, string s, vector<pair<int, char>> queries) { vector<LL> fib(nax); fib[0] = 0; fib[1] = 1; for (int i = 2; i < nax; i++) fib[i] = (fib[i-1] + fib[i-2]) % mod; SegmentTree segTree(s); int parts = 1; for (int i = 0; i < s.size()-1; i++) parts += (s[i] != s[i+1]); LL distinct = segTree.distinctSubsequences(); LL unique = (parts == 1 ? 1 : fib[parts+2]); auto answer = [&](){ return (distinct - unique + mod) % mod; }; cout << answer() << "\n"; for (auto &[a, c] : queries) { if (s[a] != c) { segTree.flip(a); parts -= (a > 0 && s[a-1] != s[a]); parts -= (a < s.size()-1 && s[a+1] != s[a]); s[a] = c; parts += (a > 0 && s[a-1] != s[a]); parts += (a < s.size()-1 && s[a+1] != s[a]); distinct = segTree.distinctSubsequences(); unique = (parts == 1 ? 1 : fib[parts+2]); } cout << answer() << "\n"; } } LL answer_bruteforce(const string &s) { int n = s.size(); vector<LL> dp(n+1); vector<int> last(10, -1); dp[0] = 1; for (int i = 0; i < n; i++) { int c = s[i] - 'a'; dp[i+1] = 2*dp[i] % mod; if (last[c] != -1) dp[i+1] += mod - dp[last[c]]; dp[i+1] %= mod; last[c] = i; } LL all_subsequences = dp[n]-1; dp = vector<LL>(n); last = vector<int>(10, -1); dp[0] = 1; last[s[0] - 'a'] = 0; for (int i = 1; i < n; i++) { int c = s[i] - 'a'; if (last[c] == -1) dp[i] = 1; for (auto &j : last) { if (j != -1 && j >= last[c]) { dp[i] += dp[j]; dp[i] %= mod; } } last[c] = i; } LL unique_subsequences = 0; for (auto &j : last) { if (j != -1) { unique_subsequences += dp[j]; unique_subsequences %= mod; } } return (all_subsequences - unique_subsequences + mod) % mod; } void solve_bruteforce(int n, int q, string s, vector<pair<int, char>> queries) { cout << answer_bruteforce(s) << "\n"; for (auto &[a, c] : queries) { s[a] = c; cout << answer_bruteforce(s) << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; vector<pair<int, char>> queries(q); for (auto &[a, c] : queries) { cin >> a >> c; a--; } bool only_ab = 1; for (auto &c : s) { if (c != 'a' && c != 'b') only_ab = 0; } for (auto &[a, c] : queries) { if (c != 'a' && c != 'b') only_ab = 0; } if (only_ab) solve_only_ab(n, q, s, queries); else solve_bruteforce(n, q, s, queries); } |
English