Codeforces Round #316 (Div. 2) C. Replacement(线段树)
2 seconds
256 megabytes
standard input
standard output
Daniel has a string s, consisting of lowercase English letters and period signs (characters '.').
Let's define the operation of replacementas the following sequence of steps: find a substring ".."
(two consecutive periods) in string s, of all occurrences of the substring let's choose the first one, and replace this substring
with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains
no two consecutive periods, then nothing happens.
Let's define f(s) as the minimum number of operations of replacement to
perform, so that the string does not have any two consecutive periods left.
You need to process m queries, the i-th
results in that the character at position xi (1 ≤ xi ≤ n)
of string s is assigned value ci.
After each operation you have to calculate and output the value of f(s).
Help Daniel to process all queries.
The first line contains two integers n and m (1 ≤ n, m ≤ 300 000)
the length of the string and the number of queries.
The second line contains string s, consisting of n lowercase
English letters and period signs.
The following m lines contain the descriptions of queries. The i-th
line contains integer xi and ci (1 ≤ xi ≤ n, ci —
a lowercas English letter or a period sign), describing the query of assigning symbol ci to
position xi.
Print m numbers, one per line, the i-th
of these numbers must be equal to the value of f(s) after performing the i-th
assignment.
10 3
.b..bz....
1 h
3 c
9 f
4
3
1
4 4
.cc.
2 .
3 .
2 a
1 a
1
3
1
1
Note to the first sample test (replaced periods are enclosed in square brackets).
The original string is ".b..bz....".
- after the first query f(hb..bz....) =
4 ("hb[..]bz...." → "hb.bz[..].." → "hb.bz[..]." → "hb.bz[..]" → "hb.bz.") - after the second query f(hbс.bz....) =
3 ("hbс.bz[..].." → "hbс.bz[..]." → "hbс.bz[..]" → "hbс.bz.") - after the third query f(hbс.bz..f.) =
1 ("hbс.bz[..]f." → "hbс.bz.f.")
Note to the second sample test.
The original string is ".cc.".
- after the first query: f(..c.) =
1 ("[..]c." → ".c.") - after the second query: f(....) =
3 ("[..].." → "[..]." → "[..]" → ".") - after the third query: f(.a..) =
1 (".a[..]" → ".a.") - after the fourth query: f(aa..) =
1 ("aa[..]" → "aa.")
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define lc idx<<1
#define rc idx<<1|1
#define lson l,mid,lc
#define rson mid+1,r,rc
#define N 300010 using namespace std;
int n,m;
char s[N];
struct node {
bool ok; ///整段是否为‘*’
bool ls,rs; ///左右端点是否为‘*’
int num;
} tree[N<<2]; void push_up(int idx,int l,int r) {
tree[idx].ok=tree[lc].ok&&tree[rc].ok;
if(tree[idx].ok) {
tree[idx].num=r-l;
tree[idx].ls=tree[idx].rs=1;
} else {
tree[idx].num=tree[lc].num+tree[rc].num;
if(tree[lc].rs&&tree[rc].ls)
tree[idx].num++;
tree[idx].ls=tree[lc].ls;
tree[idx].rs=tree[rc].rs;
}
} void build(int l,int r,int idx) {
if(l==r) {
tree[idx].num=0;
if(s[l]=='.') {
tree[idx].ls=tree[idx].rs=1;
tree[idx].ok=1;
} else {
tree[idx].ls=tree[idx].rs=0;
tree[idx].ok=0;
}
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
push_up(idx,l,r);
} void update(int l,int r,int idx,int pos) {
if(l==r) {
if(s[l]=='.') {
tree[idx].ls=tree[idx].rs=1;
tree[idx].ok=1;
} else {
tree[idx].ls=tree[idx].rs=0;
tree[idx].ok=0;
}
return ;
}
int mid=(l+r)>>1;
if(pos<=mid) {
update(lson,pos);
} else {
update(rson,pos);
}
push_up(idx,l,r);
} int main() {
//freopen("test.in","r",stdin);
while(~scanf("%d%d",&n,&m)) {
scanf("%s",s+1);
build(1,n,1);
char c[2];
int pos;
while(m--) {
scanf("%d%s",&pos,c);
if(c[0]=='.'&&s[pos]=='.') {
printf("%d\n",tree[1].num);
continue;
}
if(c[0]!='.'&&s[pos]!='.') {
printf("%d\n",tree[1].num);
s[pos]=c[0];
continue;
}
s[pos]=c[0];
update(1,n,1,pos);
printf("%d\n",tree[1].num);
}
}
return 0;
}
Codeforces Round #316 (Div. 2) C. Replacement(线段树)的更多相关文章
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #765 Div.1 F. Souvenirs 线段树
题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)
Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...
- Codeforces Codeforces Round #316 (Div. 2) C. Replacement set
C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...
- Codeforces Round #316 (Div. 2) C. Replacement
题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
随机推荐
- css中常见几种float方式以及倒计时(刷新页面不清)
css中常见几种float方式 http://jingyan.baidu.com/article/72ee561a670269e16138dfd5.html <script type=" ...
- 微信服务号获取openId流程(订阅号)
微信公众平台官网:https://mp.weixin.qq.com/ 微信测试开发平台官网:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandb ...
- PHP开发心得一
1,php获得服务器时间 $time= date('Y-m-d H:i'); echo $time; 一般写法如上,但发现打印出来的时间小时数总数不对,和机器的时间差几个小时.查资料发现,要设定时区. ...
- dutacm.club_1085_Water Problem_(矩阵快速幂)
1085: Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Othe ...
- Exception Information
https://developer.apple.com/library/content/technotes/tn2004/tn2123.html Exception Information The t ...
- PHP 之QQ第三方登录
一.下载QQ SDK 下载地址:http://wiki.open.qq.com/wiki/mobile/SDK 二.配置SDK 三.具体代码 login.html <!DOCTYPE html& ...
- CAD使用SetxDataString写数据(网页版)
主要用到函数说明: MxDrawEntity::SetxDataString 写一个字符串扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 ...
- RabbitMQ系列(六)--面试官问为什么要使用MQ,应该怎么回答
如果简历中有写到使用过RabbitMQ或者其他的消息中间件,可能在MQ方面的第一个问题就是问:为什么要使用MQ 面试官期望的回答 1.项目中有什么业务场景需要用到MQ 2.但是用了MQ,会带来很多问题 ...
- db2 in out inout
总结: in 给存储过程传的参数,传一个确定的值 out 传的时候传一个非确定的变量v或者“?” .在存储过程里面计算一个结果赋给v,这个变量v在存储过程执行完之后就是一个确定的值了. inou ...
- vue中路由
关于每次点击链接都要刷新页面的问题众所周知,开发单页应用就是因为那丝般顺滑的体验效果,如果每次点击都会刷新页面… 出现这个的原因是因为使用了window.location来跳转,只需要使用使用rout ...