题目:Click here

题意:看一下题目下面的Note就会明白的。

分析:一开始想的麻烦了,用了树状数组(第一次用)优化,可惜没用。

直接判断:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int M = 3e5+; int n, m;
char str[M];
int main() {
while( ~scanf("%d %d", &n, &m ) ) {
scanf("%s", str );
int len = strlen( str );
int ans = ;
for( int i=; i<len; i++ )
if( str[i]==str[i-] && str[i]=='.' )
ans++;
for( int i=; i<m; i++ ) {
int pos; char s;
scanf("%d %c", &pos, &s );
pos--;
if( str[pos]=='.' && s!='.' ) {
if( pos> && str[pos-]=='.' ) ans--;
if( pos<n- && str[pos+]=='.' ) ans--;
}
else if( str[pos]!='.' && s=='.' ) {
if( pos> && str[pos-]=='.' ) ans++;
if( pos<n- && str[pos+]=='.' ) ans++;
}
str[pos] = s;
printf("%d\n", ans );
}
}
return ;
}

树状数组优化(上面的方法更快):

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int M = 3e5+; int n, m;
char str[M];
int C[M];
int query( int x ) {
int ret = ;
while( x > ) {
ret += C[x];
x -= x&-x;
}
return ret;
}
void update( int x, int y ) {
while( x <= n ) {
C[x] += y;
x += x&-x;
}
}
int main() {
while( ~scanf("%d %d", &n, &m ) ) {
scanf("%s", str );
int len = strlen( str );
memset( C, , sizeof(C) );
for( int i=; i<len; i++ )
if( str[i]==str[i-] && str[i]=='.' )
update( i, );
for( int i=; i<m; i++ ) {
int pos; char s;
scanf("%d %c", &pos, &s );
pos--;
if( str[pos]=='.' && s!='.' ) {
if( pos> && str[pos-]=='.' ) update( pos, - );
if( pos<n- && str[pos+]=='.' ) update( pos+, - );
}
else if( str[pos]!='.' && s=='.' ) {
if( pos> && str[pos-]=='.' ) update( pos, );
if( pos<n- && str[pos+]=='.' ) update( pos+, );
}
str[pos] = s;
printf("%d\n", query( len- ) );
}
}
return ;
}

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

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

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

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

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

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

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

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

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

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

  6. Codeforces Round #316 (Div. 2)

    A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  7. Codeforces Round #316 (Div. 2) C 思路/模拟

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

  8. Codeforces Round #316 (Div. 2) (ABC题)

    A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...

  9. Codeforces Round #316 (Div. 2) B. Simple Game

    思路:把n分成[1,n/2],[n/2+1,n],假设m在左区间.a=m+1,假设m在右区间,a=m-1.可是我居然忘了处理1,1这个特殊数据.被人hack了. 总结:下次一定要注意了,提交前一定要看 ...

随机推荐

  1. (转)C++笔记:面向对象编程基础

    非常棒的总结 http://blog.csdn.net/liufei_learning/article/details/21312701 面向对象编程基础 面向对象编程基于三个基本概念: 数据抽象-类 ...

  2. Java I/O theory in system level

    参考文章: JAVA NIO之浅谈内存映射文件原理与DirectMemory Java NIO 2.0 : Memory-Mapped Files | MappedByteBuffer Tutoria ...

  3. 有关extern的用法

    1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言, C++保留了一部分过程式 ...

  4. ios中的银联支付

    场景 随着移动互联网的迅猛发展,移动互联已经深深地融入我们的生活.其中,支付方式也是我们生活中经常遇到的情况.所以,在我们的应用中加入支付功能是多么的重要.现在主流的支付接口,一是支付宝类的,一是银联 ...

  5. Ubuntu下lamp(PHP+Mysql+Apache)搭建+完全卸载卸载方法

    安装apache2 sudo apt-get install apache2 安装完成,运行如下命令重启下: sudo /etc/init.d/apache2 restart 在浏览器里输入http: ...

  6. 送给刚刚開始学cocos2d-x引擎 移植Android的同学

    刚刚開始学cocos2-x,不过依照教程把已经安了一般Android的开发环境的eclipse又一次升级到安装好cdt和ndk就花了我几十小时,差点都要放弃了. 參考博客 http://blog.cs ...

  7. zoj 1453 Stripies

    /* 根据题意:不难看出,要是整个方程式最小,那么应该大的数先结合,小的数后结合.先排序然后结合(贪心) */ #include<stdio.h> #include<stdlib.h ...

  8. 面试题之请写出用于校验 HTML 文本框中输入的内容全部为数字 的 javascript 代码

    <input type="text" id="d1" onblur=" chkNumber(this)"/> <scrip ...

  9. POJ 1797 Heavy Transportation 最短路变形(dijkstra算法)

    题目:click here 题意: 有n个城市,m条道路,在每条道路上有一个承载量,现在要求从1到n城市最大承载量,而最大承载量就是从城市1到城市n所有通路上的最大承载量.分析: 其实这个求最大边可以 ...

  10. ZOJ 3430 Detect the Virus(AC自动机)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...