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 ≤ 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
Sample Output
4
3
1
HINT
题意
给你一个字符串,然后每两个点可以变成一个点
然后有m次修改操作,每次可以修改一个位置的字符
然后问你修改之后,需要多少次操作,把所有的点,都变成连续的一个点
题解:
出去玩了5天一回来,本不该做比赛的:(
set乱搞
代码:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include<bits/stdc++.h>
#include <stack>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************************************
set<int >s;
set<int >::iterator it,itt;
int vis[]; char a[];
int main()
{ int n=read();
int m=read();
gets(a+);
s.insert();
s.insert(n+);
int last=;
int r=;
int ans=;
for(int i=;i<=n;i++){
if(a[i]!='.'){
s.insert(i);
vis[i]=;
if(r>last)
ans+=(r-last-);
last=i;
r=i;
}
else {r++;
if(i==n){
if(r>last)
ans+=(r-last-);
}
}
// cout<<ans<<" "<<endl;
} //cout<<ans<<endl;
int x,l,mid;
char ch;
for(int i=;i<=m;i++){
scanf("%d %c",&x,&ch);
if(ch!='.')
{
if(vis[x]){
printf("%d\n",ans);
continue;
}
r=*s.lower_bound(x);
l=*--s.lower_bound(x);
// if(i==3){cout<<l<<" "<<r<<endl;}
ans-=(max(r-l-,));
//if(i==3)cout<<ans<<" ";
ans+=max(x-l-,);
// if(i==3)cout<<ans<<" ";
ans+=max(r-x--,);
// if(i==3)cout<<ans<<" ";
vis[x]=;
s.insert(x);
cout<<ans<<endl;
}
else {
if(s.count(x)==){
cout<<ans<<endl;
continue;
}
it=s.lower_bound(x);
mid=*it;
l=*--it;
it++;
r=*++it;
ans-=max(mid-l-,);
ans-=max(r-mid-,);
ans+=max(r-l-,);
vis[x]=;
s.erase(x);
cout<<ans<<endl;
}
}
return ;
}
好特么捉急
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include<bits/stdc++.h>
#include <stack>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//***************************************************************
int n,m,x;
char c,ss[];
int main()
{
while(~scanf("%d%d",&n,&m))
{
int ans=;
scanf("%s",ss+);
for(int i=;i<=n;i++)
{
if(ss[i]=='.'&&ss[i+]=='.')
{
ans++;
}
}
for(int i=;i<=m;i++)
{
scanf("%d %c",&x,&c);
if(c=='.'&&ss[x]!='.')
{
if(ss[x-]=='.') ans++;
if(ss[x+]=='.') ans++;
}
if(c!='.'&&ss[x]=='.')
{
if(ss[x-]=='.') ans--;
if(ss[x+]=='.') ans--;
}
ss[x]=c;
printf("%d\n",ans);
}
}
return ;
}