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自动化--语言基础五面向对象、迭代器、range和切片的区分

    面向对象 一.面向对象简单介绍: class Test(): #类的定义 car = "buick" #类变量,定义在类里方法外,可被对象直接调用,具有全局效果 def __ini ...

  2. C#.NET,技巧篇(DataGridView线程操作)

    这个系列的文章,主要是平时做C#.NET(Framework 3.5)开发的时候,积累的经验和技巧.我们平时总有这样的体会,遇到一个特别难解决的问题,网上寻它千百度也没能搜索到有用的信息.这时你肯定会 ...

  3. spark查看stage和tasks信息

    spark提供了web-ui接口.外部命令等多种方法监视spark程序的执行状态.利用spark的监视功能,可以方便的查看spark应用程序执行的状态,具体包括:1)stage和tasks列表信息  ...

  4. 并发编程学习笔记(15)----Executor框架的使用

    Executor执行已提交的 Runnable 任务的对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方法.通常使用 Executor 而不是显式地创建 ...

  5. 并发编程学习笔记(10)----并发工具类CyclicBarrier、Semaphore和Exchanger类的使用和原理

    在jdk中,为并发编程提供了CyclicBarrier(栅栏),CountDownLatch(闭锁),Semaphore(信号量),Exchanger(数据交换)等工具类,我们在前面的学习中已经学习并 ...

  6. HDU多校Round 5

    Solved:3 rank:71 E. Everything Has Changed #include <bits/stdc++.h> using namespace std; const ...

  7. ArrayList经典Demo

    import java.util.ArrayList; import java.util.Iterator; public class ArrayListDemo { public static vo ...

  8. 扒一扒PROMISE的原理,大家不要怕!

    在前端的日常工作中,回调函数(callback)应该是见怪不怪了,但是当回调函数遇上了异步(async),这就令人发指了.那么异步是什么意思呢,简单地说就是不等你执行完,就先执行下方的代码了. 举个

  9. cc.Node—Action

    1: Action类是动作命令,我们创建Action,然后节点运行action就能够执行Action的动作; 2: Action分为两类: (1) 瞬时就完成的ActionInstant, (2) 要 ...

  10. 母牛的故事(hdoj 2018,动态规划递推,详解)

    有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Sample Input2450Sample Output246 / ...