#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=(a);i<(n);i++) #define per(i,a,n) for (int i=(n)-1;i>=(a);i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef long double db; mt19937 mrand(1111); const ll mod=998244353; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=50100; int dp[N],lst[10]; int n,q,a[N]; char s[N],t[10]; struct node { int pre[6],suf[6]; int seq[6][6]; ll mat[7][7]; }nd[N*4]; ll tmp[10][10],tmp2[10][10]; void upd(int p) { rep(i,0,6) { nd[p].pre[i]=min(nd[p+p].pre[i],nd[p+p+1].pre[i]); nd[p].suf[i]=max(nd[p+p].suf[i],nd[p+p+1].suf[i]); } rep(i,0,6) rep(j,0,6) tmp[i][j]=0; rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]) { rep(k,0,6) if (nd[p+p+1].pre[k]<=n&&nd[p+p].suf[k]<=nd[p+p].suf[j] &&nd[p+p+1].pre[k]<=nd[p+p+1].pre[j]) { tmp[i][k]+=nd[p+p].seq[i][j]; } } rep(i,0,6) rep(k,0,6) tmp[i][k]%=mod; rep(i,0,6) rep(k,0,6) { tmp2[i][k]=(tmp[i][0]*nd[p+p+1].seq[0][k]+tmp[i][1]*nd[p+p+1].seq[1][k]+tmp[i][2]*nd[p+p+1].seq[2][k]+ tmp[i][3]*nd[p+p+1].seq[3][k]+tmp[i][4]*nd[p+p+1].seq[4][k]+tmp[i][5]*nd[p+p+1].seq[5][k])%mod; } rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]&&nd[p+p+1].pre[j]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p].seq[i][j]); } rep(i,0,6) rep(j,0,6) if (nd[p+p+1].seq[i][j]&&nd[p+p].pre[i]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p+1].seq[i][j]); } rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=tmp2[i][j]%mod; rep(i,0,6) rep(j,0,6) tmp[i][j]=0; rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]) { rep(k,0,6) if (nd[p+p+1].pre[k]<=n&&nd[p+p].suf[k]<=nd[p+p].suf[j] &&nd[p+p+1].pre[k]<=nd[p+p+1].pre[j]) { tmp[i][k]+=nd[p+p].seq[i][j]; } } rep(i,0,6) rep(k,0,6) tmp[i][k]%=mod; rep(i,0,6) rep(k,0,6) { tmp2[i][k]=(tmp[i][0]*nd[p+p+1].seq[0][k]+tmp[i][1]*nd[p+p+1].seq[1][k]+tmp[i][2]*nd[p+p+1].seq[2][k]+ tmp[i][3]*nd[p+p+1].seq[3][k]+tmp[i][4]*nd[p+p+1].seq[4][k]+tmp[i][5]*nd[p+p+1].seq[5][k])%mod; } rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]&&nd[p+p+1].pre[j]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p].seq[i][j]); } rep(i,0,6) rep(j,0,6) if (nd[p+p+1].seq[i][j]&&nd[p+p].pre[i]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p+1].seq[i][j]); } rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=tmp2[i][j]%mod; rep(i,0,7) rep(j,0,7) { nd[p].mat[i][j]=(nd[p+p].mat[i][0]*nd[p+p+1].mat[0][j]+nd[p+p].mat[i][1]*nd[p+p+1].mat[1][j]+ nd[p+p].mat[i][2]*nd[p+p+1].mat[2][j]+nd[p+p].mat[i][3]*nd[p+p+1].mat[3][j]+ nd[p+p].mat[i][4]*nd[p+p+1].mat[4][j]+nd[p+p].mat[i][5]*nd[p+p+1].mat[5][j]+ nd[p+p].mat[i][6]*nd[p+p+1].mat[6][j])%mod; } } void build(int p,int l,int r) { if (l==r) { rep(i,0,6) nd[p].pre[i]=n+1,nd[p].suf[i]=0; rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=0; rep(i,0,7) rep(j,0,7) nd[p].mat[i][j]=(i==j); rep(i,0,7) nd[p].mat[i][a[l]]=1; nd[p].seq[a[l]][a[l]]=1; nd[p].suf[a[l]]=l; nd[p].pre[a[l]]=l; } else { int md=(l+r)>>1; build(p+p,l,md); build(p+p+1,md+1,r); upd(p); } } void change(int p,int l,int r,int x,int v) { if (l==r) { a[x]=v; rep(i,0,6) nd[p].pre[i]=n+1,nd[p].suf[i]=0; rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=0; rep(i,0,7) rep(j,0,7) nd[p].mat[i][j]=(i==j); rep(i,0,7) nd[p].mat[i][a[x]]=1; nd[p].seq[a[x]][a[x]]=1; nd[p].suf[a[x]]=x; nd[p].pre[a[x]]=x; } else { int md=(l+r)>>1; if (x<=md) change(p+p,l,md,x,v); else change(p+p+1,md+1,r,x,v); upd(p); } } int query() { int ans1=0; int ans2=0; rep(i,0,6) ans1=(ans1+nd[1].mat[6][i])%mod; rep(i,0,6) rep(j,0,6) ans2=(ans2+nd[1].seq[i][j])%mod; ans1=(ans1+mod-ans2)%mod; return ans1; } int main() { scanf("%d%d",&n,&q); scanf("%s",s+1); rep(i,1,n+1) a[i]=s[i]-'a'; build(1,1,n); printf("%d\n",query()); rep(i,0,q) { int x; scanf("%d%s",&x,t); change(1,1,n,x,t[0]-'a'); printf("%d\n",query()); } }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=(a);i<(n);i++) #define per(i,a,n) for (int i=(n)-1;i>=(a);i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef long double db; mt19937 mrand(1111); const ll mod=998244353; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=50100; int dp[N],lst[10]; int n,q,a[N]; char s[N],t[10]; struct node { int pre[6],suf[6]; int seq[6][6]; ll mat[7][7]; }nd[N*4]; ll tmp[10][10],tmp2[10][10]; void upd(int p) { rep(i,0,6) { nd[p].pre[i]=min(nd[p+p].pre[i],nd[p+p+1].pre[i]); nd[p].suf[i]=max(nd[p+p].suf[i],nd[p+p+1].suf[i]); } rep(i,0,6) rep(j,0,6) tmp[i][j]=0; rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]) { rep(k,0,6) if (nd[p+p+1].pre[k]<=n&&nd[p+p].suf[k]<=nd[p+p].suf[j] &&nd[p+p+1].pre[k]<=nd[p+p+1].pre[j]) { tmp[i][k]+=nd[p+p].seq[i][j]; } } rep(i,0,6) rep(k,0,6) tmp[i][k]%=mod; rep(i,0,6) rep(k,0,6) { tmp2[i][k]=(tmp[i][0]*nd[p+p+1].seq[0][k]+tmp[i][1]*nd[p+p+1].seq[1][k]+tmp[i][2]*nd[p+p+1].seq[2][k]+ tmp[i][3]*nd[p+p+1].seq[3][k]+tmp[i][4]*nd[p+p+1].seq[4][k]+tmp[i][5]*nd[p+p+1].seq[5][k])%mod; } rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]&&nd[p+p+1].pre[j]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p].seq[i][j]); } rep(i,0,6) rep(j,0,6) if (nd[p+p+1].seq[i][j]&&nd[p+p].pre[i]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p+1].seq[i][j]); } rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=tmp2[i][j]%mod; rep(i,0,6) rep(j,0,6) tmp[i][j]=0; rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]) { rep(k,0,6) if (nd[p+p+1].pre[k]<=n&&nd[p+p].suf[k]<=nd[p+p].suf[j] &&nd[p+p+1].pre[k]<=nd[p+p+1].pre[j]) { tmp[i][k]+=nd[p+p].seq[i][j]; } } rep(i,0,6) rep(k,0,6) tmp[i][k]%=mod; rep(i,0,6) rep(k,0,6) { tmp2[i][k]=(tmp[i][0]*nd[p+p+1].seq[0][k]+tmp[i][1]*nd[p+p+1].seq[1][k]+tmp[i][2]*nd[p+p+1].seq[2][k]+ tmp[i][3]*nd[p+p+1].seq[3][k]+tmp[i][4]*nd[p+p+1].seq[4][k]+tmp[i][5]*nd[p+p+1].seq[5][k])%mod; } rep(i,0,6) rep(j,0,6) if (nd[p+p].seq[i][j]&&nd[p+p+1].pre[j]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p].seq[i][j]); } rep(i,0,6) rep(j,0,6) if (nd[p+p+1].seq[i][j]&&nd[p+p].pre[i]>n) { tmp2[i][j]=(tmp2[i][j]+nd[p+p+1].seq[i][j]); } rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=tmp2[i][j]%mod; rep(i,0,7) rep(j,0,7) { nd[p].mat[i][j]=(nd[p+p].mat[i][0]*nd[p+p+1].mat[0][j]+nd[p+p].mat[i][1]*nd[p+p+1].mat[1][j]+ nd[p+p].mat[i][2]*nd[p+p+1].mat[2][j]+nd[p+p].mat[i][3]*nd[p+p+1].mat[3][j]+ nd[p+p].mat[i][4]*nd[p+p+1].mat[4][j]+nd[p+p].mat[i][5]*nd[p+p+1].mat[5][j]+ nd[p+p].mat[i][6]*nd[p+p+1].mat[6][j])%mod; } } void build(int p,int l,int r) { if (l==r) { rep(i,0,6) nd[p].pre[i]=n+1,nd[p].suf[i]=0; rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=0; rep(i,0,7) rep(j,0,7) nd[p].mat[i][j]=(i==j); rep(i,0,7) nd[p].mat[i][a[l]]=1; nd[p].seq[a[l]][a[l]]=1; nd[p].suf[a[l]]=l; nd[p].pre[a[l]]=l; } else { int md=(l+r)>>1; build(p+p,l,md); build(p+p+1,md+1,r); upd(p); } } void change(int p,int l,int r,int x,int v) { if (l==r) { a[x]=v; rep(i,0,6) nd[p].pre[i]=n+1,nd[p].suf[i]=0; rep(i,0,6) rep(j,0,6) nd[p].seq[i][j]=0; rep(i,0,7) rep(j,0,7) nd[p].mat[i][j]=(i==j); rep(i,0,7) nd[p].mat[i][a[x]]=1; nd[p].seq[a[x]][a[x]]=1; nd[p].suf[a[x]]=x; nd[p].pre[a[x]]=x; } else { int md=(l+r)>>1; if (x<=md) change(p+p,l,md,x,v); else change(p+p+1,md+1,r,x,v); upd(p); } } int query() { int ans1=0; int ans2=0; rep(i,0,6) ans1=(ans1+nd[1].mat[6][i])%mod; rep(i,0,6) rep(j,0,6) ans2=(ans2+nd[1].seq[i][j])%mod; ans1=(ans1+mod-ans2)%mod; return ans1; } int main() { scanf("%d%d",&n,&q); scanf("%s",s+1); rep(i,1,n+1) a[i]=s[i]-'a'; build(1,1,n); printf("%d\n",query()); rep(i,0,q) { int x; scanf("%d%s",&x,t); change(1,1,n,x,t[0]-'a'); printf("%d\n",query()); } } |