Matrix Queries

Time limit1.5sMemory limit512 MB

Summary
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 2n×2n2n \times 2n, initially painted white. The color of a cell is either black or white. The price of a matrix is defined as follows:

  1. If the matrix is painted with only one color, the price is 1 coin.
  2. 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 qq queries. Each query gives the number xx 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 nn and qq (0≤n≤200 \le n \le 20, 1≤q≤1061 \le q \le 10^6), where nn means the matrix has size 2n×2n2n \times 2n and qq means the number of queries.

Each of the next qq lines contains two integers tt and xx (0≤t≤10 \le t \le 1, 1≤x≤2n1 \le x \le 2n). If t=0t = 0, the xx-th row is changed; otherwise, the xx-th column.

Output

For each query, print the price of the matrix.

Hint

In the example, the matrix looks as follows after each query:

Examples1

  1. Example 1

    Input
    2 7
    1 3
    0 2
    1 1
    1 4
    0 4
    0 3
    1 1
    
    Expected output
    13
    17
    21
    17
    21
    17
    13