0w1

CFR 570 C. Replacement ( Ad hoc )

Problem - C - Codeforces
We will first retrieve the answer for the original string, then update one by one in each query. Without loss of generality, we will assume that each update is meaningful ( that is, either changes a character to a dot, or vice versa ). When we replace with a dot, if one of its neighbours is also a dot, the answer should be increased by one, if both of its neighbours are dots, the answer should be increased by two. That will also apply on replacing with characters as well, where instead the answer will decrease.

void solve(){
    int N, M; cin >> N >> M;
    string s; cin >> s;
    vi is_dot( N );
    for( int i = 0; i < N; ++i )
        is_dot[ i ] = ( s[ i ] == '.' );
    int ans = 0;
    for( int i = 0; i + 1 < N; ++i )
        if( s[ i ] == '.' and s[ i + 1 ] == '.' ){
            int j;
            for( j = i + 1; j < N and s[ j ] == '.'; ++j )
                ++ans;
            i = j;
        }
    for( int i = 0; i < M; ++i ){
        int x; cin >> x; --x;
        string u; cin >> u;
        int d = ( u[ 0 ] == '.' );
        if( d == is_dot[ x ] ){
            cout << ans << endl;
            continue;
        }
        is_dot[ x ] = d;
        if( x - 1 >= 0 and is_dot[ x - 1 ] )
            ans += d ? 1 : -1;
        if( x + 1 <  N and is_dot[ x + 1 ] )
            ans += d ? 1 : -1;
        cout << ans << endl;
    }
}