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 repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600

class Segtree{
public:
  int n;
  vector<int> lazy, val;
  inline void lazy_eval(int k){//dbg(mp(k,v));
    if(lazy[k]>-1){
      if(lazy[k]==0) val[k]=0;
      else val[k] = 1;
      if(k<n-1){
        lazy[2*k+1]=lazy[k];
        lazy[2*k+2]=lazy[k];
      }
      lazy[k]=-1;
    }
  }
  int get(int v){
    v += n-1;
    int ret = val[v];
    if(lazy[v]>-1) ret = lazy[v];
    while(v>0){
      v = (v-1)/2;
      if(lazy[v]>-1) ret = lazy[v];
    }
    return ret;
  }
  void _update(int a, int b, int v, int k, int l, int r){// dbg(k);
    lazy_eval(k);
    if(r<=a || b<=l) return;
    if(a<=l && r<=b){
      lazy[k]=v;
      lazy_eval(k);
      return;
    }
    int m=(l+r)/2;
    _update(a,b,v,k*2+1,l,m);
    _update(a,b,v,k*2+2,m,r);
    val[k] = val[k*2+1] + val[k*2+2] - get(m-1) * get(m);
    return;
  }
  void update(int a, int b, int v){ _update(a,b,v,0,0,n); return; }
  Segtree(int n_){
    n=1;
    while(n<n_) n*=2;
    lazy = vector<int>(2*n-1, -1);
    val = vector<int>(2*n-1, 0);
  }
};


int main(){
  int n; string s;
  cin>>n>>s;

  Segtree st(n);
  rep(i,n) st.val[st.n-1+i] = s[i]=='1';

  function<pair<int,int>(int)> idfs = [&](int i){
    if(i>=st.n-1){
      return mp(i-(st.n-1), i-(st.n-1)+1);
    }
    auto pl = idfs(i*2+1);
    auto pr = idfs(i*2+2);assert(pl.se==pr.fi);
//    cout << vector<int>{i, st.val[i*2+1], st.val[i*2+2], st.val[pr.fi-1], st.val[pr.fi]} << endl;
    st.val[i] = st.val[i*2+1] + st.val[i*2+2] - st.val[st.n-1+pr.fi-1]*st.val[st.n-1+pr.fi];
    return mp(pl.fi, pr.se);
  };
  idfs(0);

//  dbg(st.val);

  int q;cin>>q;
  rep(_,q){
    int l,r,b;
    cin>>l>>r>>b;
    st.update(l-1,r,b);// dbg(st.val);dbg(st.lazy);
    cout << st.val[0] << endl;
  }


  return 0;
}