Counting Regions

시간 제한2초메모리 제한1024 MB

요약
2N-2번의 행/열 칠하기 연산 각각이 끝난 뒤 단색 연결 영역의 개수를 구하고, 연산 색을 범위로 뒤집는 누적 질의를 처리한다.
난이도

어려움10점 중 9점

유형
세그먼트 트리, 조합론, 누적 합, 행렬
정답자
아직 제출이 없습니다

문제

You are given an N×NN\times N grid. A cell in the ii-th row and jj-th column is denoted as (i,j)(i,j). Initially, every cell on the grid is colored white.

We will color each cell using 2N−22N-2 operations. The ii-th operation is denoted as (d_i,x_i,c_i)(d\_i,x\_i,c\_i). An operation of form (d,x,c)(d,x,c) indicates the following:

  • For d=0d=0, we color cells in the xx-th column. For d=1d=1, we color cells in a xx-th row.
  • For c=0c=0, we color the column/row white. For c=1c=1, we color the column/row black.

It is guaranteed that if you carry out the operation in the order, each of the 2,3,4,…,N2,3,4,\ldots ,N-th row will be colored exactly once in that order, and each of the 2,3,4,…,N2,3,4,\ldots ,N-th column will be colored exactly once in that order. Note that no operation colors the first row and the first column. Formally, the following holds:

  • For all integers i,ji,j such that 0≤i≤10\le i\le 1 and 2≤j≤N2\le j\le N, there is a unique integer 1≤k≤2N−21\le k\le 2N-2 such that (d_k,x_k)=(i,j)(d\_k,x\_k) =(i,j).
  • For all integers i,ji,j such that 1≤i\<j≤2N−21\le i\<j\le 2N-2 and d_i=d_jd\_i=d\_j, x_i\<x_jx\_i\<x\_j holds.

A region is defined as maximal sections of neighboring cells of the same color, where two cells are considered neighbors if they share an edge. You need to find the number of regions after performing each 2N−22N-2 operation in the given order.

Of course, this problem is too easy, so we prepared QQ queries for you! Each query is denoted as 33 integers (z,l,r)(z,l,r). After the query, you should set c_i=1−c_ic\_i=1-c\_i for all ii-th operation where l≤x_i≤r,d_i=zl\le x\_i\le r,d\_i=z holds. Then, with the changed operation sequence, you need to find the number of regions after performing each 2N−22N-2 operation. Note that the queries are cumulative.

입력

The first line contains two space-separated integers NN and QQ.

The ii-th line of the next 2N−22N-2 lines contains three space-separated integers d_id\_i, x_ix\_i, c_ic\_i.

The ii-th line of next QQ lines contains three space-separated integer zz, ll, rr, describing the ii-th query.

출력

After each query, output a line with a single integer, which is the number of regions.

제한

  • 2≤N,Q≤2×1052\leq N,Q\leq 2\times 10^5
  • For each operation, 0≤d_i,c_i≤10\leq d\_i,c\_i\leq 1 and 2≤x_i≤N2\leq x\_i\leq N
  • For all integers i,ji,j such that 0≤i≤10\le i\le 1 and 2≤j≤N2\le j\le N, there is a unique integer 1≤k≤2N−21\le k\le 2N-2 such that (d_k,x_k)=(i,j)(d\_k,x\_k) =(i,j).
  • For all integers i,ji,j such that 1≤i\<j≤2N−21\le i\<j\le 2N-2 and d_i=d_jd\_i=d\_j, x_i\<x_jx\_i\<x\_j holds.
  • 0≤z≤10\leq z\leq 1; 2≤l≤r≤N2\leq l\leq r\leq N for each query.

힌트

State of the grid after each operation for example 2

예제2

  1. 예제 1

    입력
    5 7
    1 2 0
    0 2 0
    1 3 1
    1 4 0
    1 5 1
    0 3 1
    0 4 1
    0 5 1
    1 3 5
    0 4 4
    0 2 5
    1 2 3
    1 5 5
    1 3 4
    0 2 4
    
    예상 출력
    3
    5
    5
    5
    5
    6
    4
    
  2. 예제 2

    입력
    3 6
    0 2 0
    1 2 1
    0 3 0
    1 3 1
    0 2 2
    0 2 3
    0 3 3
    1 2 2
    1 2 3
    1 3 3
    
    예상 출력
    3
    2
    2
    2
    2
    2