Problem: E: Binary Sequence-Binary Sequence-

story

I love binaries Bonald.Brvin.Bnuth! He is known as Uncle Bnuth, who is studying the nature of binary sequences at Offtunford University! By the way, if my name "Bonald.Brvin.Bnuth" is binarized from ASCII code, it will be "1000010 1101111 1101110 1100001 1101100 1100100 101110 1000010 1110010 1110110 1101001 1101110 101110 1000010 1101110 1110101 1110100 1101000"! It's fun!

I'm looking forward to it from the morning because my disciples are going to do some interesting experiments today! What are you doing?

What! I want to discover some properties by continuing to rewrite binary strings! HM! This is the scent of an important discovery! Let's start the experiment! Let's summarize more detailed settings as sentences!

problem

Given a binary sequence of length n x = (x_1, ..., x_n) (x_i \ in \\ {0,1 \\}, i = 1, ..., n). For the binary string x, we define two functions f (x) and g (x) as follows.


f (x) = Σ_ {i = 1} ^ {n} x_i = x_1 + x_2 + ... + x_n
g (x) = Σ_ {i = 1} ^ {n-1} x_i x_ {i + 1} = x_1 x_2 + x_2 x_3 + ... x_ {n-1} x_n


Now, perform the following change operation on the binary string x q times. The jth change operation is given by l_j, r_j, b_j (1 \ leq l_j \ leq r_j \ leq n, b_j \ in \\ {0,1 \\}, j = 1, ..., q). This corresponds to the operation of replacing the l_jth to r_jths of the binary string x with b_j. Find f (x) --g (x) after each change operation.

Input format


n
x
q
l_1 r_1 b_1
...
l_q r_q b_q


The first line is given the integer n that represents the length of the binary column. The second line is given a string that represents the binary column x. The third line is given the integer q, which represents the number of queries. In the following q lines, the i-th line is given information about the i-th query l_i, r_i, b_i in this order, separated by blanks.

Constraint

* 2 \ leq n \ leq 100000
* x is a character string consisting of ‘0’ and ‘1’
* | x ​​| = n
* 1 \ leq q \ leq 100000
* 1 \ leq l_j \ leq r_j \ leq n, b_j \ in \\ {0,1 \\}, j = 1, ..., q



Output format

For each i = 1, ..., q, output the value of f (x) --g (x) after processing the i-th query on the i-line.

Input example 1


Ten
0101100110
3
3 3 1
1 6 0
2 5 1


Output example 1


2
1
2


Input example 2


8
00111100
3
8 8 1
4 5 0
7 8 1


Output example 2


2
3
2






Example

Input

10
0101100110
3
3 3 1
1 6 0
2 5 1


Output

2
1
2

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

struct node{
    int pc,g,l,r;
    node(int a,int b,int c,int d):pc(a),g(b),l(c),r(d){}
    node(){pc=g=l=r=-1;}
};
node e;
inline node add(const node &a,const node &b){
    if(a.pc==-1)return b;
    if(b.pc==-1)return a;
    node c;
    c.pc=a.pc+b.pc;
    c.g=a.g+b.g+a.r*b.l;
    c.l=a.l;
    c.r=b.r;
    return c;
}

struct segtree{
    static const int SEG=1<<17;
    vector<node>dat;
    vector<int>put;
    segtree(string &s):dat(SEG*2),put(SEG*2,-1){
        for(int i=0;i<s.size();i++){
            node d;
            int a=s[i]-'0';
            d.pc=a;
            d.g=0;
            d.l=d.r=a;
            dat[i+SEG-1]=d;
        }
        for(int i=SEG-2;i>=0;i--){
            dat[i]=add(dat[i*2+1],dat[i*2+2]);
        }
    }
    inline void push(int k,int l,int r){
        if(put[k]==-1)return;
        dat[k].pc=(r-l)*put[k];
        dat[k].g=(r-l-1)*put[k];
        dat[k].l=dat[k].r=put[k];

        if(k<SEG-1){
            put[k*2+1]=put[k*2+2]=put[k];
        }
        put[k]=-1;
    }
    void update(int a,int b,int x,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return;
        if(a<=l&&r<=b){
            put[k]=x;
            push(k,l,r);
            return;
        }
        update(a,b,x,k*2+1,l,(l+r)/2);
        update(a,b,x,k*2+2,(l+r)/2,r);
        dat[k]=add(dat[k*2+1],dat[k*2+2]);
    }
    node query(int a,int b,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return e;
        if(a<=l&&r<=b)return dat[k];
        return add(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
    }
};

signed main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    int N;
    string S;
    cin>>N>>S;
    segtree seg(S);

     int Q;cin>>Q;
     while(Q--){
        int l,r,b;
        cin>>l>>r>>b;
        l--;
        seg.update(l,r,b);
        node d=seg.query(0,N);
        cout<<d.pc-d.g<<endl;
     }
    return 0;
}