程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> Codeforces Round #316 (Div. 2) C. Replacement 找規律 或 線段樹

Codeforces Round #316 (Div. 2) C. Replacement 找規律 或 線段樹

編輯:關於C++
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 ≤ 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 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.

    方法一,直接找規律,如果,連續的點,個數就是連續的點num-1。那新加入的點如果是點原來不是點和新加的的點不是點原來是點,與左右比較一下更新一個就可以了。復雜度為O(n)

     

    #define N 500005
    #define M 100005
    char str[N],c[N];
    int n,m,t;
    int main(){
        while(S2(n,m) != EOF){
            SS(str);
            int ans=0,num=0;
            FI(n+1){
                if(str[i]!='.'&& num){
                    ans += num - 1;
                    num = 0;
                }
                if(str[i]=='.')
                    num++;
            }
            FJ(m){
                S(t);
                t--;
                SS(c);
                if(c[0]=='.' && str[t]!='.'){
                    if(t >= 1 && str[t-1]=='.') ans++;
                    if(t < n-1 && str[t+1]=='.') ans++;
                }
                else if(c[0]!='.' && str[t]=='.'){
                    if(t >= 1 && str[t-1]=='.') ans--;
                    if(t < n-1 && str[t+1]=='.') ans--;
                }
                str[t] = c[0];
                printf(%d
    ,ans);
            }
        }
        return 0;
    }


     

    方法二,題意,給出一段字符串,要求出連續的.的個數和。查詢時候,會更改某個字符,直接用線段樹單點更新就可以了。復雜度o(n * logn)比第一種要麻煩很多了!

     

    #define N 300005
    #define M 100005
    #define maxn 205
    #define SZ 26
    #define MOD 1000000000000000007
    #define lson (now<<1)
    #define rson (now<<1|1)
    int n,q,ss;
    char str[N],str2[10];
    struct node{
        int c,sum,l,r;
    };
    node tree[N*4];
    void pushUp(int now){
        tree[now].sum = tree[lson].sum + tree[rson].sum ;
        if(tree[lson].r && tree[rson].l){
            tree[now].sum++;
        }
        tree[now].l = tree[lson].l;
        tree[now].r = tree[rson].r;
    }
    void buildTree(int l,int r,int now){
        tree[now].c = 0,tree[now].l = 0,tree[now].r = 0,tree[now].sum = 0;
        if(l >= r){
            return ;
        }
        int mid = (l+r)>>1;
        buildTree(l,mid,lson);
        buildTree(mid+1,r,rson);
    }
    void updateTree(int l,int r,int now,int s,int e,int c){
        if(s <= l && e>= r){
            tree[now].c = tree[now].l = tree[now].r =c;
            tree[now].sum = 0;
            return ;
        }
        int mid = (l+r)>>1;
        if(s <= mid) updateTree(l,mid,lson,s,e,c);
        if(e > mid) updateTree(mid+1,r,rson,s,e,c);
        pushUp(now);
    }
    int queryTree(int l,int r,int now,int s,int e){
        return tree[now].sum;
    }
    int main()
    {
        while(S2(n,q)!=EOF)
        {
            SS(str);
            buildTree(1,n,1);
            FI(n){
                updateTree(1,n,1,i+1,i+1,str[i] == '.'?1:0);
            }
            FI(q){
                S(ss);
                SS(str2);
                updateTree(1,n,1,ss,ss,str2[0] == '.'?1:0);
                printf(%d
    ,queryTree(1,n,1,1,n));
            }
        }
        return 0;
    }
    

     
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved