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个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
随机推荐
- thinkphp信息修改和分页
关联两个数据表,在Model里建立StuModel.class.php: <?php //Belongs_to 关联表示当前模型从属于另外一个父对象 namespace Admin\Model; ...
- CAD使用SetxDataString写数据(网页版)
主要用到函数说明: MxDrawEntity::SetxDataString 写一个字符串扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 ...
- CAD保存文件为各种格式
<p class="mtext"> 主要用到函数说明:</p><p style="line-height: 0.6;"> & ...
- 梦想CAD控件安卓文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现代码如下: // 增加文字样式 //getCurrentDatabase()返回当前数据库对象 //getTextstyle() ...
- Spring框架系列(九)--MyBatis面试题(转载)
1.什么是Mybatis? 1.Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动.创建 连接.创建statement ...
- JDBC配置MSSQL
使用JDBC连接SQL SERVER 这可能是个很老套的话题,但不管怎么说还是有用的.姑且把配置方法贴出来吧.1. 确认Sql Server的的运行状态打开Sql Server配置管理器,确认Sql ...
- 微信小程序 导航 4种页面跳转 详解
1.wx.navigateTo 保留当前页面,跳转到应用内的某个页面,目前页面路径最多只能十层. 参数:url(可携带参数) .success .fail .complete 可用wxml代替: ...
- buf.readDoubleBE()
buf.readDoubleBE(offset[, noAssert]) buf.readDoubleLE(offset[, noAssert]) offset {Number} 0 <= of ...
- SQL学习笔记:表的约束
目录 NOT NULL约束 INDEX 索引 CHECK 约束 DEFAULT 约束 UNIQUE 约束 PRIMARY KEY 约束 FOREIGN KEY 约束:简单的说,就是创建表的时候,对表或 ...
- [luoguP2434] [SDOI2005]区间(贪心)
传送门 简单贪心 ——代码 #include <cstdio> #include <iostream> #include <algorithm> int n, l, ...