Codeforces Round #316 (Div. 2) C. Replacement 找规律 或 线段树

来源:互联网 发布:淘宝收藏量有什么用 编辑:程序博客网 时间:2024/05/21 14:51
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 h3 c9 f
output
431
input
4 4.cc.2 .3 .2 a1 a
output
1311
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 100005char 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\n",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\n",queryTree(1,n,1,1,n));        }    }    return 0;}


0 0
原创粉丝点击