Under construction

Still being built. Things may change

Aquarium Drainage

Time limit1sMemory limit128 MB

Summary
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 xx-axis increases from left to right, and the yy-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 yy-coordinate (that is, at a yy-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 4040L of water, equal to its area.

A single hole lets out 11L of water per second. So in Figure 1 all the water drains in 4040 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 2626L and there are 22 holes.

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

Figure 3. A state partway through draining.

First, the 1616L of water above the segment that carries hole 1 drains out through hole 1 and hole 2 at the same time. Since 1616L leaves through the two holes, after 88 seconds the state is as in Figure 3.

After that, 33L of water drains through hole 2 alone over 33 seconds. In total the water drains for 8+3=118 + 3 = 11 seconds and then stops. After 1111 seconds the remaining water is 77L, 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 NN (4≤N≤300,0004 \le N \le 300{,}000), the number of vertices that form the aquarium boundary. NN is even. The boundary always starts at vertex (0,0)(0, 0) and ends at vertex (A,0)(A, 0), so the first and last vertices both have yy-coordinate 00. Every coordinate is an integer between 00 and 500,000500{,}000 inclusive.

Starting at (0,0)(0, 0), 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 NN lines gives the xx- and yy-coordinates of one boundary vertex, separated by a space, one per line, listed counterclockwise starting from the first vertex (0,0)(0, 0).

The next line contains the number of holes KK (1≤K≤N/21 \le K \le N/2). Each of the following KK 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 (a,b)(a, b) and (c,b)(c, b), and always a<ca < c.

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 00 or greater.

Hint

Intermediate values may exceed the range of a 3232-bit integer, so use a 6464-bit integer where needed. For real-number arithmetic, a double is recommended.

Examples2

  1. Example 1

    Input
    4
    0 0
    0 5
    8 5
    8 0
    1
    0 5 8 5
    
    Expected output
    40.00
    0
    
  2. Example 2

    Input
    14
    0 0
    0 5
    1 5
    1 3
    2 3
    2 4
    3 4
    3 2
    5 2
    5 4
    6 4
    6 3
    8 3
    8 0
    2
    1 3 2 3
    3 2 5 2
    
    Expected output
    11.00
    7