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:
/*  -*- coding: utf-8 -*-
 *
 * 2820.cc: Binary Sequence
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144

/* typedef */

template <typename T, const int MAX_E2>
struct SegTreeFG {
  int n, e2;
  int ls[MAX_E2], rs[MAX_E2];
  T fs[MAX_E2], gs[MAX_E2];
  bool dfs[MAX_E2];
  SegTreeFG() {}

  void init(int _n) {
    n = _n;
    for (e2 = 1; e2 < n; e2 <<= 1);
    for (int i = 0, j = e2 - 1; i < e2; i++, j++)
      ls[j] = i, rs[j] = i + 1;
    for (int j = e2 - 2; j >= 0; j--)
      ls[j] = ls[j * 2 + 1], rs[j] = rs[j * 2 + 2];
  }

  T &get(int i) { return fs[e2 - 1 + i]; }
  void set(int i, T v) { get(i) = v; }

  T f() { return fs[0]; }
  T g() { return gs[0]; }
  
  void setall() {
    for (int j = e2 - 2; j >= 0; j--) {
      int j0 = j * 2 + 1, j1 = j0 + 1;
      fs[j] = fs[j0] + fs[j1];
      gs[j] = gs[j0] + gs[j1] + get(rs[j0] - 1) * get(ls[j1]);
    }
  }

  T get_range(int r0, int r1, int k) {
    int i0 = ls[k], i1 = rs[k];
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return fs[k];

    int k0 = k * 2 + 1, k1 = k0 + 1;
    if (dfs[k]) {
      fs[k0] = fs[k1] = fs[k] / 2;
      gs[k0] = gs[k1] = gs[k] / 2;
      dfs[k0] = dfs[k1] = true;
      dfs[k] = false;
    }

    T v0 = get_range(r0, r1, k0);
    T v1 = get_range(r0, r1, k1);
    return v0 + v1;
  }
  T get_range(int r0, int r1) { return get_range(r0, r1, 0); }

  void set_range(int r0, int r1, T v, int k) {
    int i0 = ls[k], i1 = rs[k];
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      fs[k] = v * (i1 - i0);
      gs[k] = v * (i1 - i0 - 1);
      dfs[k] = true;
      return;
    }

    int k0 = k * 2 + 1, k1 = k0 + 1;
    if (dfs[k]) {
      fs[k0] = fs[k1] = fs[k] / 2;
      gs[k0] = gs[k1] = gs[k] / 2;
      dfs[k0] = dfs[k1] = true;
      dfs[k] = false;
    }

    set_range(r0, r1, v, k0);
    set_range(r0, r1, v, k1);

    fs[k] = fs[k0] + fs[k1];
    gs[k] =
      gs[k0] + gs[k1] +
      get_range(rs[k0] - 1, rs[k0]) * get_range(ls[k1], ls[k1] + 1);
  }
  void set_range(int r0, int r1, T v) { set_range(r0, r1, v, 0); }
};

/* global variables */

char s[MAX_N + 4];
SegTreeFG<int,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n, q;
  scanf("%d%s%d", &n, s, &q);

  st.init(n);

  for (int i = 0; i < n; i++) st.set(i, s[i] - '0');
  st.setall();
  //printf("f=%d, g=%d\n", st.f(), st.g());

  while (q--) {
    int li, ri, bi;
    scanf("%d%d%d", &li, &ri, &bi);
    li--;

    st.set_range(li, ri, bi);
    printf("%d\n", st.f() - st.g());
  }

  return 0;
}