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 <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
int n_;
int q;

struct data{
	int lazy;
	int sum;
	data(){}
	data(int laz,int ss){
		lazy=laz;
		sum=ss;
	}
};

struct data2{
	int sum;
	int l,r;
	int lazy;
	data2(){}
	data2(int ss,int ll,int rr){
		sum=ss;
		l=ll;
		r=rr;
	}
};

data dat[1<<21];
data2 dat2[1<<21];

inline void lazy_evaluate_node(int k,int a,int b){
	if(dat[k].lazy==1){
		dat[k].sum=b-a;
	}
	if(dat[k].lazy==-1){
		dat[k].sum=0;
	}
	if(k+1<n_){
		if(dat[k].lazy!=0)dat[k*2+1].lazy=dat[k].lazy;
		if(dat[k].lazy!=0)dat[k*2+2].lazy=dat[k].lazy;
	}
	dat[k].lazy=0;
}

inline void lazy_update_node(int k,int a,int b){
	dat[k].sum=dat[k*2+1].sum+dat[k*2+2].sum;
}

void add(int v,int a,int b,int k=0,int l=0,int r=n_){
	//printf("%d %d %d %d %d %d\n",v,a,b,k,l,r);
	lazy_evaluate_node(k,l,r);
	if(r<=a || b<=l)return;
	if(a<=l && r<=b){
		dat[k].lazy=v;
		lazy_evaluate_node(k,l,r);
		return;
	}
	add(v,a,b,k*2+1,l,(l+r)/2);
	add(v,a,b,k*2+2,(l+r)/2,r);
	lazy_update_node(k,l,r);
}

int query(int a,int b,int k=0,int l=0,int r=n_){
	lazy_evaluate_node(k,l,r);
	if(r<=a || b<=l)return 0;
	if(a<=l && r<=b)return dat[k].sum;
	int vl=query(a,b,k*2+1,l,(l+r)/2);
	int vr=query(a,b,k*2+2,(l+r)/2,r);
	lazy_update_node(k,l,r);
	return vl+vr;
}

inline void lazy_evaluate_node2(int k,int a,int b){
	if(dat2[k].lazy==1){
		dat2[k].sum=b-a-1;
		dat2[k].l=1;
		dat2[k].r=1;
	}
	if(dat2[k].lazy==-1){
		dat2[k].sum=0;
		dat2[k].l=0;
		dat2[k].r=0;
	}
	if(k+1<n_){
		if(dat2[k].lazy!=0)dat2[k*2+1].lazy=dat2[k].lazy;
		if(dat2[k].lazy!=0)dat2[k*2+2].lazy=dat2[k].lazy;
	}
	dat2[k].lazy=0;
}

inline void lazy_update_node2(int k,int a,int b){
	dat2[k].sum=dat2[k*2+1].sum+dat2[k*2+2].sum;
	if(dat2[k*2+1].r+dat2[k*2+2].l==2)dat2[k].sum++;
	dat2[k].l=dat2[k*2+1].l;
	dat2[k].r=dat2[k*2+2].r;
}

void add2(int v,int a,int b,int k=0,int l=0,int r=n_){
	lazy_evaluate_node2(k,l,r);
	if(r<=a || b<=l)return;
	if(a<=l && r<=b){
		dat2[k].lazy=v;
		lazy_evaluate_node2(k,l,r);
		return;
	}
	add2(v,a,b,k*2+1,l,(l+r)/2);
	add2(v,a,b,k*2+2,(l+r)/2,r);
	lazy_update_node2(k,l,r);
}

int query2(int a,int b,int k=0,int l=0,int r=n_){
	lazy_evaluate_node2(k,l,r);
	if(r<=a || b<=l)return 0;
	if(a<=l && r<=b)return dat2[k].sum;
	int vl=query2(a,b,k*2+1,l,(l+r)/2);
	int vr=query2(a,b,k*2+2,(l+r)/2,r);
	lazy_update_node2(k,l,r);
	return dat2[k].sum;
}

int main(void){
	scanf("%d",&n);
	n_=1;
	memset(dat,0,sizeof(data));
	memset(dat2,0,sizeof(data2));
	while(n_<n)n_*=2;
	string str;
	cin >> str;
	for(int i=0;i<n;i++){
		if(str[i]=='0'){
			add(-1,i,i+1);
			add2(-1,i,i+1);
		}else{
			add(1,i,i+1);
			add2(1,i,i+1);
		}
	}
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		int l,r,b;
		scanf("%d%d%d",&l,&r,&b);
		l--;
		if(b==0){	
			add(-1,l,r);
			add2(-1,l,r);
		}
		if(b==1){	
			add(1,l,r);
			add2(1,l,r);
		}
		printf("%d\n",query(0,n)-query2(0,n));
		/*
		printf("%d %d\n",query(0,n),query2(0,n));
		printf("query1\n");
		for(int i=0;i<n;i++){
			printf("%d ",query(i,i+1));
		}
		printf("\n");
		for(int i=0;i<n_*2-1;i++){
			printf("%d %d %d %d %d\n",i,dat2[i].lazy,dat2[i].l,dat2[i].r,dat2[i].sum);
		}
		printf("%d\n",query(0,n)-query2(0,n));
		*/
	}
	return 0;
}