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

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 ≤ nci — 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次修改操作,每次可以修改一个位置的字符

然后问你修改之后,需要多少次操作,把所有的点,都变成连续的一个点

题解

我比较蠢,我用的线段树,太蠢了

正解不超过30行,几个if就好了……

维护的是每一个pos的左边的字母和右边的字母位置

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;

#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
#define MID(a,b) (a+((b-a)>>1))
const int N=300005;

struct node
{
    int lft,rht;
    int lmx,rmx;
    int len(){return rht-lft+1;}
    int mid(){return MID(lft,rht);}
    void init(){lmx=rmx=len();}
    void fun(int valu)
    {
        if(valu==-1) lmx=rmx=0;
        else lmx=rmx=1;
    }
};

int n,m;

struct Segtree
{
    node tree[N*10];
    void up(int ind)
    {
        tree[ind].lmx=tree[LL(ind)].lmx;
        tree[ind].rmx=tree[RR(ind)].rmx;
        if(tree[LL(ind)].lmx==tree[LL(ind)].len())
            tree[ind].lmx+=tree[RR(ind)].lmx;
        if(tree[RR(ind)].rmx==tree[RR(ind)].len())
            tree[ind].rmx+=tree[LL(ind)].rmx;
    }
    void build(int lft,int rht,int ind)
    {
        tree[ind].lft=lft,tree[ind].rht=rht;
        tree[ind].init();
        if(lft!=rht)
        {
            int mid=tree[ind].mid();
            build(lft,mid,LL(ind));
            build(mid+1,rht,RR(ind));
        }
    }
    void updata(int pos,int ind,int valu)
    {
        if(tree[ind].lft==tree[ind].rht) tree[ind].fun(valu);
        else
        {
            int mid=tree[ind].mid();
            if(pos<=mid) updata(pos,LL(ind),valu);
            else updata(pos,RR(ind),valu);
            up(ind);
        }
    }
    void query(int pos,int ind,int& x,int& y)
    {
        if(tree[ind].lft==tree[ind].rht)
        {
            if(tree[ind].lmx==1) x=y=tree[ind].lft;
            else x=y=0;
        }
        else
        {
            int mid=tree[ind].mid();
            if(pos<=mid) query(pos,LL(ind),x,y);
            else query(pos,RR(ind),x,y);
            if(tree[LL(ind)].rht==y) y+=tree[RR(ind)].lmx;
            if(tree[RR(ind)].lft==x) x-=tree[LL(ind)].rmx;
        }
    }
}seg;
char s[N];
int vis[N];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        scanf("%s",s+1);
        seg.build(1,n+100,1);
        int len=strlen(s+1);
        int ans=0;
        int tmp=0;
        seg.updata(n+1,1,-1);
        for(int i=1;i<=len;i++)
        {
            if(s[i]=='.')
                tmp++;
            else
            {
                if(tmp!=0)
                    ans+=(tmp-1);
                tmp=0;
                seg.updata(i,1,-1);
                vis[i]=1;
            }
        }
        if(tmp!=0)
            ans+=(tmp-1);
        while(m--)
        {
            char cmd[5];
            int pos,st,ed;
            scanf("%d",&pos);
            scanf("%s",&cmd);
            if(cmd[0]!='.')
            {
                if(vis[pos]==1)
                    printf("%d
",ans);
                else
                {
                    vis[pos]=1;

                    seg.query(pos,1,st,ed);
                    ans-=(ed-st);
                    seg.updata(pos,1,-1);
                    seg.query(pos+1,1,st,ed);
                    ans+=(ed-st);
                    if(pos-1!=0)
                    {
                        seg.query(pos-1,1,st,ed);
                        ans+=(ed-st);
                    }
                    printf("%d
",ans);
                }

            }
            else
            {
                 if(vis[pos]==0)
                    printf("%d
",ans);
                else
                {
                    vis[pos]=0;
                    seg.query(pos+1,1,st,ed);
                    ans-=(ed-st);
                    if(pos-1!=0)
                    {
                        seg.query(pos-1,1,st,ed);
                        ans-=(ed-st);
                    }
                    seg.updata(pos,1,1);
                    seg.query(pos,1,st,ed);
                    ans+=(ed-st);
                    printf("%d
",ans);

                }
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4728869.html