#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
using ull=unsigned long long;
const ull mod=998244353;
void upd(ull &a,ull b)
{
a+=b;
if(a>=mod) a-=mod;
}
#define ls (x<<1)
#define rs (ls|1)
string s;
ull tmp[6][6];
struct info_t
{
ull v[7][7];
ull w[6][6];
int pl[6],pr[6];
void init(char ch,int pos)
{
int c=ch-'a';
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
v[i][j]=0;
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
w[i][j]=0;
for(int i=0;i<6;i++)
{
pl[i]=inf;
pr[i]=-1;
}
pl[c]=pr[c]=pos;
for(int i=0;i<6;i++)
if(i!=c)
v[i][i]=1;
v[6][6]=2;
v[c][6]=mod-1;
v[6][c]=1;
w[c][c]=1;
}
void pushup(const info_t &L,const info_t &R)
{
for(int a=0;a<7;a++)
for(int b=0;b<7;b++)
{
v[a][b]=0;
for(int c=0;c<7;c++)
upd(v[a][b],L.v[a][c]*R.v[c][b]%mod);
}
for(int a=0;a<6;a++)
{
pl[a]=(~L.pr[a])?L.pl[a]:R.pl[a];
pr[a]=(~R.pr[a])?R.pr[a]:L.pr[a];
}
for(int a=0;a<6;a++)
for(int b=0;b<6;b++)
{
w[a][b]=0;
if(R.pr[b]==-1)
upd(w[a][b],L.w[a][b]);
if(L.pr[a]==-1)
upd(w[a][b],R.w[a][b]);
}
memset(tmp,0,sizeof(tmp));
for(int a=0;a<6;a++)
for(int x=0;x<6;x++)
for(int y=0;y<6;y++)
if(L.pr[y]<=L.pr[x]&&R.pl[x]>=R.pl[y])
upd(tmp[a][y],L.w[a][x]);
for(int a=0;a<6;a++)
for(int y=0;y<6;y++)
for(int b=0;b<6;b++)
upd(w[a][b],tmp[a][y]*R.w[y][b]%mod);
}
}seg[50050<<2];
void build(int x,int l,int r)
{
if(l==r)
{
seg[x].init(s[l-1],l);
return ;
}
int mid=(l+r)/2;
build(ls,l,mid);
build(rs,mid+1,r);
seg[x].pushup(seg[ls],seg[rs]);
}
void update(int x,int l,int r,int p)
{
if(l==r)
{
seg[x].init(s[l-1],l);
return ;
}
int mid=(l+r)/2;
if(p<=mid) update(ls,l,mid,p);
else update(rs,mid+1,r,p);
seg[x].pushup(seg[ls],seg[rs]);
}
int n,q;
void answer()
{
ull ans=seg[1].v[6][6];
for(int i=0;i<6;i++)
for(int j=0;j<6;j++)
upd(ans,mod-seg[1].w[i][j]);
upd(ans,mod-1);
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q>>s;
build(1,1,n);
answer();
while(q--)
{
int p;
char ch;
cin>>p>>ch;
s[p-1]=ch;
update(1,1,n,p);
answer();
}
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 | #include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f using ull=unsigned long long; const ull mod=998244353; void upd(ull &a,ull b) { a+=b; if(a>=mod) a-=mod; } #define ls (x<<1) #define rs (ls|1) string s; ull tmp[6][6]; struct info_t { ull v[7][7]; ull w[6][6]; int pl[6],pr[6]; void init(char ch,int pos) { int c=ch-'a'; for(int i=0;i<7;i++) for(int j=0;j<7;j++) v[i][j]=0; for(int i=0;i<6;i++) for(int j=0;j<6;j++) w[i][j]=0; for(int i=0;i<6;i++) { pl[i]=inf; pr[i]=-1; } pl[c]=pr[c]=pos; for(int i=0;i<6;i++) if(i!=c) v[i][i]=1; v[6][6]=2; v[c][6]=mod-1; v[6][c]=1; w[c][c]=1; } void pushup(const info_t &L,const info_t &R) { for(int a=0;a<7;a++) for(int b=0;b<7;b++) { v[a][b]=0; for(int c=0;c<7;c++) upd(v[a][b],L.v[a][c]*R.v[c][b]%mod); } for(int a=0;a<6;a++) { pl[a]=(~L.pr[a])?L.pl[a]:R.pl[a]; pr[a]=(~R.pr[a])?R.pr[a]:L.pr[a]; } for(int a=0;a<6;a++) for(int b=0;b<6;b++) { w[a][b]=0; if(R.pr[b]==-1) upd(w[a][b],L.w[a][b]); if(L.pr[a]==-1) upd(w[a][b],R.w[a][b]); } memset(tmp,0,sizeof(tmp)); for(int a=0;a<6;a++) for(int x=0;x<6;x++) for(int y=0;y<6;y++) if(L.pr[y]<=L.pr[x]&&R.pl[x]>=R.pl[y]) upd(tmp[a][y],L.w[a][x]); for(int a=0;a<6;a++) for(int y=0;y<6;y++) for(int b=0;b<6;b++) upd(w[a][b],tmp[a][y]*R.w[y][b]%mod); } }seg[50050<<2]; void build(int x,int l,int r) { if(l==r) { seg[x].init(s[l-1],l); return ; } int mid=(l+r)/2; build(ls,l,mid); build(rs,mid+1,r); seg[x].pushup(seg[ls],seg[rs]); } void update(int x,int l,int r,int p) { if(l==r) { seg[x].init(s[l-1],l); return ; } int mid=(l+r)/2; if(p<=mid) update(ls,l,mid,p); else update(rs,mid+1,r,p); seg[x].pushup(seg[ls],seg[rs]); } int n,q; void answer() { ull ans=seg[1].v[6][6]; for(int i=0;i<6;i++) for(int j=0;j<6;j++) upd(ans,mod-seg[1].w[i][j]); upd(ans,mod-1); cout<<ans<<'\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q>>s; build(1,1,n); answer(); while(q--) { int p; char ch; cin>>p>>ch; s[p-1]=ch; update(1,1,n,p); answer(); } return 0; } |
English