(线段树 && 字符串的处理)codeforces -- 570C
链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/J
Description
Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as 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 ≤ n, ci — 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
4
3
1
4 4
.cc.
2 .
3 .
2 a
1 a
1
3
1
1
Hint
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.")
找到了规律很水的AC了,但看了题解后知道原来这是个线段树, 两个代码都粘一下
水的代码:
#include<stdio.h>
#include<string.h> #define N 301100 char s[N]; int main()
{
int n, m, sum, z;
while(scanf("%d%d", &n, &m)!=EOF)
{
char ch;
int k, i;
scanf("%s", s); sum=;
for(i=; i<n; i++)
{
z = ;
while(s[i]=='.' && i<n)
{
z++;
i++;
}
if(z)
sum+=z-;
} for(i=; i<=m; i++)
{
scanf("%d %c", &k, &ch); if((s[k-]!='.'&&ch!='.') || (s[k-]=='.'&&ch=='.') )
{
printf("%d\n", sum);
continue;
}
if(ch=='.' && s[k-]!='.')
{
if(s[k]!='.' && s[k-]!='.')
sum = sum;
else if(s[k]=='.' && s[k-]=='.')
sum += ;
else if(s[k]=='.' || s[k-]=='.')
sum += ;
}
if(ch!='.' && s[k-]=='.')
{
if(s[k]=='.' && s[k-]=='.')
sum = sum-;
else if(s[k]!='.' && s[k-]!='.')
sum = sum;
else if(s[k]!='.' || s[k-]!='.')
sum = sum-;
}
s[k-] = ch;
printf("%d\n", sum);
}
}
return ;
}
线段树:
/*************************************************************************
> File Name: C.cpp
> Author: ALex
> Mail: zchao1995@gmail.com
> Created Time: 2015年08月14日 星期五 01时09分43秒
************************************************************************/ #include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm> using namespace std; const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> PLL;
const LL INF = (1LL << ); const int N = ;
struct SegTree {
int l, r;
int lenl, lenr;
int len;
int cnt;
}tree[N << ];
char str[N]; void pushup(int p)
{
tree[p].lenl = tree[p << ].lenl;
if (tree[p].lenl == tree[p << ].r - tree[p << ].l + )
{
tree[p].lenl += tree[p << | ].lenl;
}
tree[p].lenr = tree[p << | ].lenr;
if (tree[p].lenr == tree[p << | ].r - tree[p << | ].l + )
{
tree[p].lenr += tree[p << ].lenr;
}
tree[p].cnt = tree[p << ].cnt + tree[p << | ].cnt;
if (tree[p << ].lenr && tree[p << | ].lenl)
{
--tree[p].cnt;
}
tree[p].len = tree[p << ].len + tree[p << | ].len;
} void build(int p, int l, int r)
{
tree[p].l = l;
tree[p].r = r;
if (l == r) {
tree[p].cnt = tree[p].lenl = tree[p].lenr = tree[p].len = (str[l - ] == '.');
return;
}
int mid = (l + r) >> ;
build(p << , l, mid);
build(p << | , mid + , r);
pushup(p);
} void update(int p, int pos)
{
if (tree[p].l == tree[p].r)
{
tree[p].cnt = tree[p].lenl = tree[p].lenr = tree[p].len = (str[tree[p].l - ] == '.');
return;
}
int mid = (tree[p].l + tree[p].r) >> ;
if (pos <= mid)
{
update(p << , pos);
}
else
{
update(p << | , pos);
}
pushup(p);
} char v[];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
scanf("%s", str);
build(, , n);
int x;
while (m--)
{
scanf("%d%s", &x, v);
str[x - ] = v[];
update(, x);
printf("%d\n", tree[].len - tree[].cnt);
}
return ;
}
(线段树 && 字符串的处理)codeforces -- 570C的更多相关文章
- 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch
Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...
- 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E
http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...
- HDU 3973 线段树+字符串hash
题目大意: 不断修改字符串中的字母,然后询问区间字符串是否处于已给定的字符串集合中 这里将原来的字符串集合保存到hash表中,当然用map,set都没有问题 修改查询都用线段树实现,自己的query函 ...
- hdu3973 AC's String 线段树+字符串hash
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3973/ 题意是:给出一个模式串,再给出一些串组成一个集合,操作分为两种,一种是替换模式串中的一个字符,还有一种是 ...
- 数据结构(线段树):Educational Codeforces Round 6 620E. New Year Tree
E. New Year Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- [bzoj2124]等差子序列——线段树+字符串哈希
题目大意 给一个1到N的排列\(A_i\),询问是否存在\(p_i\),\(i>=3\),使得\(A_{p_1}, A_{p_2}, ... ,A_{p_len}\)是一个等差序列. 题解 显然 ...
- 线段树 C - Connected Components? CodeForces - 920E
这个题目居然可以用线段树写,好震惊,如果不是在线段树专题肯定想不到,但是就算在线段树的专题里面,我也不太会怎么写. 这个题目大意是,给你n m n代表n个点,m代表m条边,然后就是m行,每行两个数字, ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)
原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解 By 岩之痕 目录: 一:综述 ...
随机推荐
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 处理静态资源
视频地址:https://www.cctalk.com/v/15114923882788 处理静态资源 无非花开花落,静静. 指定静态资源目录 这里我们使用第三方中间件: koa-static 安装并 ...
- linux、centos下查看系统版本、bios版本,内存信息等
1.查看系统版本 [root@localhost ~]# more /etc/issueCentOS release 6.2 (Final)Kernel \r on an \m 2.查看CPU信息 : ...
- linux系统分区表修复
有些时候在系统突然断电或硬盘只读后在看机会出现报错,开机到不了登录界面,而是来到一个提示: Give root password for maintenance(or type Control-d t ...
- cdoj844-程序设计竞赛 (线段树的区间最大连续和)【线段树】
http://acm.uestc.edu.cn/#/problem/show/844 程序设计竞赛 Time Limit: 3000/1000MS (Java/Others) Memory L ...
- 了解大数据的特点、来源与数据呈现方式以及用Python写Mad Libs游戏
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2620. 1.浏览2019春节各种大数据分析报告,例如: 这世间,再 ...
- GridView,datalist添加序号列
GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...
- 解决 listView gridView 与ScrollView嵌套时的冲突
package com.xqx.fight; import android.app.Activity; import android.os.Bundle; import android.view.Me ...
- ROS学习笔记二(创建ROS软件包)
catkin软件包的组成 一个软件包必须满足如下条件才能被称之为catkin软件包: 必须包含一个catkin编译文件package.xml(manifests文件),此文件包含了描述该软件包的重要信 ...
- maven使用感受
第一次接触的时候,什么都不懂,感觉好复杂. 后来系统地看了一个使用教程: 简单评价一下: 自动帮我们下载jar架包,还有就是可以执行命令自己部署到远程服务器上面去. 缺点: 学习成本.一般人不了解.第 ...
- 抽象 abstract 和 接口 interface。 java 的 堆 和 栈。 参数传递(基本类型和普通对象的区别)
package com.test; import com.test.Pro; //protected 修饰的成员只能在本包中和 继承该类的方法中使用 public abstract class Tes ...