Balloon
Time limit2sMemory limit512 MB
Given N disjoint ceiling segments, trace each vertically rising balloon as it sticks to horizontal segments or slides to the higher endpoint of tilted ones, and report the final resting point or escape x coordinate.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Binary search, Simulation
- Solved
- No attempts yet
Problem
After a programming contest, the balloons that come loose and float up to the ceiling of the hall have to be collected. The contract with the owner of the hall requires the hall to be cleaned right after the event, and a fine applies otherwise. This year the organizers obtained the design drawing of the ceiling in advance. They want to know, for a balloon released at a given point of the floor, whether the ceiling blocks it and where it stops, or whether it escapes out of the hall.
Seen from the side, the ceiling is line segments. No two of these segments have a point in common.
A balloon is a point. It is released on the floor at and rises straight up. What happens next depends on the first segment it touches. The endpoints of a segment belong to that segment, so a balloon that rises exactly into an endpoint touches it.
- If the segment is horizontal, the balloon gets stuck there.
- If the segment is tilted, the balloon glides along it to the highest endpoint of that segment, is released from that point, and rises straight up again. It can then touch another segment, or it can escape out of the hall.
The figure below shows one ceiling. The four marked points of the floor, a, b, c and d, are at , , and .

For that ceiling, the balloons released at a and at b both get stuck at , the balloon released at c gets stuck at , and the balloon released at d is not blocked and escapes out of the hall at .
Read the description of the ceiling and answer queries about the final position of balloons released on the floor.
Input
The first line contains two integers and , the number of segments that describe the ceiling and the number of queries.
Each of the next lines contains four integers , , , , describing a ceiling segment whose endpoints are and .
Each of the next lines contains one integer , a query asking what happens to a balloon released at the point .
Constraints
- No two of the endpoint x coordinates are equal, over all segments.
- No two segments have a point in common.
Output
Print one line for each query, in the order the queries are given. If the balloon escapes the hall, the line contains one integer, the x coordinate at which the balloon escapes. Otherwise the line contains two integers and separated by one space, the position at which the balloon gets stuck on the ceiling.