[ABC246Ex] 01? Queries
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)$.
- First, change the $x_i$-th character from the beginning of $S$ to $c_i$.
- 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$ with0
or1
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
and110
. 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]);
}
}
[ABC246Ex] 01? Queries的更多相关文章
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Profiling MySQL queries from Performance Schema
转自:http://www.percona.com/blog/2015/04/16/profiling-mysql-queries-from-performance-schema/ When opti ...
- 数据结构(线段树):CodeForces 145E Lucky Queries
E. Lucky Queries time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainl ...
- The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...
- DB Query Analyzer 6.01 is released, SQL Execute Schedule function can be used
DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has English versi ...
- HDU6191(01字典树启发式合并)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- 01: docker 基本使用
1.1 docker基础 1.docker与虚拟机比较 2.docker版本 1. 社区版(Community Edition, CE) 2. 企业版(Enterprise Edition, EE) ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries 水题
C. Sonya and Queries 题目连接: http://codeforces.com/contest/714/problem/C Description Today Sonya learn ...
- CSS3 响应式web设计,CSS3 Media Queries
两种方式,一种是直接在link中判断设备的尺寸,然后引用不同的css文件: <link rel="stylesheet" type="text/css" ...
随机推荐
- java与es8实战之四:SpringBoot应用中操作es8(无安全检查)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<java与es8实战>系 ...
- Remix-Ethereum IDE连接本地详解
Remix-Ethereum IDE连接本地 由于在学习和做项目的过程中,很多人用的都是网页版的Remix,而在网页中的代码是存储在缓存中的,在使用过程中容易丢失,所以将Remix与本地文件连接起 ...
- LVS DR模式负载均衡群集部署
LVS DR模式负载均衡群集部署 1 LVS-DR 模式的特点 直接路由直接路由 调节器仅作为客户端的访问入口,节点服务器的响应消息是直接返回客户端的,不需要经过调节器(与NAT模式的区别)节点服务器 ...
- 【uniapp】【微信小程序】wxml-to-canvas
真是搞吐了,研究了整整两天,困死我了 本来使用生成二维码插件好好的,插件页也支持导出二维码图片,可是领导说要带上文件的名称,那就涉及html转图片了,当然也可以改二维码插件的源码,不过源码做了混淆,看 ...
- CAP项目集成带身份和证书验证的MongoDB
大家好,我是Edison. 最近,在使用CAP事件总线时,碰到了这样一个需求:微服务采用的是MongoDB,而且还是带身份验证 和 SSL根证书验证的.由于目前网上能找到的资料,都是不带身份验证的Mo ...
- 关于Async、Await的一些知识点
在ASP.NET Core中,当一个HTTP请求到达服务器时,它会被分配给线程池中的一个线程来处理.该线程会执行相应的Controller方法. 如果这个方法是一个异步方法并且使用了await关键字, ...
- 虹科案例 | Redis企业版数据库帮助金融机构满足客户需求
如今,传统银行与新兴银行正在进行激烈的竞争.随着苹果.亚马逊.谷歌等科技巨头正凭借其数字化.移动应用程序和云体验打入金融服务行业.为了进行公平竞争,传统银行也需要通过个性化的全渠道客户体验来实现交互式 ...
- Redis 6 学习笔记 4 —— 通过秒杀案例,学习并发相关和apache bench的使用,记录遇到的问题
背景 这是某硅谷的redis案例,主要问题是解决计数器和人员记录的事务操作 按照某硅谷的视频敲完之后出现这样乱码加报错的问题 乱码的问题要去tomcat根目录的conf文件夹下修改logging.pr ...
- Kafka基本原理、生产问题总结及性能优化实践
Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实时的处理大量 ...
- alibaba fastjson的JsonObject有序的实现和源码分析
介绍 FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean.在使用的过程中, ...