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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;


struct Value1 {
	int f;
	int g;
	int l;
	int r;
};
typedef int Value2;
const Value1 Zero{ 0,0,0,0 };

struct Node {
	Value1 sum;//??´??°????????????. ?????????????????§??????????????????????????¨????????????????????????????????????.
	Value2 lazy;	//?????¶???????????????????????????????????????
	Node() :sum(Zero) {
		lazy = 0;
	}
};
struct lazy_segtree {
	int N;
	vector<Node>dat;
	lazy_segtree(int n) :N(1) {
		while (N < n) N *= 2;
		dat.resize(2 * N);
	}

	void lazy_func(const int k, const int a, const int b) {
		//dat[k].sum += dat[k].lazy * (b - a + 1);
		
		dat[k].sum = 
			dat[k].lazy == 1 ? Value1{ b - a + 1,max(0,b - a),1,1 }:
			dat[k].lazy == -1 ? Value1{ 0,0,0,0 }:
			dat[k].sum;
	}

	Value1 connect(const Value1 l, const Value1 r) {
		//dat[k].sum = dat[2 * k].sum + dat[2 * k + 1].sum;
		Value1 v{ l.f + r.f,max(0,l.g + r.g + l.r*r.l),l.l,r.r };
		return v;
	}

	// inline??????????????¨??§???????????¨????????????!(??????)
	inline void lazy_evaluate_node(int k, int a, int b) {
		lazy_func(k, a, b);
		if (k < N) { // 2*k(???????????????) < 2*N (???????????°) ?????????????????§. ???????????????????????????????????¬???????????¨??????.
			if (dat[k].lazy) {
				dat[2 * k].lazy = dat[k].lazy;	//?¬???????????????¬??????????????£?????????.
				dat[2 * k + 1].lazy = dat[k].lazy;
			}
		}
		dat[k].lazy = 0;
	}

	inline void update_node(int k) { // k???????????¢?????????????????????????????¨?????????. ????????\????????¨????????????????????????????????????????????????????????§if???????????????.
		dat[k].sum = connect(dat[2 * k].sum, dat[2 * k + 1].sum);

	}

	// update(l,r,v) := [l,r]?????´??°??????. ?????????1-indexed.
	void update(int l, int r, Value2 v, int k = 1, int a = 1, int b = -1) {
		if (b == -1)b = N;
		if (l < 0 || r<0)assert(false);
		lazy_evaluate_node(k, a, b); 	// ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\?.

		if (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???
			return;
		if (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???
			dat[k].lazy = v;
			lazy_evaluate_node(k, a, b); //???????????¶???????????¨???????????¨?????????????????§.
			return;
		}

		int m = (a + b) / 2;
		update(l, r, v, 2 * k, a, m);
		update(l, r, v, 2 * k + 1, m + 1, b);
		update_node(k);
	}

	// get(l,r) := [l,r]?????????????????¨?????????????????????. ?????????1-indexed.
	Value1 get(int l, int r, int k = 1, int a = 1, int b = -1) {
		if (b == -1)b = N;

		if (l < 0 || r<0)assert(false);
		lazy_evaluate_node(k, a, b); // ??¨?????????????????£???????????????????????????????????§?????¬?????¨???????\?.

		if (b < l || r < a) //[a,b]??¨[l,r]???????????????????????´???
			return Zero;

		if (l <= a && b <= r) { // [l,r]???[a,b]????????¨???????????§????????´???
			return dat[k].sum;
		}

		int m = (a + b) / 2;
		Value1 vl = get(l, r, 2 * k, a, m);
		Value1 vr = get(l, r, 2 * k + 1, m + 1, b);
		update_node(k);
		return connect(vl, vr);
	}
};

int main() {
	int N; cin >> N;
	string x; cin >> x;
	int Q; cin >> Q;
	lazy_segtree tree(x.size());
	for (int i = 0; i < x.size();++i){
		tree.update(i + 1, i + 1, x[i]=='1'?1:-1);
	}
	while (Q--) {
		int l, r, b; cin >> l >> r >> b;
		tree.update(l, r, b==1?1:-1);
		auto t=tree.get(1, x.size());
		int ans = t.f - t.g;
		cout << ans << endl;
	}
	return 0;
}