#K5171. Apply Commands on Grid
Apply Commands on Grid
Apply Commands on Grid
You are given a grid with N rows and M columns, initially filled with zeros. You will then be provided with K commands. Each command is given as five integers x1, y1, x2, y2, and v which indicates that you should add the value v to all cells in the subgrid whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). Your task is to apply all the commands sequentially and output the final state of the grid.
The transformation for each command can be mathematically described as follows:
$$\textbf{For every command:} \quad \forall i \in [x1, x2],\, \forall j \in [y1, y2], \quad grid[i][j] \mathrel{+}= v $$Make sure to perform 1-indexed access adjustments as the commands use 1-indexing, while your data structures might be 0-indexed.
inputFormat
The first line contains three integers N, M, and K separated by spaces, where:
- N is the number of rows in the grid.
- M is the number of columns in the grid.
- K is the number of commands.
The following K lines each contain five integers: x1, y1, x2, y2, and v, representing a command to add v to every cell in the subgrid from (x1, y1) to (x2, y2) (both inclusive).
outputFormat
Output the final grid state after all commands have been applied. The output should consist of N lines, each containing M space-separated integers representing a row of the grid.
## sample3 3 3
1 1 2 2 1
2 2 3 3 2
1 3 3 3 3
1 1 3
1 3 5
0 2 5
</p>