Move that Mouse AGAIN
Time limit3sMemory limit128 MB
Given up to 50,000 axis-aligned rectangles in a fixed bottom-to-top stacking order, process 50,000 point clicks, printing the topmost window at each point and moving it to the top of the stack.
- Level
Hard9 of 10
- Topics
- Segment tree, Geometry, Sorting, Implementation
- Solved
- No attempts yet
Problem
You have a screen with rows and columns, where and .
There are () rectangular windows. Window is described by its top-left corner and its bottom-right corner , and you may assume and (so no window is empty). A window includes every point on its border: it contains exactly the points with and .
The windows are given in stacking order from bottom to top: wherever two windows overlap, the one listed later in the input is drawn on top of the earlier one (not necessarily immediately after it). Windows are numbered to in input order.
A mouse can click on the screen. It receives () instructions; each instruction moves the mouse to a position with and and clicks there. When the mouse clicks, the window that is visible at that position — the topmost window covering it — is moved to the very top, becoming fully visible and the focused window. If no window covers that position, the stacking order is unchanged.
After each click, report which window that click brought into focus.
Input
The first line contains the integer , the number of columns. The second line contains the integer , the number of rows. The third line contains the integer , the number of windows.
Each of the next lines contains four integers , , , — the top-left and bottom-right coordinates of a window. The windows are numbered to in this order.
The next line contains the integer . Each of the next lines contains two integers and , the new position of the mouse for that click.
Output
Print lines. The -th line contains an integer with : if , then click landed on window and moved it to the top; if , then click was on a position with no window.
Hint
As an illustration of the rule: on a -wide, -tall screen holding three windows, a click at falls on window and raises it to the top; a click at touches no window, so is reported; a click at falls on window and raises it to the top.