#P5869. [SEERC 2018] Matrix Queries

[SEERC 2018] Matrix Queries

Description

Given a 2n×2n2^n \times 2^n matrix, initially every cell is white. Each cell can be either white or black. Define the value of a matrix as follows:

  1. If the matrix is monochromatic, then its value is 11 coin.
  2. Otherwise, split the matrix into 44 equal-sized submatrices. The value of the matrix is the sum of the values of the submatrices plus 11 coin.

You are given qq queries. Each query provides a row/column index xx. You need to flip the color of every cell in that row/column (black becomes white, white becomes black), and then compute the value of the new matrix after the change.

Input Format

The first line contains two integers nn and q (0n20,1q106)q \ (0 \leq n \leq 20, 1 \leq q \leq 10^6), meaning the matrix size is 2n×2n2^n \times 2^n and there are qq queries.

The next qq lines each contain two integers tt and x (0t1,1x2n)x \ (0 \leq t \leq 1, 1 \leq x \leq 2^n). If t=0t=0, flip the colors of row xx; otherwise, flip the colors of column xx.

Output Format

For each query, output one line with the answer.

2 7
1 3
0 2
1 1
1 4
0 4
0 3
1 1
13
17
21
17
21
17
13

Hint

In the sample, the matrix after each query is shown in the figure below:

Sample Figure

Translated by ChatGPT 5