Under construction

Still being built. Things may change

Meteorites

Time limit1sMemory limit512 MB

Summary
Given non-overlapping skyline rectangles and falling point meteorites with integer direction vectors, report the building each ray hits first or 0 if none.
Level

Medium7 of 10

Topics
Geometry, Binary search, Sorting, Implementation
Solved
No attempts yet

Problem

The Naro Space Center (NSC) has observed kk meteorites falling toward our city. The center wants to know which buildings the meteorites will hit.

Consider the two dimensional version of the problem first. The city has nn buildings. Every building is a rectangle whose bottom side lies on the xx axis, and no two rectangles overlap, so two distinct rectangles never meet. The rectangles are numbered from 1 to nn, and rectangle 1 is the leftmost one. If 1≤i<j≤n1 \le i < j \le n, then rectangle ii lies to the left of rectangle jj.

Buildings and meteorites

Every meteorite is a point. Each one falls along a slope given by a pair of integers (dx,dy)(dx, dy). A meteorite currently at (x,y)(x, y) with slope (dx,dy)(dx, dy) falls downward along the line through (x,y)(x, y) and (x+dx,y+dy)(x + dx, y + dy). In the figure above, meteorite A and meteorite B both head for building 3. When a meteorite touches any point on the boundary of a building, it explodes on the spot and disappears.

Given nn buildings and kk meteorites, write a program that reports which building each meteorite damages.

Input

Your program reads from standard input. The first line holds an integer nn (1≤n≤1000001 \le n \le 100000), the number of rectangles that represent the buildings of the city. Each of the next nn lines holds one rectangle in left to right order, and the rectangles are numbered from 1 to nn in that order. Each rectangle is given by three integers x1x_1, x2x_2, and hh (1≤x1<x2≤1091 \le x_1 < x_2 \le 10^9, 1≤h<1091 \le h < 10^9), where x1x_1 is the xx coordinate of the left side, x2x_2 is the xx coordinate of the right side, and hh is the yy coordinate of the top side. The bottom side of every rectangle lies on the xx axis, and rectangle ii lies to the left of rectangle jj whenever 1≤i<j≤n1 \le i < j \le n. The next line holds an integer kk (1≤k≤1000001 \le k \le 100000), the number of meteorites. Each of the next kk lines holds one meteorite, given by four integers xx, yy, dxdx, and dydy (1≤x≤1091 \le x \le 10^9, max⁡{h}<y≤109\max\{h\} < y \le 10^9, −1000≤dx≤1000-1000 \le dx \le 1000, −1000≤dy≤−1-1000 \le dy \le -1). Here (x,y)(x, y) is the current position of the meteorite, (dx,dy)(dx, dy) is the slope of the line it falls along, and max⁡{h}\max\{h\} is the largest yy coordinate among the top sides of the buildings.

Output

Your program writes to standard output. Print exactly one line for each meteorite, in the order given in the input. Each line holds the number of the building that the meteorite collides with. If no building is damaged, print 0.

Examples2

  1. Example 1

    Input
    5
    5 9 4
    10 12 12
    13 20 8
    22 24 5
    25 30 12
    2
    7 20 3 -4
    27 20 -3 -7
    
    Expected output
    3
    3
    
  2. Example 2

    Input
    4
    10 12 6
    13 20 15
    22 24 2
    25 30 9
    4
    21 18 0 -2
    27 20 3 -5
    11 20 20 -15
    6 20 1 -5
    
    Expected output
    0
    0
    2
    1