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;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

int n;
char x[125252];
int q;

typedef pair<pii,int> piii;
#define L first.first
#define R first.second
#define C second

int f=0,g=0;

int main(){
  scanf("%d",&n);
  scanf("%s",x);
  scanf("%d",&q);
  set<piii> S;
  int bef = 0;
  int head = 0;
  REP(i,n)if(x[i]-'0' != bef){
    S.insert(piii(pii(head,i-1),bef));
    bef = x[i]-'0';
    head = i;
  }
  S.insert(piii(pii(head,n-1),bef));
    // for(piii P : S){
    //   printf("%d %d %d\n",P.L,P.R,P.C);
    // }

  REP(i,n)f += x[i]-'0';
  REP(i,n-1)g += (x[i]-'0') * (x[i+1]-'0');

  while(q--){
    int l,r,b;
    scanf("%d%d%d",&l,&r,&b);
    --l;--r;
    piii add = piii(pii(l,r),b);
    // left
    {
      auto it = S.lower_bound(piii(pii(l,-1),-1));
      if(it!=S.begin()){
        --it;
        auto cell = *it;
        int cl = cell.L;
        int cr = cell.R;
        int cc = cell.C;
        if(l <= cr){
          // cross
          if(b==cc){
            // nobasu
            l = cl;
          }else{
            // cut
            if(cc==1){
              --g;
            }
            S.erase(it);
            auto cell2 = cell;
            cell.R = l-1;
            cell2.L = l;
            S.insert(cell);
            S.insert(cell2);
          }
        }
      }
    }
    // absorb
    while(true){
      auto it = S.lower_bound(piii(pii(l,-1),-1));
      if(it==S.end())break;
      if(it->R <= r){
        auto cell = *it;
        S.erase(it);
        if(cell.C == 1){
          int len = cell.R - cell.L + 1;
          f -= len;
          g -= len-1;
        }
      }else{
        break;
      }
    }
    // tail
    {
      auto it = S.lower_bound(piii(pii(l,-1),-1));
      if(it!=S.end()){
        if(it->C == b){
          if(it->C == 1){
            int len = it->R - it->L + 1;
            f -= len;
            g -= len-1;
          }
          r = it->R;
          S.erase(it);
        }else{
          if(it->C == 1){
            int len = r - it->L + 1;
            f -= len;
            g -= len;
          }
          auto cell = *it;
          cell.L = r+1;
          S.erase(it);
          S.insert(cell);
        }
      }
    }
    // head connect
    {
      auto it = S.lower_bound(piii(pii(l,-1),-1));
      if(it!=S.begin()){
        --it;
        if(it->C == b){
          l = it->L;
          if(it->C == 1){
            int len = it->R - it->L + 1;
            f -= len;
            g -= len-1;
          }
          S.erase(it);
        }
      }
    }
    // tail connect
    {
      auto it = S.lower_bound(piii(pii(l,-1),-1));
      if(it!=S.end()){
        if(it->C == b){
          r = it->R;
          if(it->C == 1){
            int len = it->R - it->L + 1;
            f -= len;
            g -= len-1;
          }
          S.erase(it);
        }
      }
    }
    S.insert(piii(pii(l,r),b));
    if(b==1){
      int len = r-l+1;
      f += len;
      g += len-1;
    }
    // for(piii P : S){
    //   printf("%d %d %d\n",P.L,P.R,P.C);
    // }

    //
    printf("%d\n",f-g);
  }
  return 0;
}