首页 > 其他分享 > 01? Queries

01? Queries

时间:2022-09-30 18:22:34浏览次数:36  
标签:10 01 leq 110 Queries query strings dp

Problem Statement

You are given a string $S$ of length $N$ consisting of 0, 1, and ?.

You are also given $Q$ queries $(x_1, c_1), (x_2, c_2), \ldots, (x_Q, c_Q)$.
For each $i = 1, 2, \ldots, Q$, $x_i$ is an integer satisfying $1 \leq x_i \leq N$ and $c_i$ is one of the characters 0 , 1, and ?.

For $i = 1, 2, \ldots, Q$ in this order, do the following process for the query $(x_i, c_i)$.

  1. First, change the $x_i$-th character from the beginning of $S$ to $c_i$.
  2. Then, print the number of non-empty strings, modulo $998244353$, that can be obtained as a (not necessarily contiguous) subsequence of $S$ after replacing each occurrence of ? in $S$ with 0 or 1 independently.

Constraints

  • $1 \leq N, Q \leq 10^5$
  • $N$ and $Q$ are integers.
  • $S$ is a string of length $N$ consisting of 0, 1, and ?.
  • $1 \leq x_i \leq N$
  • $c_i$ is one of the characters 0 , 1, and ?.

Input

Input is given from Standard Input in the following format:

$N$ $Q$
$S$
$x_1$ $c_1$
$x_2$ $c_2$
$\vdots$
$x_Q$ $c_Q$

Output

Print $Q$ lines. For each $i = 1, 2, \ldots, Q$, the $i$-th line should contain the answer to the $i$-th query $(x_i, c_i)$ (that is, the number of strings modulo $998244353$ at the step 2. in the statement).


Sample Input 1

3 3
100
2 1
2 ?
3 ?

Sample Output 1

5
7
10
  • The $1$-st query starts by changing $S$ to 110. Five strings can be obtained as a subsequence of $S = $ 110: 0, 1, 10, 11, 110. Thus, the $1$-st query should be answered by $5$.

  • The $2$-nd query starts by changing $S$ to 1?0. Two strings can be obtained by the ? in $S = $ 1?0: 100 and 110. Seven strings can be obtained as a subsequence of one of these strings: 0, 1, 00, 10, 11, 100, 110. Thus, the $2$-nd query should be answered by $7$.

  • The $3$-rd query starts by changing $S$ to 1??. Four strings can be obtained by the ?'s in $S = $ 1??: 100, 101, 110, 111. Ten strings can be obtained as a subsequence of one of these strings: 0, 1, 00, 01, 10, 11, 100, 101, 110, 111. Thus, the $3$-rd query should be answered by $10$.


Sample Input 2

40 10
011?0??001??10?0??0?0?1?11?1?00?11??0?01
5 0
2 ?
30 ?
7 1
11 1
3 1
25 1
40 0
12 1
18 1

Sample Output 2

746884092
532460539
299568633
541985786
217532539
217532539
217532539
573323772
483176957
236273405

Be sure to print the count modulo $998244353$.

如果这个问题不是动态的,那要怎么做?想到dp做法。
定义 \(dp_{i,0/1}\) 为在前 \(i\) 个字符的所有子序列中,如果再加上 \(0/1\) 这个字符后,就不是前 \(i\) 个字符的子序列了的子序列个数。

那么如果遇到一个 \(1\),那么就相当于给所有加上 \(1\) 不属于前 \(i\) 个数的子序列加上了一个 \(1\),\(dp_{i,1}=dp_{i-1,1}\),然后新生成的这些子序列肯定再加上 \(0\) 后不属于前面的子序列,\(dp_{i,0}=dp_{i-1,1}+dp_{i-1,0}\).

如果遇到一个 \(0\) ,同理。遇到一个问好,\(dp_{i,1}=dp_{i,0}=dp_{i-1,1}+dp_{i-1,0}\)。

另开一个变量统计答案就可以了。

然后就要开始动态 dp,设 \((dp_0,dp_1,ans)\)为一个向量
遇到一个\(1\),向量乘上 \(\begin{Bmatrix}1&0&0\\1&1&1\\0&0&1 \end{Bmatrix}\)

遇到一个\(0\),向量乘上 \(\begin{Bmatrix}1&1&1\\0&1&0\\0&0&1 \end{Bmatrix}\)

遇到一个\(?\),向量乘上 \(\begin{Bmatrix}1&1&1\\1&1&1\\0&0&1 \end{Bmatrix}\)

剩下的就是用线段树维护矩阵乘法,单点修改,区间查询就可以了。

#include<bits/stdc++.h>
const int N=1e5+5,P=998244353;
int n,q,x;
char c;
struct matrix{
	int a[4][4];
}t[3],tr[N<<2],p,dw;
matrix cheng(matrix a,matrix b)
{
	matrix c;
	memset(c.a,0,sizeof(c.a));
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			for(int k=1;k<=3;k++)
				c.a[i][j]+=1LL*a.a[i][k]*b.a[k][j]%P,c.a[i][j]%=P;
	return c;
}
int turn(char c)
{
	if(c<='1')
		return c-'0';
	return 2;
}
void copy(matrix&a,matrix b)
{
	for(int i=1;i<=3;i++)
		for(int j=1;j<=3;j++)
			a.a[i][j]=b.a[i][j];
}
void build(int o,int l,int r)
{
//	printf("%d %d %d\n",o,l,r);
	if(l>r)	
		return;
	if(l==r)
	{
		scanf("  %c",&c);
		copy(tr[o],t[turn(c)]);
		return;
	}
	int md=l+r>>1;
	build(o<<1,l,md);
	build(o<<1|1,md+1,r);
	copy(tr[o],cheng(tr[o<<1],tr[o<<1|1]));
}
void update(int o,int l,int r,int x,int y)
{
	if(l==r)
	{
		copy(tr[o],t[y]);
		return;
	}
	int md=l+r>>1;
	if(md>=x)
		update(o<<1,l,md,x,y);
	else
		update(o<<1|1,md+1,r,x,y);
	copy(tr[o],cheng(tr[o<<1],tr[o<<1|1]));
}
int main()
{
	scanf("%d%d",&n,&q);
	p.a[1][1]=p.a[1][2]=1;
	dw.a[1][1]=dw.a[2][2]=dw.a[3][3]=1;
	for(int i=0;i<(N<<2);i++)
		copy(tr[i],dw);
	t[0].a[1][1]=t[0].a[2][1]=t[0].a[2][2]=t[0].a[2][3]=t[0].a[3][3]=1;
	t[1].a[1][1]=t[1].a[1][2]=t[1].a[1][3]=t[1].a[2][2]=t[1].a[3][3]=1;
	t[2].a[1][1]=t[2].a[1][2]=t[2].a[1][3]=t[2].a[2][1]=t[2].a[2][2]=t[2].a[2][3]=t[2].a[3][3]=1;
	build(1,1,n);
	while(q--)
	{
		scanf("%d %c",&x,&c);
		update(1,1,n,x,turn(c));
		printf("%d\n",cheng(p,tr[1]).a[1][3]);
	}
}

标签:10,01,leq,110,Queries,query,strings,dp
From: https://www.cnblogs.com/mekoszc/p/16745662.html

相关文章

  • 题解 [POI2010]ZAB-Frog
    很厉害的题。倍增和单调队列。这是zpl新手向算法第二弹,第一弹可以看小挖的核燃料填充我会尽量讲的比较细致。同第一弹,尽量配合代码食用。这道题的题目描述写的不是......
  • 警告⚠️Exchange 2013/2016/2019在野可利用的0 Day漏洞
       2022年9月29日第三方网站批量Exchange2013/2016/2019存在一个在野0Day漏洞,该漏洞目前已经得到微软证实,确实存在,并且CVE评分为8.8。该漏洞目前还没有修复补丁,并且......
  • postman 自动重定向地址问题, 301 Moved Permanently
    最近公司在对接一家英国的服务商接口地址为:https://XXX.app/API?testMode=1在对接这家公司的api接口的时候遇到了一点问题,甚是头疼,现在就把经历记录下来当我在调试......
  • IPW60R017C7英飞凌MOS管,ASEMI代销
    编辑-ZIPW60R017C7英飞凌MOS管参数:型号:IPW60R017C7连续漏极电流(ID):109A功耗(Ptot):446W贮存温度和工作结温:-55~150℃漏源击穿电压V(BR)DSS:600V栅极阈值电压V(GS)th:4V零栅极电......
  • #yyds干货盘点# 面试必刷TOP101:编辑距离(一)
    1.简述:描述给定两个字符串str1和str2,请你算出将str1转为str2的最少操作数。你可以对字符串进行3种操作:1.插入一个字符2.删除一个字符3.修改一个字符。字符串长度满......
  • Windows Server 服务器漏洞:OpenSSL 信息泄露漏洞(CVE-2016-2183)和 OpenSSL弱加密算法
    一、更新openssl版本这个漏洞我目前了解到是直接使用系统自带版本,版本过低引起的弱加密信息泄露,直接更新。更新会同时把标题两个漏洞都补上先下载一波安装包: http://sl......
  • #yyds干货盘点# 面试必刷TOP101:把数字翻译成字符串
    1.简述:描述有一种将字母编码成数字的方式:'a'->1,'b->2',...,'z->26'。现在给一串数字,返回有多少种可能的译码结果数据范围:字符串长度满足 进阶:空间复杂度 ,时间复杂度......
  • 代码大全2 阅读笔记01
    第七章:高质量的子程序1、为什么要创建子程序?      提高程序的可读性,减少以及隔离程序复杂度,提高代码复用率,在代码变更时减少带来的影响(功能变更,变更导致的测试),可......
  • 程序员修炼之道+从小工到专家阅读笔记01
    作者在第一章讲述了注重实效应有的思想,如何成为一个注重实效的程序员。注重实效的程序员会有考虑大局的思想,他们谦虚敢于承认错误,敢于负责任。作者用一句“我的源码让猫给......
  • EN 892:2012+A1:2016亚马逊登山绳安全要求
    攀岩绳是与攀岩安全带和锚点(例如:墙壁、岩壁或山壁)相连的一件器械。攀岩绳用于防止攀岩者坠落。攀岩绳通常由长捻纤维芯和编织彩色纤维外护套组成。产品示例亚马逊美国站安......