PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000199. Sơn lưới theo hàng và cột (E-liter)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sơn lưới theo hàng và cột (E-liter)

E-liter

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, an N×NN\times N grid contains only white cells. Rows are numbered from 11 to NN from top to bottom, and columns from 11 to NN from left to right.

Process QQ operations in their given order. An operation is one of the following:

  • 1 R: Paint every cell in row RR black, regardless of its current color.
  • 2 C: Paint every cell in column CC white, regardless of its current color.

Operations modify the current grid and may paint the same row or column repeatedly. A cell's current color is determined by the most recent operation that affected it.

After each operation, determine the number of black cells in the entire grid.

Input

  • The first line contains two integers N,QN,Q, the grid size and the number of operations.
  • Each of the next QQ lines contains 1 R or 2 C, giving the operation type and the corresponding row or column index.
  • Operations are applied in the order in which they appear.

Output

Print exactly QQ lines. Line ii contains the number of black cells immediately after operation ii. The count can be as large as N2N^2, so a sufficiently wide integer type is required.

Subtasks

  • Subtask 1 (20 points): 1≤N≤81\le N\le 8; 1≤Q≤401\le Q\le 40; 1 R always has 1≤R≤N1\le R\le N; 2 C always has 1≤C≤N1\le C\le N.
  • Subtask 2 (30 points): 1≤N≤15001\le N\le 1500; 1≤Q≤30001\le Q\le 3000; 1 R always has 1≤R≤N1\le R\le N; 2 C always has 1≤C≤N1\le C\le N.
  • Subtask 3 (50 points): 1≤N≤3000001\le N\le 300000; 1≤Q≤3000001\le Q\le 300000; 1 R always has 1≤R≤N1\le R\le N; 2 C always has 1≤C≤N1\le C\le N.

Examples

Example 1

Input

3 4
1 1
1 3
2 2
1 1

Output

3
6
4
5

Explanation

The 3×33\times3 grid is initially white. Painting row 1 makes 33 cells black. Painting row 3 adds 33, bringing the total to 66. Painting column 2 white removes two black cells, one from each painted row, leaving 44. Repainting row 1 turns cell (1,2)(1,2) black again while the other two cells in that row are already black, so the total becomes 55. Thus the outputs are 3, 6, 4, and 5.

Example 2

Input

300000 1
2 300000

Output

0

Explanation

All cells of the 300000×300000300000\times300000 grid are initially white. Painting column 300000300000 white changes no cell, so the number of black cells remains 00.