zoukankan      html  css  js  c++  java
  • Codeforces Round #316 (Div. 2) C. Replacement

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 test(s)
    input
    10 3
    .b..bz....
    1 h
    3 c
    9 f
    
    output
    4
    3
    1
    
    input
    4 4
    .cc.
    2 .
    3 .
    2 a
    1 a
    
    output
    1
    3
    1
    1
    
    Note

    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.")

    我的思路是记录点所在的区域,用set记录下来,先算出第一次没有变化总共需要的操作数num,然后对于每一次变化,更改点的区间,变化操作数num。

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define inf 0x7fffffff
    #define maxn 300060
    char s[maxn],str[10];
    struct node{
        int l,r;
    }a,temp,b,temp1;
    
    bool operator<(node a,node b){
        return a.l<b.l;
    }
    
    set<node>myset;
    set<node>::iterator it;
    int main()
    {
        int len,m,i,j,num,l,c,t;
        while(scanf("%d%d",&len,&m)!=EOF)
        {
           scanf("%s",s+1);
           myset.clear();
           if(len==1){
              for(i=1;i<=m;i++){
                scanf("%d%s",&c,str);
                printf("0
    ");
              }
              continue;
           }
           num=0;i=1;
           while(i<=len){
               if(s[i]=='.'){
                  a.l=i;
                  while(s[i]=='.' && i<=len){
                     i++;
                  }
                  a.r=i-1;
                  if(a.r-a.l+1>=2){
                     num+=a.r-a.l;
                  }
                  myset.insert(a);
               }
               else{
                 s[i]='a';i++;
               }
           }
           for(i=1;i<=m;i++){
                 scanf("%d%s",&c,&str);
                 if(str[0]!='.'){
                    str[0]='a';
                 }
                 if(str[0]==s[c]){
                    printf("%d
    ",num);continue;
                 }
                 s[c]=str[0];
                 if(myset.empty()){
                    if(str[0]=='.'){
                        a.l=a.r=c;
                        myset.insert(a);
                        printf("0
    ");continue;
                    }
                    else{
                        printf("0
    ");continue;
                    }
                 }
    
                 it=myset.begin();
                 if((*it).l>c ){
                    temp=*it;
                    if(str[0]=='.'){
                        if(c+1==temp.l){
                            a.l=c;a.r=temp.r;
                            num=num-(temp.r-temp.l)+a.r-a.l;
                            printf("%d
    ",num);
                            myset.erase(temp);
                            myset.insert(a);
                        }
                        else{
                            a.l=a.r=c;
                            myset.insert(a);
                            printf("%d
    ",num);
                        }
    
                    }
                    else{
                        printf("%d
    ",num);
                    }
                    continue;
    
                 }
    
                 a.l=c;a.r=0;
                 it=myset.upper_bound(a);
                 it--;
                 temp=*it;
                 if(temp.r<c){
                    it++;
                    if(it!=myset.end() && temp.r+1==c && c+1==(*it).l){
                        temp1=*it;
                        a.l=temp.l;a.r=temp1.r;
                        num=num-(temp.r-temp.l)-(temp1.r-temp1.l)+a.r-a.l;
                        printf("%d
    ",num);
                        myset.erase(temp);
                        myset.erase(temp1);
                        myset.insert(a);
    
                    }
                    else if(temp.r+1==c){
                        a.l=temp.l;a.r=c;
                        num=num-(temp.r-temp.l)+a.r-a.l;
                        printf("%d
    ",num);
    
                        myset.erase(temp);
                        myset.insert(a);
    
                    }
                    else if(it!=myset.end() && c+1==(*it).l){
                        temp1=*it;
                        a.l=c;a.r=temp1.r;
                        num=num-(temp1.r-temp1.l)+a.r-a.l;
                        printf("%d
    ",num);
    
                        myset.erase(temp1);
                        myset.insert(a);
    
                    }
                    else{
                        a.l=c;a.r=c;
                        myset.insert(a);
                        printf("%d
    ",num);
                    }
    
                 }
                 else{
                    if(temp.l==temp.r){
                        printf("%d
    ",num);
                        myset.erase(temp);
                    }
                    else if(temp.l+1==temp.r){
                        num--;
                        printf("%d
    ",num);
                        if(temp.l==c){
                            a.l=a.r=temp.r;
                        }
                        else a.l=a.r=temp.l;
                        myset.erase(temp);
                        myset.insert(a);
                    }
                    else{
                        if(c==temp.l){
                            a.l=temp.l+1;a.r=temp.r;
                            num=num-(temp.r-temp.l)+a.r-a.l;
                            printf("%d
    ",num);
                            myset.erase(temp);
                            myset.insert(a);
    
    
                        }
                        else if(c==temp.r){
                            a.l=temp.l;a.r=temp.r-1;
                            num=num-(temp.r-temp.l)+a.r-a.l;
                            printf("%d
    ",num);
                            myset.erase(temp);
                            myset.insert(a);
    
                        }
                        else{
                            a.l=temp.l;a.r=c-1;
                            b.l=c+1;b.r=temp.r;
                            num=num-(temp.r-temp.l)+a.r-a.l+b.r-b.l;
                            printf("%d
    ",num);
                            myset.erase(temp);
                            myset.insert(a);
                            myset.insert(b);
                        }
    
                    }
                 }
           }
        }
        return 0;
    }
    


  • 相关阅读:
    今天VSS 了一把
    中文字母检索
    当心! 您也可能犯得js错 eval()不等于eval("")!
    腾讯微博邀请码2010年6月9日11:14:28
    存储过程原理
    腾讯微博邀请码2010年5月25日16:44:24
    《QQ我的好友想到的信息架构》
    8小时之外(Beyond the 8 Hours)
    超搞笑漫画比喻!如果浏览器是出行工具
    Nginx环境下配置PHP使用的SSL认证(https)
  • 原文地址:https://www.cnblogs.com/herumw/p/9464667.html
Copyright © 2011-2022 走看看