#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)
#define REP(i,k) FOR(i,0,(k)-1)
#define RFOR(i,p,n) for(int i=(p); i>=(n); --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define pn printf("\n")
using namespace std;
typedef long long ll;
typedef V <int> vi;
typedef V <ll> vll;
typedef C int ci;
typedef C ll cll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf=2.1e9;
cll infll=4.5e18;
int I(){
int z;
scanf("%d", &z);
//cin>>z;
return z;
}
ci mod=998244353;
typedef V <vi> vvi;
struct mac{
vvi t;
mac(){}
mac(ci n, ci m){
t.resize(n, vi(m));
}
vi &operator[](ci i){
return t[i];
}
C vi &operator[](ci i) C{
return t[i];
}
template <int rozm> mac mul(C mac &a) C{
static mac ret(rozm, rozm);
REP(i, rozm)
REP(j, rozm){
ll l=0;
static_assert(__int128(mod)*mod*7<=(__int128(1)<<63));
REP(k, rozm)
l+=ll(t[i][k])*a[k][j];
ret[i][j]=int(l%mod);
}
return ret;
}
mac operator*(C mac &a) C{
assert(ssize(t[0])==ssize(a.t));
if (ssize(t[0])==ssize(t)&&ssize(a.t)==ssize(a[0])&&ssize(t)==ssize(a.t)){
if (ssize(t[0])==6)
return mul<6>(a);
if (ssize(t[0])==7)
return mul<7>(a);
assert(0);
}
mac ret(ssize(t), ssize(a[0]));
REP(i, ssize(t))
REP(j, ssize(a[0])){
ll l=0;
static_assert(__int128(mod)*mod*7<=(__int128(1)<<63));
ci cap=ssize(a.t);
REP(k, cap)
l+=ll(t[i][k])*a[k][j];
ret[i][j]=int(l%mod);
}
return ret;
}
void operator*=(C mac &a){
*this=*this*a;
}
bool operator==(C mac &a) C{
return t==a.t;
}
};
struct stt{
V <mac> t;
int comp;
stt(ci n){
comp=bit_ceil(unsigned(n));
t.resize(comp<<1);
}
void pull(ci i){
if (t[i<<1].t.size()){
if (t[i<<1|1].t.size()){
t[i]=t[i<<1]*t[i<<1|1];
}
else
t[i]=t[i<<1];
}
else
t[i]=mac(0, 0);
}
mac dej() C{
return t[1];
}
void ust(int i, C mac &m){
i+=comp;
if (t[i]==m)
return;
t[i]=m;
for (i>>=1; i; i>>=1)
pull(i);
}
/*mac que(int p, int k) C{
mac lew,praw;
for (p+=comp,k+=comp+1; p<k; p>>=1,k>>=1){
if (p&1){
if (!lew.t.size())
lew=t[p];
else
lew*=t[p];
++p;
}
if (k&1){
--k;
if (!praw.t.size())
praw=t[k];
else
praw=t[k]*praw;
}
}
if (!lew.t.size())
lew=praw;
else if (praw.t.size())
lew*=praw;
return lew;
}*/
};
void ans(){
ci alf=6;
V <mac> fullbazmac(alf);
REP(i, alf){
mac m(alf+1, alf+1);
REP(a, alf+1){
m[a][a]=1;
m[i][a]=1;
}
fullbazmac[i]=m;
}
ci m2s=alf;
mac pierw(1, 0),pojpierw(1, 0),pojidmac(m2s, m2s);
pierw[0].resize(alf+1, 1); pierw[0][alf]=0;
pojpierw[0].resize(alf, 1);
REP(i, m2s)
pojidmac[i][i]=1;
ci n=I(),q=I();
vi wej(n);
scanf("\n");
stt stfull(n),stpoj(n);
V <set <int>> poz(alf);
auto dejwyn=[&](){
C mac fullm=pierw*stfull.dej();
C mac pojm=pojpierw*stpoj.dej();
cll full=fullm[0][alf];
ll poj=0;
REP(i, alf)
if (poz[i].size())
poj+=pojm[0][i];
return int((full-poj%mod+mod)%mod);
};
auto genmac=[&](ci i){
mac m=pojidmac;
REP(a, m2s)
m[a][a]=1;
auto popitr=poz[wej[i]].find(i);
ci pop=popitr == poz[wej[i]].begin() ? 0 : *(--popitr)+1;
REP(a, alf){
auto itr=poz[a].upper_bound(i);
if (itr==poz[a].begin())
continue;
--itr;
m[a][wej[i]]=pop<=*itr;
}
return m;
};
auto aktmac=[&](ci i){
if (!i||wej[i]!=wej[i-1])
stpoj.ust(i, genmac(i));
else
stpoj.ust(i, pojidmac);
};
REP(i, n){
wej[i]=getchar_unlocked()-'a';
poz[wej[i]].emplace(i);
stfull.ust(i, fullbazmac[wej[i]]);
aktmac(i);
}
printf("%d\n", dejwyn());
REP(_, q){
int p; char z;
scanf("\n%d %c", &p, &z);
poz[wej[p-1]].erase(p-1);
wej[p-1]=z-'a';
poz[wej[p-1]].emplace(p-1);
stfull.ust(p-1, fullbazmac[wej[p-1]]);
aktmac(p-1);
REP(i, alf){
auto itr=poz[i].upper_bound(p-1);
if (itr!=poz[i].end())
aktmac(*itr);
}
//if (p<n)
// aktmac(p);
printf("%d\n", dejwyn());
}
fflush(stdout);
_Exit(0);
}
int main(){
//ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
//tt=I();
while (tt--)ans();
}
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 | #ifndef LOCAL #pragma GCC optimize("O3") #endif #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define REP(i,k) FOR(i,0,(k)-1) #define RFOR(i,p,n) for(int i=(p); i>=(n); --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define pn printf("\n") using namespace std; typedef long long ll; typedef V <int> vi; typedef V <ll> vll; typedef C int ci; typedef C ll cll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf=2.1e9; cll infll=4.5e18; int I(){ int z; scanf("%d", &z); //cin>>z; return z; } ci mod=998244353; typedef V <vi> vvi; struct mac{ vvi t; mac(){} mac(ci n, ci m){ t.resize(n, vi(m)); } vi &operator[](ci i){ return t[i]; } C vi &operator[](ci i) C{ return t[i]; } template <int rozm> mac mul(C mac &a) C{ static mac ret(rozm, rozm); REP(i, rozm) REP(j, rozm){ ll l=0; static_assert(__int128(mod)*mod*7<=(__int128(1)<<63)); REP(k, rozm) l+=ll(t[i][k])*a[k][j]; ret[i][j]=int(l%mod); } return ret; } mac operator*(C mac &a) C{ assert(ssize(t[0])==ssize(a.t)); if (ssize(t[0])==ssize(t)&&ssize(a.t)==ssize(a[0])&&ssize(t)==ssize(a.t)){ if (ssize(t[0])==6) return mul<6>(a); if (ssize(t[0])==7) return mul<7>(a); assert(0); } mac ret(ssize(t), ssize(a[0])); REP(i, ssize(t)) REP(j, ssize(a[0])){ ll l=0; static_assert(__int128(mod)*mod*7<=(__int128(1)<<63)); ci cap=ssize(a.t); REP(k, cap) l+=ll(t[i][k])*a[k][j]; ret[i][j]=int(l%mod); } return ret; } void operator*=(C mac &a){ *this=*this*a; } bool operator==(C mac &a) C{ return t==a.t; } }; struct stt{ V <mac> t; int comp; stt(ci n){ comp=bit_ceil(unsigned(n)); t.resize(comp<<1); } void pull(ci i){ if (t[i<<1].t.size()){ if (t[i<<1|1].t.size()){ t[i]=t[i<<1]*t[i<<1|1]; } else t[i]=t[i<<1]; } else t[i]=mac(0, 0); } mac dej() C{ return t[1]; } void ust(int i, C mac &m){ i+=comp; if (t[i]==m) return; t[i]=m; for (i>>=1; i; i>>=1) pull(i); } /*mac que(int p, int k) C{ mac lew,praw; for (p+=comp,k+=comp+1; p<k; p>>=1,k>>=1){ if (p&1){ if (!lew.t.size()) lew=t[p]; else lew*=t[p]; ++p; } if (k&1){ --k; if (!praw.t.size()) praw=t[k]; else praw=t[k]*praw; } } if (!lew.t.size()) lew=praw; else if (praw.t.size()) lew*=praw; return lew; }*/ }; void ans(){ ci alf=6; V <mac> fullbazmac(alf); REP(i, alf){ mac m(alf+1, alf+1); REP(a, alf+1){ m[a][a]=1; m[i][a]=1; } fullbazmac[i]=m; } ci m2s=alf; mac pierw(1, 0),pojpierw(1, 0),pojidmac(m2s, m2s); pierw[0].resize(alf+1, 1); pierw[0][alf]=0; pojpierw[0].resize(alf, 1); REP(i, m2s) pojidmac[i][i]=1; ci n=I(),q=I(); vi wej(n); scanf("\n"); stt stfull(n),stpoj(n); V <set <int>> poz(alf); auto dejwyn=[&](){ C mac fullm=pierw*stfull.dej(); C mac pojm=pojpierw*stpoj.dej(); cll full=fullm[0][alf]; ll poj=0; REP(i, alf) if (poz[i].size()) poj+=pojm[0][i]; return int((full-poj%mod+mod)%mod); }; auto genmac=[&](ci i){ mac m=pojidmac; REP(a, m2s) m[a][a]=1; auto popitr=poz[wej[i]].find(i); ci pop=popitr == poz[wej[i]].begin() ? 0 : *(--popitr)+1; REP(a, alf){ auto itr=poz[a].upper_bound(i); if (itr==poz[a].begin()) continue; --itr; m[a][wej[i]]=pop<=*itr; } return m; }; auto aktmac=[&](ci i){ if (!i||wej[i]!=wej[i-1]) stpoj.ust(i, genmac(i)); else stpoj.ust(i, pojidmac); }; REP(i, n){ wej[i]=getchar_unlocked()-'a'; poz[wej[i]].emplace(i); stfull.ust(i, fullbazmac[wej[i]]); aktmac(i); } printf("%d\n", dejwyn()); REP(_, q){ int p; char z; scanf("\n%d %c", &p, &z); poz[wej[p-1]].erase(p-1); wej[p-1]=z-'a'; poz[wej[p-1]].emplace(p-1); stfull.ust(p-1, fullbazmac[wej[p-1]]); aktmac(p-1); REP(i, alf){ auto itr=poz[i].upper_bound(p-1); if (itr!=poz[i].end()) aktmac(*itr); } //if (p<n) // aktmac(p); printf("%d\n", dejwyn()); } fflush(stdout); _Exit(0); } int main(){ //ios_base::sync_with_stdio(0),cin.tie(0); int tt=1; //tt=I(); while (tt--)ans(); } |
English