This page is still under construction.

Parts of this page are still being built. What you see may change.

Move that Mouse AGAIN

Time limit3sMemory limit128 MB

Summary
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 RR rows and CC columns, where 1≤R≤10,0001 \le R \le 10{,}000 and 1≤C≤10,0001 \le C \le 10{,}000.

There are nn (1≤n≤50,0001 \le n \le 50{,}000) rectangular windows. Window ii is described by its top-left corner (xl,yt)(x_l, y_t) and its bottom-right corner (xr,yb)(x_r, y_b), and you may assume 1≤xl<xr≤C1 \le x_l < x_r \le C and 1≤yb<yt≤R1 \le y_b < y_t \le R (so no window is empty). A window includes every point on its border: it contains exactly the points (x,y)(x, y) with xl≤x≤xrx_l \le x \le x_r and yb≤y≤yty_b \le y \le y_t.

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 11 to nn in input order.

A mouse can click on the screen. It receives mm (1≤m≤50,0001 \le m \le 50{,}000) instructions; each instruction moves the mouse to a position (x,y)(x, y) with 1≤x≤C1 \le x \le C and 1≤y≤R1 \le y \le R 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 CC, the number of columns. The second line contains the integer RR, the number of rows. The third line contains the integer nn, the number of windows.

Each of the next nn lines contains four integers xlx_l, yty_t, xrx_r, yby_b — the top-left and bottom-right coordinates of a window. The windows are numbered 11 to nn in this order.

The next line contains the integer mm. Each of the next mm lines contains two integers xx and yy, the new position of the mouse for that click.

Output

Print mm lines. The ii-th line contains an integer viv_i with 0≤vi≤n0 \le v_i \le n: if vi>0v_i > 0, then click ii landed on window viv_i and moved it to the top; if vi=0v_i = 0, then click ii was on a position with no window.

Hint

As an illustration of the rule: on a 200200-wide, 100100-tall screen holding three windows, a click at (60,20)(60, 20) falls on window 11 and raises it to the top; a click at (150,90)(150, 90) touches no window, so 00 is reported; a click at (150,30)(150, 30) falls on window 22 and raises it to the top.

Examples3

  1. Example 1

    Input
    200
    100
    3
    50 50 80 20
    70 60 180 10
    10 90 100 40
    3
    60 20
    150 90
    150 30
    
    Expected output
    1
    0
    2
    
  2. Example 2

    Input
    10
    10
    1
    2 8 5 3
    3
    2 3
    1 1
    5 8
    
    Expected output
    1
    0
    1
    
  3. Example 3

    Input
    10
    10
    2
    1 10 6 1
    5 10 10 1
    4
    5 5
    3 5
    5 5
    8 5
    
    Expected output
    2
    1
    1
    2