#P8427. [COI 2020] Paint

[COI 2020] Paint

Description

You must have used the one-click fill tool in drawing software. If we look at it on the pixel level: if all pixels in a large connected component have the same color, then recoloring one pixel will recolor all pixels in that connected component.

Now you are given an R×SR \times S pixel image and QQ recoloring operations:

  • Recolor (ri,si)(r_i, s_i) to color cic_i.

Output the entire image after all recoloring operations.

Input Format

The first line contains two integers R,SR, S, representing the size of the pixel image.
The next RR lines each contain SS integers, representing the initial colors of the image.
Line R+2R + 2 contains an integer QQ, representing the number of operations.
The next QQ lines each contain three integers ri,si,cir_i, s_i, c_i, representing one recoloring operation.

Output Format

Output RR lines, each containing SS integers, representing the recolored pixel image.

12 11
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 1 1 1 1
1 1 1 0 0 0 0 0 1 1 1
1 1 0 0 0 0 0 0 0 1 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 2 2 2 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
1 1 0 0 0 2 0 0 0 1 1
0 1 1 0 0 2 0 0 1 1 0
0 0 1 1 0 0 0 1 1 0 0
0 0 0 1 1 1 1 1 0 0 0
2
5 5 3
6 2 4
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 4 4 4 1 1 1 1
1 1 1 4 4 4 4 4 1 1 1
1 1 4 4 4 4 4 4 4 1 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 3 3 3 4 4 4 1
1 4 4 4 4 4 4 4 4 4 1
1 1 4 4 4 2 4 4 4 1 1
0 1 1 4 4 2 4 4 1 1 0
0 0 1 1 4 4 4 1 1 0 0
0 0 0 1 1 1 1 1 0 0 0
4 4
1 0 1 3
1 3 2 2
3 3 1 2
2 2 1 3
3
1 2 3
3 2 1
4 2 3
1 1 1 3
1 1 2 2
1 1 1 2
3 3 1 3
6 6
1 2 1 2 2 2
3 1 2 1 3 1
3 3 2 3 2 2
2 3 1 3 3 2
3 3 3 3 3 3
2 3 2 2 2 1
4
6 2 2
3 5 2
3 2 3
1 2 3
1 3 1 2 2 2
3 1 3 1 3 1
3 3 3 3 3 3
3 3 1 3 3 3
3 3 3 3 3 3
3 3 3 3 3 1

Hint

Explanation for Sample 1

Assume 00 is white, 11 is red, 22 is blue, 33 is green, and 44 is yellow, as shown in the figure below:

Constraints

This problem uses bundled testdata.

  • Subtask 1 (8 pts): 1R×S1041 \le R \times S \le 10^4, 1Q1041 \le Q \le 10^4.
  • Subtask 2 (9 pts): R=1R = 1, 1S2×1051 \le S \le 2 \times 10^5, 1Q1051 \le Q \le 10^5.
  • Subtask 3 (31 pts): 1R×S2×1051 \le R \times S \le 2 \times 10^5, 1Q1051 \le Q \le 10^5, and there are only colors 00 and 11.
  • Subtask 4 (52 pts): 1R×S2×1051 \le R \times S \le 2 \times 10^5, 1Q1051 \le Q \le 10^5.

For 100%100\% of the testdata, 1riR1 \le r_i \le R, 1siS1 \le s_i \le S, 0ci1050 \le c_i \le 10^5, and the color range is [0,105][0, 10^5].

Notes

Translated from Croatian Olympiad in Informatics 2020 A Paint

Translated by ChatGPT 5