Matrix Queries
Time limit1.5sMemory limit512 MB
Given a 2^n by 2^n white matrix, toggle whole rows or columns and report its quadtree price after each of up to 10^6 queries.
- Level
Medium7 of 10
- Topics
- Matrix, Math, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
You are given a matrix of size , initially painted white. The color of a cell is either black or white. The price of a matrix is defined as follows:
- If the matrix is painted with only one color, the price is 1 coin.
- Otherwise, split the matrix into 4 submatrices of equal size, and the price of the matrix is the sum of the prices of the submatrices plus 1 coin.
You are given queries. Each query gives the number of a row or column, and you have to change the color of all cells in this row or column (a white cell becomes black, and a black cell becomes white) and find the price of the new matrix.
Input
The first line contains two integers and (, ), where means the matrix has size and means the number of queries.
Each of the next lines contains two integers and (, ). If , the -th row is changed; otherwise, the -th column.
Output
For each query, print the price of the matrix.
Hint
In the example, the matrix looks as follows after each query:
