Connected Gheeves
Time limit1sMemory limit128 MB
Given two convex funnel-shaped containers joined at the bottom, find the water level reached after pouring a given area of water, capped at the lower rim.
- Level
Hard8 of 10
- Topics
- Geometry, Binary search, Prefix sum, Math
- Solved
- No attempts yet
Problem
A gheef (plural gheeves) is a funnel-shaped two-dimensional container. A gheef is described by a sequence of points satisfying the following conditions:
- .
- Writing , there is an index with and . That is, the -coordinates strictly decrease down to the vertex and then strictly increase. This is called the cusp of the gheef.
- For every , , and for every , . The left wall lies to the left of the cusp and the right wall to its right.
- The walls are convex, bulging outward: at each vertex with or , rotating clockwise about until it is collinear with requires more than . Also, the segments joining consecutive points meet only at their shared endpoints, so the body does not self-intersect.
The sequence of segments is the body of the gheef. The figure below shows a gheef of six points with :

You are given two gheeves and . Every -coordinate of is a negative integer and every -coordinate of is a positive integer, so sits to the left of the -axis and to its right. The two cusps are joined by a thin pipe of negligible volume, so the gheeves behave as communicating vessels: the water always stands at the same level in both.
You pour a given amount of water into the system. Because the problem is two-dimensional, the amount of water is measured as the cross-sectional area it occupies. Water fills the gheeves from the bottom upward. If the level in reaches — the lower of its two rims — the water spills out of , and likewise for ; the level can therefore never rise above the lower of the two overflow rims. Determine the final water level once the given amount of water has been poured.
Input
The first line contains the integer , the number of test cases. Each test case is given on three lines:
- The first line contains one integer (), the amount of water poured into the system, measured as area.
- The second line describes gheef : an integer (the number of points) followed by coordinate pairs .
- The third line describes gheef in the same format.
All coordinates are integers. Every -coordinate of is negative and every -coordinate of is positive.
Output
For each test case, print one line containing the final water level , given as a -coordinate rounded to exactly three digits after the decimal point (for example, -15.000 or 3.536). If the poured amount is at least the system's capacity up to the lower overflow rim, the level equals that rim.