Given two observers' angular-direction and distance-range observations, decide whether a consistent star placement exists and find the maximum number of stars.
Medium7GeometryIntervalsGreedySortingNo attempts yetTime limit2sMemory limit128 MBCoders and High are astronomers who live in the two dimensional coordinate plane. Coders stands still at the point (xC,0) and High stands still at the point (xH,0), and both of them watch the stars floating in the sky, that is, the part of the plane where y>0.
Each astronomer takes their own position as the reference point and writes the coordinates of a point in the sky as (r,θ). Here r>0 is the straight line distance between the observer and the point, and 0∘<θ<180∘ is the size of the angle of the ray from the observer to the point, measured counterclockwise from the positive direction of the x axis.

From the observer's reference point, the star in the picture above is at (5,65∘).
The reason the two write positions in such an awkward way is a limit of their equipment. It reports the direction θ of a star exactly, but it cannot report the straight line distance r to the star. Instead it analyzes the waveform of the light and reports only a range of distances where a star can be. A single observation therefore tells the observer one fact: in the measured direction, inside the measured range of distances, there is at least one star.

An observer measures only the direction of a star and a range of distances, both relative to their own position.
The two know this limit well, so they decided to work together and make up for the error in their measurements. Each of them observed every light source in the sky and recorded the result, and now they want to combine the two records and recover the exact positions of the stars. They treat every position where a star may or may not be as a position where a star is floating, so that not a single star is missed. At most one star sits at the same coordinates.
Write a program that decides whether the positions of the stars in the sky can be recovered so that both records are satisfied, and if they can, finds the largest number of stars the two can recover.
The first line contains two integers xC and xH (0≤xC<xH≤5000), separated by a space, giving the positions where Coders and High stand. Coders stands still at (xC,0) and High stands still at (xH,0).
The second line contains the number of observations n (1≤n≤100000) made by Coders.
Each of the next n lines contains one observation of Coders, given as four integers θxC, θyC, sC, eC (−5000<θxC<5000, 0<θyC<10000, 0<sC≤eC<5000) separated by spaces. It means that the direction vector of the ray from Coders toward a star is (θxC,θyC), and that the straight line distance between the star and Coders is at least sC and at most eC.
The next line contains the number of observations m (1≤m≤100000) made by High.
Each of the next m lines contains one observation of High, given as four integers θxH, θyH, sH, eH (−5000<θxH<5000, 0<θyH<10000, 0<sH≤eH<5000) separated by spaces, with the same meaning as for Coders.
A single observation is the segment of the ray from the observer whose distance from the observer is at least s and at most e. The observations made by Coders never touch or overlap each other. The observations made by High never touch or overlap each other.
If the positions of the stars in the sky can be recovered so that both records are satisfied, print on the first line the largest number of stars the two can recover. If they cannot be recovered, print -1.