This page is still under construction.

Parts of this page are still being built. What you see may change.

Connected Gheeves

Time limit1sMemory limit128 MB

Summary
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 (p1,p2,…,pn)(p_1, p_2, \dots, p_n) satisfying the following conditions:

  • 3≤n≤10003 \le n \le 1000.
  • Writing pi=(xi,yi)p_i = (x_i, y_i), there is an index 1<c<n1 < c < n with y1>y2>⋯>ycy_1 > y_2 > \dots > y_c and yc<yc+1<⋯<yny_c < y_{c+1} < \dots < y_n. That is, the yy-coordinates strictly decrease down to the vertex pcp_c and then strictly increase. This pcp_c is called the cusp of the gheef.
  • For every 1≤i<c1 \le i < c, xi<xcx_i < x_c, and for every c<i≤nc < i \le n, xi>xcx_i > x_c. 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 pip_i with 1<i<c1 < i < c or c<i<nc < i < n, rotating pi−1p_{i-1} clockwise about pip_i until it is collinear with pipi+1p_i p_{i+1} requires more than 180°180°. Also, the segments joining consecutive points meet only at their shared endpoints, so the body does not self-intersect.

The sequence of segments (p1p2,p2p3,…,pn−1pn)(p_1p_2, p_2p_3, \dots, p_{n-1}p_n) is the body of the gheef. The figure below shows a gheef of six points with c=4c = 4:

You are given two gheeves P=(p1,…,pn)P = (p_1, \dots, p_n) and Q=(q1,…,qm)Q = (q_1, \dots, q_m). Every xx-coordinate of PP is a negative integer and every xx-coordinate of QQ is a positive integer, so PP sits to the left of the yy-axis and QQ 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 PP reaches min⁡(y1,yn)\min(y_1, y_n) — the lower of its two rims — the water spills out of PP, and likewise for QQ; 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 tt, the number of test cases. Each test case is given on three lines:

  • The first line contains one integer aa (1≤a≤1000001 \le a \le 100000), the amount of water poured into the system, measured as area.
  • The second line describes gheef PP: an integer kk (the number of points) followed by kk coordinate pairs x1 y1 x2 y2 … xk ykx_1\ y_1\ x_2\ y_2\ \dots\ x_k\ y_k.
  • The third line describes gheef QQ in the same format.

All coordinates are integers. Every xx-coordinate of PP is negative and every xx-coordinate of QQ is positive.

Output

For each test case, print one line containing the final water level LL, given as a yy-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.

Examples2

  1. Example 1

    Input
    2
    25
    3 -30 10 -20 0 -10 10
    3 10 10 20 0 30 10
    25
    3 -30 -10 -20 -20 -10 -10
    3 10 10 20 0 30 10
    
    Expected output
    3.536
    -15.000
    
  2. Example 2

    Input
    1
    50
    3 -30 10 -20 0 -10 10
    3 10 10 20 0 30 10
    
    Expected output
    5.000