Parade
Time limit1sMemory limit128 MB
Maintain a list of N perimeter-rotation commands on a 4x4 grid under Q cumulative point updates, printing the resulting grid after each update.
- Level
Medium7 of 10
- Topics
- Simulation, Implementation, Array, Prefix sum
- Solved
- No attempts yet
Problem
May 9 is Victory Day, when an annual victory parade marches through Red Square. To rehearse it digitally, you must track how a single formation changes.
A formation is a grid. The people start labelled through in row-major order:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
A sequence of commands is then issued. Each command is a triple meaning:
Rotate every person on the perimeter of the square whose upper-left corner is at row , column clockwise by one position.
For example, command turns the initial grid into:
5 1 3 4
6 2 7 8
9 10 11 12
13 14 15 16
Command turns the initial grid into:
1 2 3 4
5 10 6 7
9 14 11 8
13 15 16 12
Command turns the initial grid into:
5 1 2 3
9 6 7 4
13 10 11 8
14 15 16 12
You are given the original sequence of commands. You then perform edits. Each edit permanently rewrites one command:
Change the -th command to .
Every edit is cumulative and permanent: it modifies the same command list that previous edits already changed. After each edit, output what the formation looks like once all commands (with every edit applied so far) have been executed in order on the initial grid.
Input
The first line contains two integers and () — the number of commands and the number of edits.
Each of the next lines contains three integers , , describing one rotation command, with , , and .
Each of the next lines contains four integers , , , : the -based index of the command to rewrite, followed by its new description (subject to the same bounds on , , ).
Output
For each edit, print the final configuration after applying every edit so far, as lines of space-separated integers. Print the blocks for the edits one after another, with no blank lines between them.
Notes
- Each edit changes the command list in place, and the changes accumulate: the -th edit is applied on top of edits .
- A command with acts on a single cell and therefore leaves the formation unchanged.
- The answer after each edit is uniquely determined, so the required output is exact.