Electric Needs
Time limit1sMemory limit128 MB
Number all empty cells of a huge grid by Chebyshev distance to the nearest of up to 20 power plants, breaking ties by row then column, and answer positional queries.
- Level
Hard8 of 10
- Topics
- Binary search, Geometry, Math, Brute force
- Solved
- No attempts yet
Problem
You are building a new factory in your city. Because the factory has large electric needs, placing it close to a power plant matters, so you want to produce a prioritized list of the possible locations.
The candidate area is a rectangular grid of rows and columns of cells. Some cells contain a power plant. The factory occupies exactly one cell and may be placed on any empty cell (any cell that does not contain a power plant).
Rows are numbered to and columns to , so a cell is identified by two integers: cell is in row and column . The distance between cells and is , where is the absolute value of . The electric priority of a location is its minimum distance to a power plant.
Number every possible location with consecutive integers starting from , in ascending order of electric priority. Among locations with equal electric priority, order them by ascending row number; among those with equal priority and row, order them by ascending column number.
For intuition, in a grid the empty cells that lie at distance from a power plant all receive electric priority , the next ring outward receives priority , and so on; the empty cells are then numbered following the ordering rule above.
You will answer several queries about this prioritized list. Each query gives a position in the list, and you must report which location was assigned that position.
Input
The input contains several test cases.
Each test case begins with a line of three integers , and — the number of rows, the number of columns (), and the number of power plants (). Each of the next lines contains two integers and , the row and column of a power plant (, ). Within a test case all power plant locations are distinct. The next line contains one integer , the number of queries (). The following line contains integers , positions in the prioritized list ().
The last test case is followed by a line containing three zeros (0 0 0), which must not be processed.
Output
For each test case, output lines. For , line contains two integers: the row and the column of the location that was assigned position . After those lines, print a single line containing the character - (a hyphen).