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

24 篇文章 0 订阅
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.

方法一,直接找规律,如果,连续的点,个数就是连续的点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\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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值