ABC246Ex 01? Queries(动态 DP)

题意

给定长度为 n 的字符串 s,只包含 0,1,?,其中 ? 可以任意替换为 01

再给定 q 次单点修改,修改后查询字符串本质不同的子序列个数,对 998244353 取模。

n,q105

分析

考虑没有修改怎么做。

首先跟 SA 没有任何关系。

fi,0/1 表示考虑前 i 个字符,以 0 或者 1 结尾的子序列个数。

先给转移:

  • si= 0fi,0=fi1,0+fi1,1+1,fi,1=fi1,1
  • si= 1fi,0=fi1,0,fi,1=fi1,0+fi1,1+1
  • si= ?fi,0=fi,1=fi1,0+fi1,1+1

看起来是比较反直觉的。这为什么是对的呢?

si= 0fi,0 的转移为例,其他的转移要么类似要么显然。

fi,0=fi1,0+fi1,1+1,其意义是:给所有原先的合法子序列的末尾添加一个 01 是指再添加一个新的子序列:单独一个 0

考虑这样为什么可以不重不漏的计数。对于原先的一个子序列 s,不难发现将 s 去掉末一位后的子序列仍然是合法的。所以将 s 末尾添加一个 0 后,s 本身可以由原来的 s 去掉末一位再添加一个 0 得到(由于我们统计的是以 0 结尾的子序列,若 s 仍然要被保留在答案里那么其末尾一定是 0)。注意到对所有合法子序列末尾添加 0 后单独一个 0 的子序列就没被算进答案里了,这时候我们要补加上。

现在考虑修改。考虑动态 DP,发现序列是树上的一种特殊形式。不难发现 f 可以矩乘优化的形式,把每次转移写成一个矩阵,那么单点修改实际上就是单点修改每一位的转移矩阵,查询就是求出所有转移矩阵的乘积。

时间复杂度 O(33qlogn)

转移矩阵塞代码里了。

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f,mod=998244353;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q;
struct Matrix{
	int mat[3][3];
	Matrix(){rep(i,0,2)rep(j,0,2)mat[i][j]=0;}
}Z0,Z1,ZZ;
inline Matrix operator*(Matrix x,Matrix y){
	Matrix res;
	rep(i,0,2)rep(j,0,2)rep(k,0,2)res.mat[i][j]=(res.mat[i][j]+1ll*x.mat[i][k]*y.mat[k][j]%mod)%mod;
	return res;
}
char s[maxn];
namespace sgt{
	Matrix d[maxn<<2];
	#define mid ((l+r)>>1)
	inline void pu(int p){
		d[p]=d[lson(p)]*d[rson(p)];
	}
	void bd(int l=1,int r=n,int p=1){
		if(l==r){
			d[p]=s[l]=='?'?ZZ:(s[l]=='0'?Z0:Z1);
			return;
		}
		bd(l,mid,lson(p)),bd(mid+1,r,rson(p));
		pu(p);
	}
	void upd(int x,char c,int l=1,int r=n,int p=1){
		if(l==r){
			d[p]=c=='?'?ZZ:(c=='0'?Z0:Z1);
			return;
		}
		x<=mid?upd(x,c,l,mid,lson(p)):upd(x,c,mid+1,r,rson(p));
		pu(p);
	}
	#undef mid
}un sgt;

inline void solve_the_problem(){
	n=rd(),Q=rd(),scanf("%s",s+1);
	Z0.mat[0][0]=Z0.mat[1][0]=Z0.mat[2][0]=Z0.mat[1][1]=Z0.mat[2][2]=1;
	Z1.mat[0][0]=Z1.mat[1][1]=Z1.mat[2][1]=Z1.mat[0][1]=Z1.mat[2][2]=1;
	ZZ.mat[0][0]=ZZ.mat[1][0]=ZZ.mat[2][0]=ZZ.mat[1][1]=ZZ.mat[2][2]=ZZ.mat[0][1]=ZZ.mat[2][1]=1;
	bd();
	while(Q--){
		int x=rd();char t[1];scanf("%s",t);
		s[x]=t[0],upd(x,s[x]);
		write((d[1].mat[2][0]+d[1].mat[2][1])%mod,10);
	}
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18409043

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示