Problem list
Log in
#coci00558 · COI 2020

Paint

Ruby V 3.0 s 512 MB

We will represent the drawing area of $MS$ Paint as a rectangular grid of unit squares divided into $R$ rows and $S$ columns. Each square of the grid represents a single pixel that can be colored in one of the $10^{5}$ different colors. When the user applies the so called bucket tool with color $A$ on a pixel (r, s) which is colored in the color $B$, then all pixels in the monochrome neighborhood of pixel (r, s) change their color to $A$. Monochrome neighborhood of a pixel (r, s) is a set of pixels that are reachable by walking from (r, s) in the four general directions (up, down, left and right) without changing the color of the pixel along the way. Note that the pixel (r, s) is itself a part of its monochrome neighborhood. You are given a starting image drawn in $MS$ Paint along with $Q$ instructions that should be executed in the given order. Each instruction tells you on which pixel should you apply the bucket tool and with what color. Your task is to how the image looks like after all instructions are executed.

Input

The first line contains integers $R$ and $S$ from the task description. Each of the next $R$ lines contains $S$ non-negative integers less than 100 000 that represent the starting image drawn in $MS$ Paint. More precisely, the $j$-th number in the $i$-th row of the image represents the color of the pixel (i, j). The next line contains an integer $Q$ from the task description. The $i$-th of the next $Q$ lines contains integers $r_{i}$, $s_{i}$ and $c_{i}$ ($1 \le r_{i} \le R$, $1 \le s_{i} \le S$, $0 \le c_{i} < 100,000$), which represent the $i$-th instruction that tells you to use the bucket tool with color $c_{i}$ on the pixel ($r_{i}$, $s_{i}$).

Output

You should output the final state of the image in the same format as it was given in the input.

Constraints

Subtask 1 (8 points): $1 \le R \cdot S \le 10,000$, $1 \le Q \le 10,000$

Subtask 2 (9 points): $R = 1$, $1 \le S \le 200,000$, $1 \le Q \le 100,000$

Subtask 3 (31 points): $1 \le R \cdot S \le 200,000$, $1 \le Q \le 100,000$ Each pixel will in every moment be colored either in color 0 or color 1.

Subtask 4 (52 points): $1 \le R \cdot S \le 200,000$, $1 \le Q \le 100,000 1$ od 8 October $3^{rd}$ 2020

Example 1

Input
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
Output
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

Example 2

Input
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
Output
1 1 1 3
1 1 2 2
1 1 1 2
3 3 1 3

Example 3

Input
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
Output
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

Clarification of the first example: The figure from the task description corresponds to the input of the first example. White color corresponds to number 0, red color corresponds to number 1, blue color corresponds to number 2, green color corresponds to number 3 and yellow color corresponds to number 4. 2 od 8

Source
COI 2020
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code