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个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
随机推荐
- oracle插入字符串数据时,字符串中有'单引号
使用insert into(field1,field2...) values('val1','val2'...)时,若值中有单引号时会报错. 处理方法:判断一下val1,val2中是否含有单引号,若含 ...
- JS高级——弹出框的美化
替换原有的alert方法,window.alert=function(){} https://blog.csdn.net/kirsten_z/article/details/76242286 http ...
- Xcode相关概念:Target、Project、Scheme、Workspace
创建并编译Xcode工程时,有几个常用概念想在这里记一下. Xcode Target: 定义:A target defines a single product; .... 理解:输出文件,等同于VS ...
- (转)Hibernate框架基础——映射普通属性
http://blog.csdn.net/yerenyuan_pku/article/details/52739871 持久化对象与OID 对持久化对象的要求 提供一个无参的构造器.使Hibernat ...
- 梦想CAD控件安卓文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现代码如下: // 增加文字样式 //getCurrentDatabase()返回当前数据库对象 //getTextstyle() ...
- Commons_IO_FileUtils的使用
commos_io.jar包下载地址:http://commons.apache.org/proper/commons-io/download_io.cgi 官方文档地址:http://commons ...
- 牛客多校Round 9
Solved:1 rank:112 E. Music Game 题解说有个非简化的原题 bzoj4318 #include <bits/stdc++.h> using namespace ...
- XGBoost参数中文翻译以及参数调优
XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...
- idea必选配置
参考: IDEA配置
- Missing message for key "xxx" in bundle "(default bundle)" for locale zh_CN
参考文章http://programmerslounge.blogspot.com/2013/03/error-missing-message-for-key.html 错误的struts-confi ...