Codeforces Round #316 (Div. 2C) 570C Replacement
题目: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的更多相关文章
- 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 Codeforces Round #316 (Div. 2) C. Replacement 线段树
C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...
- Codeforces Round #316 (Div. 2) C. Replacement
题意:给定一个字符串,里面有各种小写字母和' . ' ,无论是什么字母,都是一样的,假设遇到' . . ' ,就要合并成一个' .',有m个询问,每次都在字符串某个位置上将原来的字符改成题目给的字符, ...
- Codeforces Round #316 (Div. 2) C. Replacement(线段树)
C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #316 (Div. 2) C Replacement 扫描法
先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++. ...
- Codeforces Round #316 (Div. 2)
A. Elections time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #316 (Div. 2) C 思路/模拟
C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #316 (Div. 2) (ABC题)
A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...
- 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了. 总结:下次一定要注意了,提交前一定要看 ...
随机推荐
- 51nod 1239 欧拉函数之和(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...
- JAVA GUI学习 - JPopupMenu鼠标右键菜单组件学习
public class JPopmenuKnow { public void test() { //为表格添加鼠标右键菜单 JMenuItem jMenuItemFileInfo = new JMe ...
- eclipse svn插件显示作者
在另一台电脑里安装了SVN插件后,发现项目文件后面只有版本号,没有作者名字了,找了很久才找到了,现记录在这里. window->preferences->team->svn-> ...
- OpenCV 开发环境环境搭建(win10+vs2015+opencv 3.0)
OpenCV 3.0 for windows(下载地址:http://opencv.org/): 本测试中,OpenCV安装目录:D:\Program Files\opencv,笔者操作系统为64位. ...
- R语言学习笔记之外部文件读取
在win32位的系统下,RODBC包内的函数是可以直接运行的,但在win64位的系统则不支持! 1.读取外部文件read.table()---csv,txt,excel 最基本函数是read.tabl ...
- 10-C语言函数
目录: 一.函数 二.return与exit关键字 三.递归与递推 回到顶部 一.函数 1 函数由函数名.返回值.形参.函数体组成. 函数的使用分三个步骤:声明.定义.调用 2 语法格式: 返回值类型 ...
- c风格字符串函数
十一.C 风格字符串 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat( ...
- 调不尽的内存泄漏,用不完的Valgrind
调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-X.X.X.tar.bz2 2. 解压安装包:tar –jxvf ...
- Java Socket 入门2 Java与C# Socket 通信
参考http://www.cnblogs.com/cdtarena/p/3184313.html 这里以C#作为服务端 其实不论C#是服务端还是客户端都不是主要问题 毕竟不论客户端还是服务端 都包括 ...
- MVC-Easy-UI-datagrid-分页-查询
时间仓促,代码写的乱,莫怪,着影区不用理会(功能之外) <link href="@Url.Content("~/Content/Site.css")" r ...