Aquarium Drainage
Time limit1sMemory limit128 MB
Given an orthogonal aquarium floor with holes on its segments, compute the total drain time and the water left behind.
- Level
Hard9 of 10
- Topics
- Geometry, Sorting, Union-find, Simulation
- Solved
- No attempts yet
Problem
Figure 1 below shows an aquarium seen from the front. The aquarium is completely full of water. If you drill one hole in the aquarium floor (a horizontal segment), water drains out through that hole.

Figure 1. The aquarium and a hole.
The axes are set as in the figure: the -axis increases from left to right, and the -axis increases from top to bottom. If a horizontal floor segment has a hole, then every bit of water that sits at or above that segment's -coordinate (that is, at a -coordinate no greater than it) and can flow down to the hole under gravity drains out through the hole. So in Figure 1 the aquarium empties completely through the floor hole.
The amount of water equals the area it occupies, measured in L (liters). Filling the aquarium in Figure 1 gives L of water, equal to its area.
A single hole lets out L of water per second. So in Figure 1 all the water drains in seconds.
The floor can be more complicated, as in Figure 2.
The aquarium floor is a chain of horizontal and vertical segments that alternate. Moreover, as in Figure 2, when you look straight down on the aquarium from directly above, every horizontal floor segment is visible.
There is at least one hole. Every hole lies on a horizontal segment, exactly at its midpoint, and each horizontal segment holds at most one hole.

Figure 2. The initial state. The water is L and there are holes.
In Figure 2 two horizontal segments each carry a hole, for holes in total (hole 1 and hole 2). The initial water is L. If both holes start draining at time , after how many seconds does the water stop draining?

Figure 3. A state partway through draining.
First, the L of water above the segment that carries hole 1 drains out through hole 1 and hole 2 at the same time. Since L leaves through the two holes, after seconds the state is as in Figure 3.
After that, L of water drains through hole 2 alone over seconds. In total the water drains for seconds and then stops. After seconds the remaining water is L, and the final state is Figure 4.

Figure 4. The final state.
Given the shape of a full aquarium's floor and the horizontal segments that carry holes, write a program that computes how long (in seconds) it takes for the water to finish draining and how much water remains.
Input
The first line contains (), the number of vertices that form the aquarium boundary. is even. The boundary always starts at vertex and ends at vertex , so the first and last vertices both have -coordinate . Every coordinate is an integer between and inclusive.
Starting at , the boundary edges begin with a vertical segment, then alternate between horizontal and vertical segments, and end with a vertical segment. Hence there is always exactly one more vertical segment than horizontal segments.
Each of the next lines gives the - and -coordinates of one boundary vertex, separated by a space, one per line, listed counterclockwise starting from the first vertex .
The next line contains the number of holes (). Each of the following lines gives the coordinates of the two endpoints of the horizontal segment that carries a hole, in the form a b c b. This means the hole lies on the segment joining vertices and , and always .
Output
Print two lines. On the first line, print the time in seconds for all the water to drain, rounded to two decimal places (rounding at the third decimal digit). On the second line, print the amount of remaining water as an integer that is or greater.
Hint
Intermediate values may exceed the range of a -bit integer, so use a -bit integer where needed. For real-number arithmetic, a double is recommended.