(线段树 && 字符串的处理)codeforces -- 570C

时间:2022-12-30 13:35:44

链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/J

 

Description

Daniel has a string s, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as 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

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

Hint

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

找到了规律很水的AC了,但看了题解后知道原来这是个线段树, 两个代码都粘一下

 

水的代码:

#include<stdio.h>
#include<string.h>

#define N 301100

char s[N];

int main()
{
    int n, m, sum, z;
   while(scanf("%d%d", &n, &m)!=EOF)
   {
       char ch;
       int k, i;
       scanf("%s", s);

       sum=0;
       for(i=0; i<n; i++)
       {
            z = 0;
          while(s[i]=='.' && i<n)
          {
              z++;
              i++;
          }
          if(z)
          sum+=z-1;
       }

       for(i=1; i<=m; i++)
       {
           scanf("%d %c", &k, &ch);

           if((s[k-1]!='.'&&ch!='.') || (s[k-1]=='.'&&ch=='.') )
            {
               printf("%d\n", sum);
               continue;
            }
            if(ch=='.' && s[k-1]!='.')
            {
                if(s[k]!='.' && s[k-2]!='.')
                    sum = sum;
                else if(s[k]=='.' && s[k-2]=='.')
                    sum += 2;
                else if(s[k]=='.' || s[k-2]=='.')
                    sum += 1;
            }
            if(ch!='.' && s[k-1]=='.')
            {
                if(s[k]=='.' && s[k-2]=='.')
                    sum = sum-2;
                else if(s[k]!='.' && s[k-2]!='.')
                    sum = sum;
                else if(s[k]!='.' || s[k-2]!='.')
                    sum = sum-1;
            }
            s[k-1] = ch;
            printf("%d\n", sum);
       }
   }
    return 0;
}

 

 

 

线段树:

/*************************************************************************
    > File Name: C.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年08月14日 星期五 01时09分43秒
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> PLL;
const LL INF = (1LL << 60);

const int N = 300010;
struct SegTree {
    int l, r;
    int lenl, lenr;
    int len;
    int cnt;
}tree[N << 2];
char str[N];

void pushup(int p)
 {
    tree[p].lenl = tree[p << 1].lenl;
    if (tree[p].lenl == tree[p << 1].r - tree[p << 1].l + 1) 
        {
        tree[p].lenl += tree[p << 1 | 1].lenl;
    }
    tree[p].lenr = tree[p << 1 | 1].lenr;
    if (tree[p].lenr == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1) 
    {
        tree[p].lenr += tree[p << 1].lenr;
    }
    tree[p].cnt = tree[p << 1].cnt + tree[p << 1 | 1].cnt;
    if (tree[p << 1].lenr && tree[p << 1 | 1].lenl)
     {
        --tree[p].cnt;
    }
    tree[p].len = tree[p << 1].len + tree[p << 1 | 1].len;
}

void build(int p, int l, int r) 
{
    tree[p].l = l;
    tree[p].r = r;
    if (l == r) {
        tree[p].cnt = tree[p].lenl = tree[p].lenr = tree[p].len = (str[l - 1] == '.');
        return;
    }
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    pushup(p);
}

void update(int p, int pos) 
{
    if (tree[p].l == tree[p].r) 
    {
        tree[p].cnt = tree[p].lenl = tree[p].lenr = tree[p].len = (str[tree[p].l - 1] == '.');
        return;
    }
    int mid = (tree[p].l + tree[p].r) >> 1;
    if (pos <= mid) 
    {
        update(p << 1, pos);
    }
    else 
    {
        update(p << 1 | 1, pos);
    }
    pushup(p);
}

char v[4];
int main() 
{
    int n, m;
    scanf("%d%d", &n, &m);
    scanf("%s", str);
    build(1, 1, n);
    int x;
    while (m--)
    {
        scanf("%d%s", &x, v);
        str[x - 1] = v[0];
        update(1, x);
        printf("%d\n", tree[1].len - tree[1].cnt);
    }
    return 0;
}