#P1766. Liquid Drop Simulation

    ID: 15051 Type: Default 1000ms 256MiB

Liquid Drop Simulation

Liquid Drop Simulation

A drop of liquid falls vertically from above and may encounter several line segments in the plane. These segments, which may or may not intersect, form tracks. When the drop meets a track, it will roll along the track toward the lower endpoint and then leave the track from that point. If the drop does not hit any track, it will fall vertically downward unaffected. Given the starting x-coordinate of the drop and a list of segments (each specified by the coordinates of its two endpoints), simulate the process and determine the final x-coordinate from which the drop exits the system.

Note: The drop always falls vertically from its current position until it hits a segment. When hitting a segment, the drop rolls to the lower end (i.e. the endpoint with the smaller y-coordinate) before continuing its fall.

The line segments are provided in arbitrary order. Some segments may be vertical; for a vertical segment, if the x-coordinate of the drop equals the x-coordinate of the segment, then the drop will contact the segment at its top endpoint (the endpoint with the larger y-value) and then roll to the lower endpoint.

The simulation ends when the drop falls without encountering any further segments. Output the x-coordinate of the drop at that moment.

inputFormat

The input starts with a line containing two numbers: the starting x-coordinate (a real number) of the drop and an integer n (n ≥ 0) representing the number of segments. Each of the following n lines contains four numbers: x1, y1, x2, y2 – the coordinates of the two endpoints of a segment.

You can assume that all coordinates are given with at most 6 digits after the decimal point and that segments may be oriented arbitrarily.

outputFormat

Output a single number: the final x-coordinate (if it is an integer output it without any decimals) from which the drop leaves the system.

sample

5 0
5