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 <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */
// quoted from beet-aizu
template <typename T,typename E, typename F, typename G, typename H>
struct LazySegmentTree{
    //using F = function<T(T,T)>;
    //using G = function<T(T,E)>;
    //using H = function<E(E,E)>;
    int n,height;
    F f;
    G g;
    H h;
    T ti;
    E ei;
    vector<T> dat;
    vector<E> laz;
    LazySegmentTree(F f,G g,H h,T ti,E ei):
        f(f),g(g),h(h),ti(ti),ei(ei){}

    void init(int n_){
        n=1;height=0;
        while(n<n_) n<<=1,height++;
        dat.assign(2*n,ti);
        laz.assign(2*n,ei);
    }
    void build(const vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    inline T reflect(int k){
        return laz[k]==ei?dat[k]:g(dat[k],laz[k]);
    }
    inline void eval(int k){
        if(laz[k]==ei) return;
        laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
        laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
        dat[k]=reflect(k);
        laz[k]=ei;
    }
    inline void thrust(int k){
        for(int i=height;i;i--) eval(k>>i);
    }
    inline void recalc(int k){    
        while(k>>=1)
            dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1));
    }
    void update(int a,int b,E x){
        thrust(a+=n);
        thrust(b+=n-1);
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if(l&1) laz[l]=h(laz[l],x),l++;
            if(r&1) --r,laz[r]=h(laz[r],x);
        }
        recalc(a);
        recalc(b);
    }
    void set_val(int a,T x){
        thrust(a+=n);
        dat[a]=x;laz[a]=ei;
        recalc(a);
    }
    T query(int a,int b){
        thrust(a+=n);
        thrust(b+=n-1);
        T vl=ti,vr=ti;
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1) {
            if(l&1) vl=f(vl,reflect(l++));
            if(r&1) vr=f(reflect(--r),vr);
        }
        return f(vl,vr);
    }
};

/* 
 *    [考えるべきこと]
 *    区間をマージしてから作用素を作用させても、作用素を作用させてから区間をマージするのと結果が同じ
 *    複数の作用素をマージして一度に作用させられること
 *    作用素を伝搬し終わっているのかの判定に必要（まあこれは満たされていなくても最悪どうにかなる）
 *    O(N) とかだと困る（setのマージとか）
 *    区間の長さに比例して作用が変わるときは，practice/RSRA や Library-Checher の RangeAffineRangeSum を参照する
 * 
 */

/**  テンプレ
int main(){
}
**/

int main() {

  int n; cin >> n;
  string x; cin >> x;

  using t = pair<int, int>;
  using e = int;
  using P = pair<int, int>;
  auto f = [](P a, P b){ return P(a.first + b.first, a.second + b.second); };
  auto g = [](P a, int b){ return P(a.second * b, a.second); };
  auto h = [](int a, int b){ return b != INT_MAX ? b : a; };

  LazySegmentTree<t, e, decltype(f), decltype(g), decltype(h)> sg1(f, g, h, t(0, 0), INT_MAX);
  LazySegmentTree<t, e, decltype(f), decltype(g), decltype(h)> sg2(f, g, h, t(0, 0), INT_MAX);
  sg1.build(vector<t>(n, t(0, 1)));
  sg2.build(vector<t>(n, t(0, 1)));
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      sg1.update(i, i + 1, 1);
    }
  }

  for (int i = 0; i < n - 1; i++) {
    if (x[i] == '1' and x[i + 1] == '1') {
      sg2.update(i, i + 1, 1);
    }
  }

  int q; cin >> q;
  /*
  for (int i = 0; i < n; i++) {
    cerr << sg1.query(i, i + 1).first << " ";
  }
  cerr << endl;
  for (int i = 0; i < n; i++) {
    cerr << sg2.query(i, i + 1).first << " ";
  }
  cerr << endl;
  */

  for (int i = 0; i < q; i++) {
    int l, r, b; cin >> l >> r >> b;
    l--;
    r--;
    assert(0 <= l and l < n);
    assert(0 <= r and r < n);

    if (b == 1) {
      sg2.update(l, r, 1);
      if (l != 0) {
        int val = sg1.query(l - 1, l).first;
        sg2.update(l - 1, l, val);
      }

      if (r != n - 1) {
        int val = sg1.query(r + 1, r + 2).first;
        sg2.update(r, r + 1, val);
      }

    } else {
      if (l != 0) sg2.update(l - 1, r + 1, 0);
      else sg2.update(l, r + 1, 0);
    }

    sg1.update(l, r + 1, b);

    cout << sg1.query(0, n).first - sg2.query(0, n).first << endl;
    
    /*
    for (int i = 0; i < n; i++) {
      cerr << sg1.query(i, i + 1).first << " ";
    }
    cerr << endl;

    for (int i = 0; i < n; i++) {
      cerr << sg2.query(i, i + 1).first << " ";
    }
    cerr << endl;
    */

  }
  
  return 0;
}