C. Replacement
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/570/problem/C

Description

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 Input

10 3
.b..bz....
1 h
3 c
9 f

Sample Output

4
3
1

HINT

题意

给你一个字符串,然后每两个点可以变成一个点

然后有m次修改操作,每次可以修改一个位置的字符

然后问你修改之后,需要多少次操作,把所有的点,都变成连续的一个点

题解

我比较蠢,我用的线段树,太蠢了

正解不超过30行,几个if就好了……

维护的是每一个pos的左边的字母和右边的字母位置

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std; #define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=; struct node
{
int lft,rht;
int lmx,rmx;
int len(){return rht-lft+;}
int mid(){return MID(lft,rht);}
void init(){lmx=rmx=len();}
void fun(int valu)
{
if(valu==-) lmx=rmx=;
else lmx=rmx=;
}
}; int n,m; struct Segtree
{
node tree[N*];
void up(int ind)
{
tree[ind].lmx=tree[LL(ind)].lmx;
tree[ind].rmx=tree[RR(ind)].rmx;
if(tree[LL(ind)].lmx==tree[LL(ind)].len())
tree[ind].lmx+=tree[RR(ind)].lmx;
if(tree[RR(ind)].rmx==tree[RR(ind)].len())
tree[ind].rmx+=tree[LL(ind)].rmx;
}
void build(int lft,int rht,int ind)
{
tree[ind].lft=lft,tree[ind].rht=rht;
tree[ind].init();
if(lft!=rht)
{
int mid=tree[ind].mid();
build(lft,mid,LL(ind));
build(mid+,rht,RR(ind));
}
}
void updata(int pos,int ind,int valu)
{
if(tree[ind].lft==tree[ind].rht) tree[ind].fun(valu);
else
{
int mid=tree[ind].mid();
if(pos<=mid) updata(pos,LL(ind),valu);
else updata(pos,RR(ind),valu);
up(ind);
}
}
void query(int pos,int ind,int& x,int& y)
{
if(tree[ind].lft==tree[ind].rht)
{
if(tree[ind].lmx==) x=y=tree[ind].lft;
else x=y=;
}
else
{
int mid=tree[ind].mid();
if(pos<=mid) query(pos,LL(ind),x,y);
else query(pos,RR(ind),x,y);
if(tree[LL(ind)].rht==y) y+=tree[RR(ind)].lmx;
if(tree[RR(ind)].lft==x) x-=tree[LL(ind)].rmx;
}
}
}seg;
char s[N];
int vis[N];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",s+);
seg.build(,n+,);
int len=strlen(s+);
int ans=;
int tmp=;
seg.updata(n+,,-);
for(int i=;i<=len;i++)
{
if(s[i]=='.')
tmp++;
else
{
if(tmp!=)
ans+=(tmp-);
tmp=;
seg.updata(i,,-);
vis[i]=;
}
}
if(tmp!=)
ans+=(tmp-);
while(m--)
{
char cmd[];
int pos,st,ed;
scanf("%d",&pos);
scanf("%s",&cmd);
if(cmd[]!='.')
{
if(vis[pos]==)
printf("%d\n",ans);
else
{
vis[pos]=; seg.query(pos,,st,ed);
ans-=(ed-st);
seg.updata(pos,,-);
seg.query(pos+,,st,ed);
ans+=(ed-st);
if(pos-!=)
{
seg.query(pos-,,st,ed);
ans+=(ed-st);
}
printf("%d\n",ans);
} }
else
{
if(vis[pos]==)
printf("%d\n",ans);
else
{
vis[pos]=;
seg.query(pos+,,st,ed);
ans-=(ed-st);
if(pos-!=)
{
seg.query(pos-,,st,ed);
ans-=(ed-st);
}
seg.updata(pos,,);
seg.query(pos,,st,ed);
ans+=(ed-st);
printf("%d\n",ans); }
}
}
}
return ;
}

Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

    C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Codeforces Round #316 (Div. 2C) 570C Replacement

    题目:Click here 题意:看一下题目下面的Note就会明白的. 分析:一开始想的麻烦了,用了树状数组(第一次用)优化,可惜没用. 直接判断: #include <bits/stdc++. ...

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

    先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++. ...

随机推荐

  1. 强制将IE8设置为IE7兼容模式来解析网页

    强制将IE8设置为IE7兼容模式来解析网页 英文原文:http://msdn.microsoft.com/en-us/library/cc288325(VS.85).aspx 文件兼容性用于定义让IE ...

  2. 在英文 sql2005中 比较nvarchar 与 varchar的速度

    declare @str1 varchar(max); declare @count int; ; print 'begin' begin set @str1 = @str1 + '*'; ; end ...

  3. java web 学习十(HttpServletRequest对象1)

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  4. TabHost Tab的添加和删除

    TabHost 添加Tab项: tabhost = this.getTabHost(); TabSpec tabSpec = tabhost.newTabSpec("news"); ...

  5. codeforces 678D Iterated Linear Function 矩阵快速幂

    矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵  { g[n] }    =  {A   B}  * { g[n-1]} {   1   }         {0   1 ...

  6. 常用sql命令

    --1) 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话 CREATE TABLE student (     [id] [int] IDENTITY(1,1) NOT NU ...

  7. NiuTrans 日记 1

    这些天把东北大学自然语言实验室的NiuTrans 系统搭建并按照例子将短语系统运行了一遍,写这个日记主要是为了以后能提醒自己在这其中遇到的问题. 环境:短语系统我是windows和linux都运行了, ...

  8. [Web API] 如何让 Web API 统一回传格式以及例外处理[转]

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...

  9. Libsvm的MATLAB调用和交叉验证

    今天听了一个师兄的讲课,才发现我一直在科研上特别差劲,主要表现在以下几个方面,(现在提出也为了督促自己在以后的学习工作道路上能够避免这些问题) 1.做事情总是有头无尾,致使知识点不能一次搞透,每次在用 ...

  10. CentsOS7 网络自动启动

    虚拟机中安装完成CentOS7后,网络总是需要手工启动才可使用,设置为自动连接的方式如下: cd /etc/sysconfig/network-scripts/ls #找到类似的文件:ifcfg-et ...