题意:
给你一个串,每次将区间都修改为某一个字母,问最终包含多少个 F e i M a FeiMa FeiMa子序列。
思路:
首先暴力修改肯定是不行的,复杂度
n
q
nq
nq。
如果没有修改操作,有一个显然的
d
p
dp
dp方程,
d
p
[
i
]
[
j
]
dp[i][j]
dp[i][j]表示到了第
i
i
i个,
j
j
j对应
F
e
i
M
a
FeiMa
FeiMa某个子序列的数量,考虑用线段树优化这个
d
p
dp
dp。
定义
d
p
[
u
]
[
l
]
[
r
]
dp[u][l][r]
dp[u][l][r]表示到了线段树第
u
u
u个子树包含
F
e
i
M
a
FeiMa
FeiMa的
[
l
,
r
]
[l,r]
[l,r]序列的个数,答案即为
d
p
[
1
]
[
0
]
[
4
]
dp[1][0][4]
dp[1][0][4]。转移方程也比较明显了:
d
p
[
u
]
[
l
]
[
r
]
=
d
p
[
u
<
<
1
]
[
l
]
[
r
]
+
d
p
[
u
<
<
1
∣
1
]
[
l
]
[
r
]
+
∑
k
=
l
r
−
1
(
d
p
[
u
<
<
1
]
[
l
]
[
k
]
+
d
p
[
u
<
<
1
∣
1
]
[
k
+
1
]
[
r
]
)
dp[u][l][r]=dp[u<<1][l][r]+dp[u<<1|1][l][r]+\sum _{k=l} ^{r-1}(dp[u<<1][l][k]+dp[u<<1|1][k+1][r])
dp[u][l][r]=dp[u<<1][l][r]+dp[u<<1∣1][l][r]+k=l∑r−1(dp[u<<1][l][k]+dp[u<<1∣1][k+1][r])
这个直接在
p
u
s
h
u
p
pushup
pushup中维护即可。
对于区间修改,我们维护一个懒标记即可,修改的时候(如果是
F
e
i
M
a
FeiMa
FeiMa的某个字母的话)直接让
d
p
[
u
]
[
x
]
[
x
]
=
L
e
n
(
u
)
dp[u][x][x]=Len(u)
dp[u][x][x]=Len(u)即可。
// Problem: ★★飞马祝福语★★
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/16520/D
// Memory Limit: 524288 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=100010,mod=998244353,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,q;
char s[N];
string has="FeiMa";
struct Node {
int l,r;
int lazy;
LL dp[5][5];
}tr[N<<2];
int get(char ch) {
if(ch=='F') return 0;
else if(ch=='e') return 1;
else if(ch=='i') return 2;
else if(ch=='M') return 3;
else if(ch=='a') return 4;
else return 5;
}
void calc(int u,int x) {
for(int i=0;i<5;i++) for(int j=0;j<5;j++) tr[u].dp[i][j]=0;
if(x<5) tr[u].dp[x][x]=(tr[u].r-tr[u].l+1)%mod;
}
void pushup(int u) {
for(int l=0;l<5;l++) {
for(int r=l;r<5;r++) {
tr[u].dp[l][r]=(tr[L].dp[l][r]+tr[R].dp[l][r])%mod;
for(int k=l;k<=r-1;k++)
tr[u].dp[l][r]+=tr[L].dp[l][k]*tr[R].dp[k+1][r]%mod,tr[u].dp[l][r]%=mod;
}
}
}
void pushdown(int u) {
if(tr[u].lazy==-1) return;
tr[L].lazy=tr[R].lazy=tr[u].lazy;
calc(L,tr[u].lazy); calc(R,tr[u].lazy);
tr[u].lazy=-1;
}
void build(int u,int l,int r) {
tr[u]={l,r,-1};
if(l==r) {
calc(u,get(s[l]));
return;
}
build(L,l,Mid); build(R,Mid+1,r);
pushup(u);
}
void change(int u,int l,int r,int x) {
if(tr[u].l>=l&&tr[u].r<=r) {
tr[u].lazy=x;
calc(u,x);
return;
}
pushdown(u);
if(l<=Mid) change(L,l,r,x);
if(r>Mid) change(R,l,r,x);
pushup(u);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--) {
scanf("%d%d%s",&n,&q,s+1);
build(1,1,n);
while(q--) {
int l,r;
char ch;
scanf("%d%d %c",&l,&r,&ch);
change(1,l,r,get(ch));
printf("%lld\n",tr[1].dp[0][4]%mod);
}
}
return 0;
}
/*
*/