Shower
Time limit2sMemory limit1024 MB
Rain lowers cell heights over Q days; after each shower, report the lowest cell in the water-connected region containing the shower cell, breaking ties by the earliest rain time.
- Level
Hard8 of 10
- Topics
- Union-find, Simulation, Implementation, Sorting
- Solved
- No attempts yet
Problem
Sinchon has a plain of size , called Sinchon Plain. Showers fall on this plain for days. The top-left cell of Sinchon Plain has coordinates , and the bottom-right cell has coordinates .
A shower falls on cell . After it falls, the ground height decreases by and water remains in that cell. Ground heights can be negative.
If adjacent cells contain water, the water in those cells becomes connected. Two cells are adjacent when they share an edge.
After each shower, the Sinchon Plain Management Headquarters installs a water quality inspection robot at that point. The robot moves freely through the connected water and finishes its inspection at the cell with the lowest height. If several cells have the lowest height, it finishes at the cell that received rain the longest ago.
Given the locations where showers fall over days, write a program that prints, for each day, the point where the robot finishes its inspection.
Input
The first line gives (), (), and (), separated by spaces.
Lines 2 to give the height of each cell of Sinchon Plain () as integers.
Lines to give, in order, the coordinates () and () of the cell where rain falls, and the amount () by which the ground height decreases, as integers.
Output
For each day, print the coordinates of the point where the robot finishes its inspection, one per line, in order.
Hint
The following is the figure for Sample 1.
