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;

struct query{
  int type;
  ll value;
  query(int a=0,ll b=0):type(a),value(b){}
};

#define SZ (1<<17)
struct segtree{
  query s[SZ*2];
  ll t[SZ*2],u[SZ*2];

  void init(){
    for(int i=0;i<SZ*2;i++){
      s[i]=query();
      t[i]=u[i]=0;
    }
  }
  
  void func(int k,int l,int r,query q){
    if(q.type==1){
      if(s[k].type==0)s[k]=q;
      else s[k].value+=q.value;
    }
    if(q.type==2){
      s[k]=q;
      t[k]=q.value*(r-l);
      u[k]=q.value;
    }
  }

  void compute(int k,int l,int r){
    query q=s[k];
    s[k]=query();
    if(q.type==0||r-l==1)return;
    int m=(l+r)/2;
    func(k*2+1,l,m,q);
    func(k*2+2,m,r,q);
  }

  void Update(int a,int b,query x,int k=0,int l=0,int r=SZ){
    if(b<=l || r<=a)return;
    compute(k,l,r);
    if(a<=l && r<=b){
      func(k,l,r,x);
    }else{
      int m=(l+r)/2;
      Update(a,b,x,k*2+1,l,m);
      Update(a,b,x,k*2+2,m,r);
      t[k]=t[k*2+1]+t[k*2+2];
      u[k]=min(u[k*2+1],u[k*2+2]);
    }
  }

  ll Getsum(int a,int b,int k=0,int l=0,int r=SZ){
    if(b<=l || r<=a)return 0;
    compute(k,l,r);
    if(a<=l && r<=b){
      return t[k];
    }else{
      int m=(l+r)/2;
      ll lv=Getsum(a,b,k*2+1,l,m);
      ll rv=Getsum(a,b,k*2+2,m,r);
      return lv+rv;
    }
  }
};


segtree T,U;
int N;
char str[100005];
int A[100005];

int main(){
  
  scanf("%d",&N);
  scanf("%s",str);

  for(int i=0;i<N;i++)    A[i]=str[i]-'0';


  
  for(int i=0;i<N;i++){
    T.Update(i,i+1,query(2,A[i]));
    if(i+1<N){
      U.Update(i,i+1,query(2,A[i]*A[i+1]));
    }
  }
  int Q;
  scanf("%d",&Q);
  while(Q--){

    
    int l,r,x;
    scanf("%d %d %d",&l,&r,&x);
    l--;
    
    T.Update(l,r, query(2,x));




    if(r-l>1){
      U.Update(l,r-1,query(2,x));
    }



    if(l-1>=0){
      U.Update(l-1,l,query(2,0));


      if(T.Getsum(l-1,l) * T.Getsum(l,l+1) == 1 ){


        U.Update(l-1,l,query(2,1));
      }

      //                  cout<<l<<' '<<r<<' '<<x<<endl;            
    }
    

    
    if(r+1<=N){
      U.Update(r-1,r,query(2,0));
      if(T.Getsum(r-1,r)*T.Getsum(r,r+1) == 1 ){
        U.Update(r-1,r,query(2,1));
      }
    }

    //    cout<<endl;
    printf("%lld\n",T.Getsum(0,SZ)-U.Getsum(0,SZ));
    /*
    for(int i=0;i<N;i++)cout<<T.Getsum(i,i+1);
    cout<<endl;

    for(int i=0;i<N;i++)cout<<U.Getsum(i,i+1);
    cout<<endl;
    */
  }
  return 0;
}