<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

345. Revolution
Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



During the last parliament elections in Berland two political parties collected the same amount of votes. Now the first party hired you to help them with their plan of a revolution.

During the revolution they want to split the country into two parts. The party is very democratic, so each member has its own separation plan.

The border of Berland can be considered as a convex polygon on the plane. The polygon can have three or more consecutive vertices lay on one line. Each separation plan is represented by the line of the cut.

Your program should calculate the area of the smallest part of the Berland for each separation plan.

Input
The first line of the input contains integer N (3 ≤ N ≤ 5 · 104). Next N lines contain coordinates xi, yi of the Berland border in the clockwise or counterclockwise order. The polygon is non-degenerate. The next line contains integer P (1 ≤ P ≤ 5 · 104). Next P lines contain coordinates x1,j, y1,j, x2,j, y2,j of two distinct points on the separation line. All coordinates are real and do not exceed 104 by the absolute value. They are given with no more than 4 significant digits after decimal point.

Output
Print P lines. Each line should contain the area of the smallest part after separating Berland with the corresponding line. Print value with at least 6 digits after decimal point. Absolute or relative error must be less than 10-5.

Example(s)
sample input
sample output
5
0 0
0 5
0 10
10 10
10 0
4
0 0 10 10
9 10 10 9
10 -1 12 11
10 10 0 5
50.000000
0.500000
0.000000
25.000000

<|response|>
## 1. Abridged problem statement

Given a convex polygon with `N ≤ 5 · 10^4` vertices and `P ≤ 5 · 10^4` query lines, for each line compute the area of the smaller part of the polygon after cutting it by that line.

If the line does not split the polygon into two non-zero-area parts, output `0`.

The polygon vertices are given in clockwise or counterclockwise order, and consecutive collinear vertices may exist.

---

## 2. Key observations needed to solve the problem

### Observation 1: Signed side of a point

For a query line passing through points `a` and `b`, define:

```text
d = b - a
side(p) = d × (p - a)
```

where `×` is the 2D cross product.

Then:

- `side(p) > 0`: point is on the left side of the directed line `a → b`
- `side(p) < 0`: point is on the right side
- `side(p) = 0`: point lies on the line

For a convex polygon:

- if all vertices have `side >= 0`, the line does not properly cut the polygon;
- if all vertices have `side <= 0`, the line does not properly cut the polygon;
- otherwise, the line intersects the polygon boundary in two points and splits it.

So each query needs:

1. Determine if both positive and negative vertices exist.
2. Find the two crossed edges.
3. Compute the area on one side.
4. Return the smaller of that area and `total_area - area`.

---

### Observation 2: We cannot scan all vertices per query

A direct `O(N)` check per query would cost:

```text
O(NP) = 2.5 · 10^9
```

which is too large.

We need about `O(log N)` per query.

---

### Observation 3: Convex polygon chains are useful

After making the polygon counterclockwise, find:

- the lexicographically leftmost vertex,
- the lexicographically rightmost vertex.

These two vertices split the convex polygon into two convex monotone chains.

For any fixed query line, the values:

```text
side(vertex)
```

along each chain are unimodal. Therefore, maximum and minimum side values on each chain can be found by binary search.

This gives the global maximum and minimum side values in `O(log N)`.

---

### Observation 4: Crossed edges can be found by binary search

Let:

- `i_min` be a vertex with minimum `side`,
- `i_max` be a vertex with maximum `side`.

If:

```text
side(i_max) <= 0
```

or

```text
side(i_min) >= 0
```

then the polygon is not properly split.

Otherwise:

- along the counterclockwise arc from `i_min` to `i_max`, side values go from negative to positive;
- along the counterclockwise arc from `i_max` to `i_min`, side values go from positive to negative.

So the two crossing edges can also be found by binary search.

---

### Observation 5: Area can be computed using shoelace prefix sums

Precompute prefix sums of shoelace terms:

```text
pref[i + 1] = pref[i] + polygon[i] × polygon[i + 1]
```

Then the shoelace contribution of any consecutive boundary chain can be obtained in `O(1)`.

Once the two intersection points are known, the area of one side of the cut can be computed directly using the shoelace formula.

---

## 3. Full solution approach

### Preprocessing

1. Read the polygon.
2. Compute its signed doubled area.
3. If the area is negative, reverse the vertex order so the polygon becomes counterclockwise.
4. Compute prefix shoelace sums.
5. Find the lexicographically leftmost and rightmost vertices.
6. Split the polygon into two chains:
   - `leftmost → rightmost`
   - `rightmost → leftmost`
7. Duplicate the vertex array to simplify cyclic indexing.

---

### Query processing

For each query line through points `a` and `b`:

1. Let:

   ```text
   d = b - a
   side(i) = d × (polygon[i] - a)
   ```

2. Find the maximum side value:
   - maximum on first chain,
   - maximum on second chain,
   - take the larger.

3. Find the minimum side value:
   - minimum on first chain,
   - minimum on second chain,
   - take the smaller.

4. If there is no positive side or no negative side, output `0`.

5. Otherwise, binary search for:
   - first positive vertex on the arc from `i_min` to `i_max`,
   - last positive vertex on the arc from `i_max` to `i_min`.

6. These determine the two polygon edges crossed by the query line.

7. Compute the intersection points of those edges with the query line.

8. Compute the area of the positive-side polygon piece using the shoelace formula and prefix sums.

9. Output:

   ```text
   min(piece_area, total_area - piece_area)
   ```

---

### Complexity

Preprocessing:

```text
O(N)
```

Each query:

```text
O(log N)
```

Total:

```text
O(N + P log N)
```

Memory:

```text
O(N)
```

---

## 4. C++ implementation with detailed comments

```cpp
#include <bits/stdc++.h>
// #include <coding_library/geometry/point.hpp>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

using coord_t = double;

struct Point {
    static constexpr coord_t eps = 1e-9;
    static inline const coord_t PI = acos((coord_t)-1.0);

    coord_t x, y;
    Point(coord_t x = 0, coord_t y = 0) : x(x), y(y) {}

    Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); }
    Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); }
    Point operator*(coord_t c) const { return Point(x * c, y * c); }
    Point operator/(coord_t c) const { return Point(x / c, y / c); }

    coord_t operator*(const Point& p) const { return x * p.x + y * p.y; }
    coord_t operator^(const Point& p) const { return x * p.y - y * p.x; }

    bool operator==(const Point& p) const { return x == p.x && y == p.y; }
    bool operator!=(const Point& p) const { return x != p.x || y != p.y; }
    bool operator<(const Point& p) const {
        return x != p.x ? x < p.x : y < p.y;
    }
    bool operator>(const Point& p) const {
        return x != p.x ? x > p.x : y > p.y;
    }
    bool operator<=(const Point& p) const {
        return x != p.x ? x < p.x : y <= p.y;
    }
    bool operator>=(const Point& p) const {
        return x != p.x ? x > p.x : y >= p.y;
    }

    coord_t norm2() const { return x * x + y * y; }
    coord_t norm() const { return sqrt(norm2()); }
    coord_t angle() const { return atan2(y, x); }

    Point rotate(coord_t a) const {
        return Point(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a));
    }

    Point perp() const { return Point(-y, x); }
    Point unit() const { return *this / norm(); }
    Point normal() const { return perp().unit(); }
    Point project(const Point& p) const {
        return *this * (*this * p) / norm2();
    }
    Point reflect(const Point& p) const {
        return *this * 2 * (*this * p) / norm2() - p;
    }

    friend ostream& operator<<(ostream& os, const Point& p) {
        return os << p.x << ' ' << p.y;
    }
    friend istream& operator>>(istream& is, Point& p) {
        return is >> p.x >> p.y;
    }

    friend int ccw(const Point& a, const Point& b, const Point& c) {
        coord_t v = (b - a) ^ (c - a);
        if(-eps <= v && v <= eps) {
            return 0;
        } else if(v > 0) {
            return 1;
        } else {
            return -1;
        }
    }

    friend bool point_on_segment(
        const Point& a, const Point& b, const Point& p
    ) {
        return ccw(a, b, p) == 0 && p.x >= min(a.x, b.x) - eps &&
               p.x <= max(a.x, b.x) + eps && p.y >= min(a.y, b.y) - eps &&
               p.y <= max(a.y, b.y) + eps;
    }

    friend bool point_in_triangle(
        const Point& a, const Point& b, const Point& c, const Point& p
    ) {
        int d1 = ccw(a, b, p);
        int d2 = ccw(b, c, p);
        int d3 = ccw(c, a, p);
        return (d1 >= 0 && d2 >= 0 && d3 >= 0) ||
               (d1 <= 0 && d2 <= 0 && d3 <= 0);
    }

    friend Point line_line_intersection(
        const Point& a1, const Point& b1, const Point& a2, const Point& b2
    ) {
        return a1 +
               (b1 - a1) * ((a2 - a1) ^ (b2 - a2)) / ((b1 - a1) ^ (b2 - a2));
    }

    friend bool collinear(const Point& a, const Point& b) {
        return abs(a ^ b) < eps;
    }

    friend Point circumcenter(const Point& a, const Point& b, const Point& c) {
        Point mid_ab = (a + b) / 2.0;
        Point mid_ac = (a + c) / 2.0;
        Point perp_ab = (b - a).perp();
        Point perp_ac = (c - a).perp();
        return line_line_intersection(
            mid_ab, mid_ab + perp_ab, mid_ac, mid_ac + perp_ac
        );
    }

    friend coord_t arc_area(
        const Point& center, coord_t r, const Point& p1, const Point& p2
    ) {
        coord_t theta1 = (p1 - center).angle();
        coord_t theta2 = (p2 - center).angle();
        if(theta2 < theta1 - eps) {
            theta2 += 2 * PI;
        }

        coord_t d_theta = theta2 - theta1;
        coord_t cx = center.x, cy = center.y;
        coord_t area = r * cx * (sin(theta2) - sin(theta1)) -
                       r * cy * (cos(theta2) - cos(theta1)) + r * r * d_theta;
        return area / 2.0;
    }

    friend vector<Point> intersect_circles(
        const Point& c1, coord_t r1, const Point& c2, coord_t r2
    ) {
        Point d = c2 - c1;
        coord_t dist = d.norm();

        if(dist > r1 + r2 + eps || dist < abs(r1 - r2) - eps || dist < eps) {
            return {};
        }

        coord_t a = (r1 * r1 - r2 * r2 + dist * dist) / (2 * dist);
        coord_t h_sq = r1 * r1 - a * a;
        if(h_sq < -eps) {
            return {};
        }
        if(h_sq < 0) {
            h_sq = 0;
        }
        coord_t h = sqrt(h_sq);

        Point mid = c1 + d.unit() * a;
        Point perp_dir = d.perp().unit();

        if(h < eps) {
            return {mid};
        }
        return {mid + perp_dir * h, mid - perp_dir * h};
    }

    friend optional<Point> intersect_ray_segment(
        const Point& ray_start, const Point& ray_through, const Point& seg_a,
        const Point& seg_b
    ) {
        Point ray_dir = ray_through - ray_start;
        if(ray_dir.norm2() < Point::eps) {
            return {};
        }
        Point seg_dir = seg_b - seg_a;
        coord_t denom = ray_dir ^ seg_dir;
        if(fabs(denom) < eps) {
            return {};
        }
        coord_t t = ((seg_a - ray_start) ^ seg_dir) / denom;
        if(t < eps) {
            return {};
        }
        coord_t s = ((seg_a - ray_start) ^ ray_dir) / denom;
        if(s < eps || s > 1 - eps) {
            return {};
        }
        return ray_start + ray_dir * t;
    }
};

template<typename F>
int first_true(int lo, int hi, F pred) {
    while(lo < hi) {
        int mid = lo + (hi - lo) / 2;
        if(pred(mid)) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    return lo;
}

char in_buf[1 << 16];
int in_pos = 0, in_len = 0;

int next_char() {
    if(in_pos == in_len) {
        in_len = (int)fread(in_buf, 1, sizeof(in_buf), stdin);
        in_pos = 0;
        if(in_len == 0) {
            return -1;
        }
    }
    return in_buf[in_pos++];
}

int read_int() {
    int c = next_char();
    while(c <= ' ') {
        c = next_char();
    }
    bool neg = c == '-';
    if(neg) {
        c = next_char();
    }
    int x = 0;
    while(c > ' ') {
        x = x * 10 + (c - '0');
        c = next_char();
    }
    return neg ? -x : x;
}

coord_t read_real() {
    int c = next_char();
    while(c <= ' ') {
        c = next_char();
    }
    int sign = 1;
    if(c == '-') {
        sign = -1;
        c = next_char();
    } else if(c == '+') {
        c = next_char();
    }

    int64_t ival = 0;
    while(c >= '0' && c <= '9') {
        ival = ival * 10 + (c - '0');
        c = next_char();
    }

    coord_t val = (coord_t)ival;
    if(c == '.') {
        c = next_char();
        int64_t fval = 0;
        int fdigits = 0;
        while(c >= '0' && c <= '9' && fdigits < 18) {
            fval = fval * 10 + (c - '0');
            fdigits++;
            c = next_char();
        }

        while(c >= '0' && c <= '9') {
            c = next_char();
        }

        if(fdigits > 0) {
            static const coord_t ipow10[19] = {1.0,   1e-1,  1e-2,  1e-3,
                                               1e-4,  1e-5,  1e-6,  1e-7,
                                               1e-8,  1e-9,  1e-10, 1e-11,
                                               1e-12, 1e-13, 1e-14, 1e-15,
                                               1e-16, 1e-17, 1e-18};
            val += (coord_t)fval * ipow10[fdigits];
        }
    }
    return sign * val;
}

int n, q;
vector<Point> pts;
vector<coord_t> pref;
coord_t total_area;
int l_idx, r_idx;
int lower_len, upper_len;

void read() {
    n = read_int();
    pts.resize(n);
    for(int i = 0; i < n; i++) {
        pts[i].x = read_real();
        pts[i].y = read_real();
    }

    coord_t s = 0;
    for(int i = 0; i < n; i++) {
        s += pts[i] ^ pts[(i + 1) % n];
    }

    if(s < 0) {
        reverse(pts.begin(), pts.end());
        s = -s;
    }

    pref.assign(n + 1, 0);
    for(int i = 0; i < n; i++) {
        pref[i + 1] = pref[i] + (pts[i] ^ pts[(i + 1) % n]);
    }

    l_idx = r_idx = 0;
    for(int i = 1; i < n; i++) {
        if(pts[i] < pts[l_idx]) {
            l_idx = i;
        }
        if(pts[r_idx] < pts[i]) {
            r_idx = i;
        }
    }

    lower_len = ((r_idx - l_idx) % n + n) % n + 1;
    upper_len = ((l_idx - r_idx) % n + n) % n + 1;

    pts.resize(2 * n);
    for(int i = 0; i < n; i++) {
        pts[n + i] = pts[i];
    }

    total_area = s / 2;
    q = read_int();
}

void solve() {
    // After ensuring CCW orientation we precompute prefix shoelace sums
    // and split the polygon into two x-monotone chains by the leftmost
    // and rightmost vertices l_idx and r_idx:
    //
    //   lower chain = pts[l_idx], pts[l_idx + 1], ..., pts[r_idx]
    //   upper chain = pts[r_idx], pts[r_idx + 1], ..., pts[l_idx]
    //
    // Both chains share l_idx and r_idx. They are individually convex,
    // so for any cut line with direction d the side function
    // side(i) = d ^ (pts[i] - a) is unimodal on each chain: convex (one
    // interior minimum, max at endpoint) when d.x has the appropriate
    // sign for that chain, concave (one interior maximum, min at
    // endpoint) otherwise. This means a single ternary search per chain
    // suffices to find both extrema, and the polygon-wide peak i_max
    // and trough i_min are just the max/min across the two chains.
    //
    // Once i_max and i_min are known, side is genuinely monotone on each
    // arc between them: ascending on the arc from i_min to i_max (CCW),
    // descending on the arc from i_max to i_min (CCW). A textbook binary
    // search on each arc locates the zero crossing, giving the two edges
    // the cut intersects. The line_line_intersection of d with each of
    // those edges yields x and y, and the positive piece's doubled area
    // is the shoelace sum (x ^ pts[ccw_first]) + chain_sum + (pts[ccw_last]
    // ^ y) + (y ^ x), where chain_sum reduces to a prefix-sum slice.
    // The answer is min(piece, total - piece).

    cout << fixed << setprecision(6);

    for(int qi = 0; qi < q; qi++) {
        Point a, b;
        a.x = read_real();
        a.y = read_real();
        b.x = read_real();
        b.y = read_real();
        Point d = b - a;

        auto side = [&](int i) -> coord_t { return d ^ (pts[i] - a); };

        auto extreme_on_chain = [&](int chain_start, int chain_len,
                                    bool want_max) -> int {
            int lo = 0, hi = chain_len - 1;
            while(lo < hi) {
                int mid = lo + (hi - lo) / 2;
                coord_t sm = side(chain_start + mid);
                coord_t sn = side(chain_start + mid + 1);
                if(want_max ? sm < sn : sm > sn) {
                    lo = mid + 1;
                } else {
                    hi = mid;
                }
            }
            int best = lo;
            coord_t best_v = side(chain_start + lo);
            coord_t v0 = side(chain_start);
            if(want_max ? v0 > best_v : v0 < best_v) {
                best = 0;
                best_v = v0;
            }
            coord_t vL = side(chain_start + chain_len - 1);
            if(want_max ? vL > best_v : vL < best_v) {
                best = chain_len - 1;
            }
            return best;
        };

        int max_lower_k = extreme_on_chain(l_idx, lower_len, true);
        int max_upper_k = extreme_on_chain(r_idx, upper_len, true);
        coord_t max_lower_v = side(l_idx + max_lower_k);
        coord_t max_upper_v = side(r_idx + max_upper_k);
        int i_max = max_lower_v >= max_upper_v ? (l_idx + max_lower_k) % n
                                               : (r_idx + max_upper_k) % n;

        if(side(i_max) <= Point::eps) {
            cout << (coord_t)0 << '\n';
            continue;
        }

        int min_lower_k = extreme_on_chain(l_idx, lower_len, false);
        int min_upper_k = extreme_on_chain(r_idx, upper_len, false);
        coord_t min_lower_v = side(l_idx + min_lower_k);
        coord_t min_upper_v = side(r_idx + min_upper_k);
        int i_min = min_lower_v <= min_upper_v ? (l_idx + min_lower_k) % n
                                               : (r_idx + min_upper_k) % n;

        if(side(i_min) >= -Point::eps) {
            cout << (coord_t)0 << '\n';
            continue;
        }

        int len_up = ((i_max - i_min) % n + n) % n;
        int len_dn = ((i_min - i_max) % n + n) % n;

        int ccw_first_k = first_true(1, len_up + 1, [&](int k) {
            return side(i_min + k) > 0;
        });
        int ccw_last_k =
            first_true(1, len_dn, [&](int k) { return side(i_max + k) <= 0; }) -
            1;

        auto wrap = [&](int i) { return i < n ? i : i - n; };
        int ccw_first = wrap(i_min + ccw_first_k);
        int ccw_last = wrap(i_max + ccw_last_k);
        int prev_f = ccw_first == 0 ? n - 1 : ccw_first - 1;
        int next_l = ccw_last + 1 == n ? 0 : ccw_last + 1;

        Point x = line_line_intersection(pts[prev_f], pts[ccw_first], a, b);
        Point y = line_line_intersection(pts[ccw_last], pts[next_l], a, b);

        coord_t chain = ccw_first <= ccw_last
                            ? pref[ccw_last] - pref[ccw_first]
                            : pref[n] - pref[ccw_first] + pref[ccw_last];
        coord_t doubled =
            (x ^ pts[ccw_first]) + chain + (pts[ccw_last] ^ y) + (y ^ x);
        coord_t piece = fabs(doubled) / 2;
        cout << max(min(piece, total_area - piece), (coord_t)0) << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}
```

---

## 5. Python implementation with detailed comments

```python
import sys

EPS = 1e-9


def cross(a, b):
    """
    2D cross product.
    """
    return a[0] * b[1] - a[1] * b[0]


def sub(a, b):
    """
    Vector subtraction a - b.
    """
    return (a[0] - b[0], a[1] - b[1])


def first_true(lo, hi, pred):
    """
    Binary search for the first integer x in [lo, hi)
    such that pred(x) is True.

    Assumes the predicate is monotone:
    False, False, ..., True, True.
    """
    while lo < hi:
        mid = (lo + hi) // 2

        if pred(mid):
            hi = mid
        else:
            lo = mid + 1

    return lo


def line_intersection(p1, p2, q1, q2):
    """
    Intersection of two infinite lines:
    line p1 -> p2 and line q1 -> q2.
    """
    d1 = sub(p2, p1)
    d2 = sub(q2, q1)

    t = cross(sub(q1, p1), d2) / cross(d1, d2)

    return (
        p1[0] + d1[0] * t,
        p1[1] + d1[1] * t,
    )


def solve():
    data = sys.stdin.buffer.read().split()
    ptr = 0

    n = int(data[ptr])
    ptr += 1

    pts = []

    for _ in range(n):
        x = float(data[ptr])
        y = float(data[ptr + 1])
        ptr += 2
        pts.append((x, y))

    # Compute doubled signed polygon area.
    area2 = 0.0

    for i in range(n):
        area2 += cross(pts[i], pts[(i + 1) % n])

    # Make polygon counterclockwise.
    if area2 < 0:
        pts.reverse()
        area2 = -area2

    total_area = area2 / 2.0

    # Prefix sums of shoelace edge contributions.
    pref = [0.0] * (n + 1)

    for i in range(n):
        pref[i + 1] = pref[i] + cross(pts[i], pts[(i + 1) % n])

    # Find lexicographically leftmost and rightmost vertices.
    left_idx = 0
    right_idx = 0

    for i in range(1, n):
        if pts[i] < pts[left_idx]:
            left_idx = i

        if pts[right_idx] < pts[i]:
            right_idx = i

    # Chain lengths.
    lower_len = (right_idx - left_idx) % n + 1
    upper_len = (left_idx - right_idx) % n + 1

    # Duplicate array for easier cyclic indexing.
    pts2 = pts + pts

    q = int(data[ptr])
    ptr += 1

    out = []

    for _ in range(q):
        ax = float(data[ptr])
        ay = float(data[ptr + 1])
        bx = float(data[ptr + 2])
        by = float(data[ptr + 3])
        ptr += 4

        a = (ax, ay)
        b = (bx, by)
        d = (bx - ax, by - ay)

        def side(i):
            """
            Signed side of vertex i relative to directed query line a -> b.
            """
            p = pts2[i]
            return cross(d, (p[0] - ax, p[1] - ay))

        def extreme_on_chain(start, length, want_max):
            """
            Finds maximum or minimum side value on one convex chain.

            Because side values on a convex chain are unimodal,
            binary search on adjacent values is sufficient.
            """
            lo = 0
            hi = length - 1

            while lo < hi:
                mid = (lo + hi) // 2

                cur = side(start + mid)
                nxt = side(start + mid + 1)

                if want_max:
                    if cur < nxt:
                        lo = mid + 1
                    else:
                        hi = mid
                else:
                    if cur > nxt:
                        lo = mid + 1
                    else:
                        hi = mid

            best = lo
            best_value = side(start + best)

            # The required extreme may be at an endpoint.
            first_value = side(start)

            if want_max:
                if first_value > best_value:
                    best = 0
                    best_value = first_value
            else:
                if first_value < best_value:
                    best = 0
                    best_value = first_value

            last_value = side(start + length - 1)

            if want_max:
                if last_value > best_value:
                    best = length - 1
            else:
                if last_value < best_value:
                    best = length - 1

            return best

        # Global maximum side vertex.
        max_lower = extreme_on_chain(left_idx, lower_len, True)
        max_upper = extreme_on_chain(right_idx, upper_len, True)

        max_lower_value = side(left_idx + max_lower)
        max_upper_value = side(right_idx + max_upper)

        if max_lower_value >= max_upper_value:
            i_max = (left_idx + max_lower) % n
        else:
            i_max = (right_idx + max_upper) % n

        # No positive vertex: no proper split.
        if side(i_max) <= EPS:
            out.append("0.000000")
            continue

        # Global minimum side vertex.
        min_lower = extreme_on_chain(left_idx, lower_len, False)
        min_upper = extreme_on_chain(right_idx, upper_len, False)

        min_lower_value = side(left_idx + min_lower)
        min_upper_value = side(right_idx + min_upper)

        if min_lower_value <= min_upper_value:
            i_min = (left_idx + min_lower) % n
        else:
            i_min = (right_idx + min_upper) % n

        # No negative vertex: no proper split.
        if side(i_min) >= -EPS:
            out.append("0.000000")
            continue

        # Counterclockwise arc lengths.
        len_up = (i_max - i_min) % n
        len_down = (i_min - i_max) % n

        # Arc i_min -> i_max:
        # find first positive vertex.
        first_positive_offset = first_true(
            1,
            len_up + 1,
            lambda k: side(i_min + k) > 0
        )

        # Arc i_max -> i_min:
        # find where positive vertices end.
        last_positive_offset = first_true(
            1,
            len_down,
            lambda k: side(i_max + k) <= 0
        ) - 1

        def wrap(idx):
            return idx if idx < n else idx - n

        first_positive = wrap(i_min + first_positive_offset)
        last_positive = wrap(i_max + last_positive_offset)

        before_first = n - 1 if first_positive == 0 else first_positive - 1
        after_last = 0 if last_positive + 1 == n else last_positive + 1

        # Boundary intersections with the cutting line.
        x = line_intersection(
            pts[before_first],
            pts[first_positive],
            a,
            b
        )

        y = line_intersection(
            pts[last_positive],
            pts[after_last],
            a,
            b
        )

        # Shoelace contribution of polygon chain:
        # first_positive -> ... -> last_positive.
        if first_positive <= last_positive:
            chain_sum = pref[last_positive] - pref[first_positive]
        else:
            chain_sum = pref[n] - pref[first_positive] + pref[last_positive]

        # Doubled area of the positive-side piece:
        #
        # x -> first_positive -> ... -> last_positive -> y -> x
        piece_area2 = (
            cross(x, pts[first_positive])
            + chain_sum
            + cross(pts[last_positive], y)
            + cross(y, x)
        )

        piece_area = abs(piece_area2) / 2.0

        answer = min(piece_area, total_area - piece_area)

        # Clamp tiny floating-point errors.
        if answer < 0 and answer > -1e-7:
            answer = 0.0

        out.append(f"{answer:.6f}")

    sys.stdout.write("\n".join(out))


if __name__ == "__main__":
    solve()
```
