#abc346e. E - Paint

E - Paint

Score: 450450 points

问题陈述

有一个 HHWW 列的网格。最初,所有单元格都涂有颜色 00

你将按照顺序 i=1,2,,Mi = 1, 2, \ldots, M 执行以下操作。

  • 如果 Ti=1T_i = 1,则用颜色 XiX_i 重新涂画第 AiA_i 行的所有单元格。

  • 如果 Ti=2T_i = 2,则用颜色 XiX_i 重新涂画第 AiA_i 列的所有单元格。

完成所有操作后,对于网格上存在的每种颜色 ii,找出涂有颜色 ii 的单元格数量。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There is a grid with HH rows and WW columns. Initially, all cells are painted with color 00.

You will perform the following operations in the order i=1,2,,Mi = 1, 2, \ldots, M.

  • If Ti=1T_i = 1, repaint all cells in the AiA_i-th row with color XiX_i.

  • If Ti=2T_i = 2, repaint all cells in the AiA_i-th column with color XiX_i.

After all operations are completed, for each color ii that exists on the grid, find the number of cells that are painted with color ii.

Constraints

  • 1H,W,M2×1051 \leq H, W, M \leq 2 \times 10^5
  • Ti{1,2}T_i \in \lbrace 1, 2 \rbrace
  • 1AiH1 \leq A_i \leq H for each ii such that Ti=1T_i = 1,
  • 1AiW1 \leq A_i \leq W for each ii such that Ti=2T_i = 2.
  • 0Xi2×1050 \leq X_i \leq 2 \times 10^5
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

HH WW MM

T1T_1 A1A_1 X1X_1

T2T_2 A2A_2 X2X_2

\vdots

TMT_M AMA_M XMX_M

Output

Let KK be the number of distinct integers ii such that there are cells painted with color ii. Print K+1K + 1 lines.

The first line should contain the value of KK.

The second and subsequent lines should contain, for each color ii that exists on the grid, the color number ii and the number of cells painted with that color.

Specifically, the (i+1)(i + 1)-th line (1iK)(1 \leq i \leq K) should contain the color number cic_i and the number of cells xix_i painted with color cic_i, in this order, separated by a space.

Here, print the color numbers in ascending order. That is, ensure that c1<c2<<cKc_1 < c_2 < \ldots < c_K. Note also that xi>0x_i > 0 is required.

Sample Input 1

3 4 4
1 2 5
2 4 0
1 3 3
1 3 2

Sample Output 1

3
0 5
2 4
5 3

The operations will change the colors of the cells in the grid as follows:

0000   0000   0000   0000   0000
0000 → 5555 → 5550 → 5550 → 5550 
0000   0000   0000   3333   2222

Eventually, there are five cells painted with color 00, four with color 22, and three with color 55.

Sample Input 2

1 1 5
1 1 1
1 1 10
2 1 100
1 1 1000
2 1 10000

Sample Output 2

1
10000 1

Sample Input 3

5 5 10
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
2 1 6
2 2 7
2 3 8
2 4 9
2 5 10

Sample Output 3

5
6 5
7 5
8 5
9 5
10 5

update @ 2024/5/16 17:18:06