C. Replacement
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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 ≤ nci —
a lowercas English letter or a period sign), describing the query of assigning symbol ci to
position xi.

Output

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.

Sample test(s)
input
10 3
.b..bz....
1 h
3 c
9 f
output
4
3
1
input
4 4
.cc.
2 .
3 .
2 a
1 a
output
1
3
1
1
Note

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(线段树)的更多相关文章

  1. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  2. 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 ...

  3. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  4. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  5. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #316 (Div. 2) C. Replacement

    题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...

随机推荐

  1. Python 快排[pythonnic]

    def QS(array): less = [] more = [] if len(array) <= 1: return array head = array.pop() for x in a ...

  2. Maven对不同的测试环境用不同的参数进行打包

    通过mvn package -P ${env} 加载不同配置文件 1.pom.xml中的配置 filter-dev.properties jdbc.properties

  3. Angular——自定义服务

    基本介绍 之前我们介绍了angular内置的几种服务,这里我们介绍如何自己定义自己的服务,主要是通过三个方法:factory.service.value 基本使用 factory:可以返回对象,也可以 ...

  4. git 分支处理

    git 创建常用(多)分支(如:Master 主分支.Develop 分.Feature 功能分支.Release 预发布分支.Hotfix(或者Fixbug) 分支)步骤1.mkdir 项目名    ...

  5. Java 基础入门随笔(8) JavaSE版——静态static

    面向对象(2) this:代表对象.代表哪个对象呢?当前对象. 当成员变量和局部变量重名,可以用关键字this来区分. this就是所在函数所属对象的引用.(简单说:哪个对象调用了this所在的函数, ...

  6. 梦想Android版CAD控件2018.7.26更新

    下载地址: http://www.mxdraw.com/ndetail_109.html 1. 增加所有接口CHM帮助文档 2. 增加得到当前打开文件函数 3. 读写CAD扩展记录接口 4. 读写属性 ...

  7. 17Web应用乱码问题

    Web应用乱码问题 Web应用乱码问题 简介 每个国家(或区域)都规定了本国家(或地区)计算机信息交换用的字符编码集,如美国的扩展ASCII码, 中国的GB2312-80,日本的JIS 等,作为该国家 ...

  8. 【转】Go语言入门教程(一)Linux下安装Go

    说明 系统是Ubuntu. 关于安装 下载安装包 当前官方下载地址是https://golang.org/dl/,如果不能访问,请自行FQ,FQ是技术工作者的必备技能. 安装 tar -xzvf go ...

  9. NOIP 2006 金明的预算方案(洛谷P1064,动态规划递推,01背包变形,滚动数组)

    一.题目链接:P1064 金明的预算方案 二.思路 1.一共只有五种情况 @1.不买 @2.只买主件 @3.买主件和附件1(如果不存在附件也要运算,只是这时附件的数据是0,也就是算了对标准的结果也没影 ...

  10. Linux之iptables(五、firewall命令及配置)

    firewalld服务 firewalld是CentOS 7.0新推出的管理netfilter的工具 firewalld是配置和监控防火墙规则的系统守护进程.可以实现iptables,ip6table ...