Meteorites
Time limit1sMemory limit512 MB
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 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 buildings. Every building is a rectangle whose bottom side lies on the axis, and no two rectangles overlap, so two distinct rectangles never meet. The rectangles are numbered from 1 to , and rectangle 1 is the leftmost one. If , then rectangle lies to the left of rectangle .

Every meteorite is a point. Each one falls along a slope given by a pair of integers . A meteorite currently at with slope falls downward along the line through and . 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 buildings and meteorites, write a program that reports which building each meteorite damages.
Input
Your program reads from standard input. The first line holds an integer (), the number of rectangles that represent the buildings of the city. Each of the next lines holds one rectangle in left to right order, and the rectangles are numbered from 1 to in that order. Each rectangle is given by three integers , , and (, ), where is the coordinate of the left side, is the coordinate of the right side, and is the coordinate of the top side. The bottom side of every rectangle lies on the axis, and rectangle lies to the left of rectangle whenever . The next line holds an integer (), the number of meteorites. Each of the next lines holds one meteorite, given by four integers , , , and (, , , ). Here is the current position of the meteorite, is the slope of the line it falls along, and is the largest 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.