#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
const ll m=998244353;
struct jz
{
ll a[7][7];
jz operator*(jz b)
{
jz r;
for(int i=0;i<7;i++) for(int j=0;j<7;j++) r.a[i][j]=0;
for(int i=0;i<7;i++) for(int j=0;j<7;j++) for(int k=0;k<7;k++) r.a[i][k]+=a[i][j]*b.a[j][k]%m;
for(int i=0;i<7;i++) for(int j=0;j<7;j++) r.a[i][j]%=m;
return r;
}
} u[6];
ll ls[6][6];
ll gt[6];
int vld[6];
bool vd[6];
struct jzg
{
ll a[6][6];
int ft[6];
int ed[6];
int z;
void op()
{
cout<<"----\nAl "<<z<<endl;
for(int i=0;i<z;i++) cout<<ft[i]<<' ';
cout<<endl;
for(int i=0;i<z;i++) cout<<ed[i]<<' ';
cout<<endl;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++) cout<<a[i][j]<<' ';
cout<<endl;
}
cout<<"----\n";
}
jzg operator*(jzg b)
{
// cout<<"Adding"<<endl;
// op();
// b.op();
jzg r;
for(int i=0;i<6;i++) for(int j=0;j<6;j++) ls[i][j]=a[i][j],r.a[i][j]=0;
for(int i=0;i<6;i++) vld[i]=1<<i;
for(int i=z-1;i>=0;i--)
{
vld[ed[i]]=0;
for(int j=0;j<6;j++) vld[j]|=1<<ed[i];
}
// for(int i=0;i<6;i++)
// {
// for(int j=0;j<6;j++) cout<<ls[i][j]<<' ';
// cout<<endl;
// }
for(int i=0;i<b.z;i++)
{
int g=b.ft[i];
for(int j=0;j<6;j++) gt[j]=0;
// cout<<"F "<<g<<' '<<vld[g]<<endl;
for(int j=0;j<6;j++) if(vld[g]&1<<j)
{
for(int k=0;k<6;k++) gt[k]+=ls[j][k];
}
for(int j=0;j<6;j++) ls[g][j]=gt[j]%m;
// vld[g]=0;
for(int j=0;j<6;j++) vld[j]^=vld[j]&1<<g;
}
// for(int i=0;i<6;i++)
// {
// for(int j=0;j<6;j++) cout<<ls[i][j]<<' ';
// cout<<endl;
// }
for(int i=0;i<6;i++) for(int j=0;j<6;j++) for(int k=0;k<6;k++) r.a[i][k]+=b.a[i][j]*ls[j][k]%m;
for(int i=0;i<6;i++) for(int j=0;j<6;j++) r.a[i][j]%=m;
for(int i=0;i<6;i++) vd[i]=0;
r.z=0;
for(int i=0;i<z;i++) if(!vd[ft[i]]) r.ft[r.z++]=ft[i],vd[ft[i]]=1;
for(int i=0;i<b.z;i++) if(!vd[b.ft[i]]) r.ft[r.z++]=b.ft[i],vd[b.ft[i]]=1;
for(int i=0;i<6;i++) vd[i]=0;
r.z=0;
for(int i=0;i<b.z;i++) if(!vd[b.ed[i]]) r.ed[r.z++]=b.ed[i],vd[b.ed[i]]=1;
for(int i=0;i<z;i++) if(!vd[ed[i]]) r.ed[r.z++]=ed[i],vd[ed[i]]=1;
// r.op();
return r;
}
} v[6];
struct node
{
int l,r;
jz va;
jzg vb;
} nd[200005];
int a[50004];
void bd(int i,int l,int r)
{
node&x=nd[i];
x.l=l;
x.r=r;
if(l==r-1)
{
x.va=u[a[l]];
x.vb=v[a[l]];
return;
}
bd(i*2,l,l+r>>1);
bd(i*2+1,l+r>>1,r);
x.va=nd[i*2].va*nd[i*2+1].va;
x.vb=nd[i*2].vb*nd[i*2+1].vb;
}
void st(int i,int l,int h)
{
node&x=nd[i];
if(x.l==x.r-1)
{
x.va=u[h];
x.vb=v[h];
return;
}
int m=x.l+x.r>>1;
if(l<m) st(i*2,l,h);
else st(i*2+1,l,h);
x.va=nd[i*2].va*nd[i*2+1].va;
x.vb=nd[i*2].vb*nd[i*2+1].vb;
}
void clc()
{
ll op=0;
for(int i=0;i<6;i++) op+=nd[1].va.a[i][6];
// cout<<"Cr "<<op<<' ';
int fz=nd[1].vb.z;
int *x=nd[1].vb.ft;
// cout<<fz<<endl;
// for(int i=0;i<3;i++)
// {
// for(int j=0;j<3;j++) cout<<nd[1].vb.a[i][j]<<' ';
// cout<<endl;
// }
for(int i=0;i<fz;i++) for(int j=0;j<fz;j++) op+=m-nd[1].vb.a[x[i]][x[j]];
cout<<op%m<<'\n';
}
int main()
{
// freopen("6.in","r",stdin);
// freopen("op.txt","w",stdout);
ios_base::sync_with_stdio(0);
for(int i=0;i<6;i++)
{
for(int j=0;j<7;j++) u[i].a[j][j]=1,u[i].a[i][j]=1;
for(int j=0;j<6;j++) v[i].a[j][j]=1;
v[i].z=1;
v[i].ft[0]=i;
v[i].ed[0]=i;
}
int n,q;
cin>>n>>q;
char c;
for(int i=0;i<n;i++)
{
cin>>c;
a[i]=c-'a';
}
bd(1,0,n);
clc();
while(q--)
{
int x;
cin>>x>>c;
x--;
st(1,x,c-'a');
clc();
}
return 0;
}/*4 3
abca
*/
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 | #include<set> #include<map> #include<queue> #include<vector> #include<algorithm> #include<bits/stdc++.h> #define pr pair #define f first #define s second #define ll long long #define mp make_pair #define pll pr<ll,ll> #define pii pr<int,int> #define piii pr<int,pii> using namespace std; const ll m=998244353; struct jz { ll a[7][7]; jz operator*(jz b) { jz r; for(int i=0;i<7;i++) for(int j=0;j<7;j++) r.a[i][j]=0; for(int i=0;i<7;i++) for(int j=0;j<7;j++) for(int k=0;k<7;k++) r.a[i][k]+=a[i][j]*b.a[j][k]%m; for(int i=0;i<7;i++) for(int j=0;j<7;j++) r.a[i][j]%=m; return r; } } u[6]; ll ls[6][6]; ll gt[6]; int vld[6]; bool vd[6]; struct jzg { ll a[6][6]; int ft[6]; int ed[6]; int z; void op() { cout<<"----\nAl "<<z<<endl; for(int i=0;i<z;i++) cout<<ft[i]<<' '; cout<<endl; for(int i=0;i<z;i++) cout<<ed[i]<<' '; cout<<endl; for(int i=0;i<6;i++) { for(int j=0;j<6;j++) cout<<a[i][j]<<' '; cout<<endl; } cout<<"----\n"; } jzg operator*(jzg b) { // cout<<"Adding"<<endl; // op(); // b.op(); jzg r; for(int i=0;i<6;i++) for(int j=0;j<6;j++) ls[i][j]=a[i][j],r.a[i][j]=0; for(int i=0;i<6;i++) vld[i]=1<<i; for(int i=z-1;i>=0;i--) { vld[ed[i]]=0; for(int j=0;j<6;j++) vld[j]|=1<<ed[i]; } // for(int i=0;i<6;i++) // { // for(int j=0;j<6;j++) cout<<ls[i][j]<<' '; // cout<<endl; // } for(int i=0;i<b.z;i++) { int g=b.ft[i]; for(int j=0;j<6;j++) gt[j]=0; // cout<<"F "<<g<<' '<<vld[g]<<endl; for(int j=0;j<6;j++) if(vld[g]&1<<j) { for(int k=0;k<6;k++) gt[k]+=ls[j][k]; } for(int j=0;j<6;j++) ls[g][j]=gt[j]%m; // vld[g]=0; for(int j=0;j<6;j++) vld[j]^=vld[j]&1<<g; } // for(int i=0;i<6;i++) // { // for(int j=0;j<6;j++) cout<<ls[i][j]<<' '; // cout<<endl; // } for(int i=0;i<6;i++) for(int j=0;j<6;j++) for(int k=0;k<6;k++) r.a[i][k]+=b.a[i][j]*ls[j][k]%m; for(int i=0;i<6;i++) for(int j=0;j<6;j++) r.a[i][j]%=m; for(int i=0;i<6;i++) vd[i]=0; r.z=0; for(int i=0;i<z;i++) if(!vd[ft[i]]) r.ft[r.z++]=ft[i],vd[ft[i]]=1; for(int i=0;i<b.z;i++) if(!vd[b.ft[i]]) r.ft[r.z++]=b.ft[i],vd[b.ft[i]]=1; for(int i=0;i<6;i++) vd[i]=0; r.z=0; for(int i=0;i<b.z;i++) if(!vd[b.ed[i]]) r.ed[r.z++]=b.ed[i],vd[b.ed[i]]=1; for(int i=0;i<z;i++) if(!vd[ed[i]]) r.ed[r.z++]=ed[i],vd[ed[i]]=1; // r.op(); return r; } } v[6]; struct node { int l,r; jz va; jzg vb; } nd[200005]; int a[50004]; void bd(int i,int l,int r) { node&x=nd[i]; x.l=l; x.r=r; if(l==r-1) { x.va=u[a[l]]; x.vb=v[a[l]]; return; } bd(i*2,l,l+r>>1); bd(i*2+1,l+r>>1,r); x.va=nd[i*2].va*nd[i*2+1].va; x.vb=nd[i*2].vb*nd[i*2+1].vb; } void st(int i,int l,int h) { node&x=nd[i]; if(x.l==x.r-1) { x.va=u[h]; x.vb=v[h]; return; } int m=x.l+x.r>>1; if(l<m) st(i*2,l,h); else st(i*2+1,l,h); x.va=nd[i*2].va*nd[i*2+1].va; x.vb=nd[i*2].vb*nd[i*2+1].vb; } void clc() { ll op=0; for(int i=0;i<6;i++) op+=nd[1].va.a[i][6]; // cout<<"Cr "<<op<<' '; int fz=nd[1].vb.z; int *x=nd[1].vb.ft; // cout<<fz<<endl; // for(int i=0;i<3;i++) // { // for(int j=0;j<3;j++) cout<<nd[1].vb.a[i][j]<<' '; // cout<<endl; // } for(int i=0;i<fz;i++) for(int j=0;j<fz;j++) op+=m-nd[1].vb.a[x[i]][x[j]]; cout<<op%m<<'\n'; } int main() { // freopen("6.in","r",stdin); // freopen("op.txt","w",stdout); ios_base::sync_with_stdio(0); for(int i=0;i<6;i++) { for(int j=0;j<7;j++) u[i].a[j][j]=1,u[i].a[i][j]=1; for(int j=0;j<6;j++) v[i].a[j][j]=1; v[i].z=1; v[i].ft[0]=i; v[i].ed[0]=i; } int n,q; cin>>n>>q; char c; for(int i=0;i<n;i++) { cin>>c; a[i]=c-'a'; } bd(1,0,n); clc(); while(q--) { int x; cin>>x>>c; x--; st(1,x,c-'a'); clc(); } return 0; }/*4 3 abca */ |
English