跳到正文

剪贴板

剪贴板 z7xavv07

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
template <typename T> inline void checkMax(T &x, T y) { x = x > y ? x : y; }
template <typename T> inline void checkMin(T &x, T y) { x = x < y ? x : y; }
typedef long long ll;
typedef unsigned long long ull;

std::mt19937 rnd((unsigned)time(NULL));
const double eps = 1e-6, pi = acos(-1.0), inf = 1e100;
inline double sign(double x) { return x > eps ? 1 : (x < -eps ? -1 : 0); }

struct poly {
    double a, b, c;
    poly() { a = b = c = 0; }
    poly(double a, double b, double c) :
        a(a), b(b), c(c) {}
    friend poly operator+(const poly &f, const poly &g) {
        return poly(f.a + g.a, f.b + g.b, f.c + g.c);
    }
    friend poly operator-(const poly &f, const poly &g) {
        return poly(f.a - g.a, f.b - g.b, f.c - g.c);
    }
    friend poly operator*(const poly &f, const poly &g) {
        return poly(f.a * g.c + f.b * g.b + f.c * g.a, f.b * g.c + f.c * g.b, f.c * g.c);
    }
    double getValue(double x) const {
        return a * x * x + b * x + c;
    }
    friend bool operator==(const poly &x, const poly &y) {
        return x.a == y.a && x.b == y.b && x.c == y.c;
    }
    double getMinValue(double L, double R) {
        double res = std::min(getValue(L), getValue(R));
        if (sign(a) == 0) {
            return res;
        }
        double mid = -b / (2 * a);
        if (mid >= L && mid <= R) {
            return std::min(res, getValue(mid));
        } else {
            return res;
        }
    }
    auto getRoot() const {
        if (sign(a) == 0) {
            if (sign(b) == 0 && sign(c) == 0) return std::vector<double> {};
            return std::vector<double> {-c / b};
        }
        std::vector<double> res;
        double dlt = b * b - 4 * a * c;
        if (dlt < 0) return res;
        dlt = sqrt(dlt);
        double x1 = (-b - dlt) / (2 * a), x2 = (-b + dlt) / (2 * a);
        res.emplace_back(x1);
        res.emplace_back(x2);
        return res;
    }
    void write() {
        std::cerr << std::fixed << "(" << a << ")x^2 + (" << b << ")x + (" << c << ")\n";
    }
};

namespace Geometry {
struct Vector {
    double x, y;
    Vector() { x = y = 0; }
    Vector(const double &x, const double &y) : x(x), y(y) {}
    Vector(const Vector &t) : x(t.x), y(t.y) {}
    friend Vector operator-(const Vector &x, const Vector &y) { return Vector(x.x - y.x, x.y - y.y); }
    Vector &operator-=(const Vector &t) { x -= t.x, y -= t.y; return *this; }
    friend Vector operator+(const Vector &x, const Vector &y) { return Vector(x.x + y.x, x.y + y.y); }
    Vector &operator+=(const Vector &t) { x += t.x; y += t.y; return *this; }
    friend Vector operator/(const Vector &x, const double &y) { return Vector(x.x / y, x.y / y); }
    Vector &operator/=(const double t) { x /= t; y /= t; return *this; }
    friend Vector operator*(const Vector &x, const double &y) { return Vector(x.x * y, x.y * y); }
    friend Vector operator*(const double &y, const Vector &x) { return Vector(x.x * y, x.y * y); }
    Vector &operator*=(const double t) { x *= t; y *= t; return *this; }
    double mod() const { return hypot(x, y); }
    double mod2() const { return x * x + y * y; }
    friend double operator*(const Vector &a, const Vector &b) { return a.x * b.x + a.y * b.y; } //Dot Product
    friend double operator^(const Vector &a, const Vector &b) { return a.x * b.y - a.y * b.x; } //cross Product
    friend bool operator==(const Vector &a, const Vector &b) { return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0; }
    friend bool operator!=(const Vector &a, const Vector &b) { return sign(a.x - b.x) != 0 || sign(a.y - b.y) != 0; }
    friend bool operator<(const Vector &a, const Vector &b) { return sign(a.x - b.x) ? a.x < b.x : sign(a.y - b.y) < 0; }
    friend bool operator>(const Vector &a, const Vector &b) { return sign(a.x - b.x) ? a.x > b.x : sign(a.y - b.y) > 0; }
    friend bool operator||(const Vector &a, const Vector &b) { return sign(a ^ b) == 0; }
    Vector rotate(const double &theta) const {
        double c = cos(theta), s = sin(theta);
        return Vector(x * c + y * s, -x * s + y * c);
    }
    void read() { std::cin >> x >> y; }
    Vector normal() const { return Vector(-y, x); }
    double slope() const { return sign(y) ? (sign(x) ? y / x : (y > 0 ? inf : -inf)) : 0; }
    double angle() const { return atan2(y, x); }
};
typedef Vector Point;
double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }
double angle(Vector a, Vector b) {
    return acos((a * b) / a.mod() / b.mod());
}
double dis(Point x, Point y) { return (x - y).mod(); }

}

using namespace Geometry;

const int N = 105;
double h, g;
int n, prec[N], succ[N];
double ans = inf;

double cur = 0;
struct dPoint {
    poly x, y;
    dPoint() {}
    dPoint(const poly &x, const poly &y) : x(x), y(y) {}
    Point getPoint() const {
        return Point(x.getValue(cur), y.getValue(cur));
    }
    friend bool operator<(const dPoint &x, const dPoint &y) {
        return x.getPoint() < y.getPoint();
    }
    friend bool operator==(const dPoint &x, const dPoint &y) {
        return x.getPoint() == y.getPoint();
    }
    friend dPoint operator-(const dPoint &x, const dPoint &y) {
        return dPoint(x.x - y.x, x.y - y.y);
    }
    friend poly operator^(const dPoint &a, const dPoint &b) { return a.x * b.y - a.y * b.x; } //cross Product
} P[N];


struct Import {
    double t;
    int i, j, k;
    Import() {}
    Import(double t, int i, int j, int k) :
        t(t), i(i), j(j), k(k) {}
};
std::vector<Import> imp;


//DynamicConvexHull
std::set<dPoint> U, D;
poly area_X;
int in(const std::set<dPoint> &s, const dPoint &p, int op) {
    auto it = s.lower_bound(p);
    if (it == s.end()) return 0;
    if (p == *it) return 2;
    if (it == s.begin()) return 0;
    auto pv = prev(it);
    double d = cross((p - *pv).getPoint(), (*it - *pv).getPoint()) * op;
    return sign(d) == 0 ? 2 : (sign(d) > 0 ? 1 : 0);
}
int in(const dPoint &p) {
    int x = in(U, p, 1), y = in(D, p, -1);
    if (x == 2 || y == 2) return 2;
    return x == 1 && y == 1 ? 1 : 0;
}

void insert(std::set<dPoint> &s, const dPoint &p, int op) {
    if (in(s, p, op)) return;
    auto it = s.lower_bound(p);
    std::set<dPoint>::iterator pv, nx;
    if (it != s.end()) {
        nx = next(it);
        while (nx != s.end() && sign(cross((*it - p).getPoint(), (*nx - *it).getPoint())) * op >= 0) {
            s.erase(it++);
            nx = next(it);
        }
    }
    if (it != s.begin()) {
        --it;
        while (it != s.begin()) {
            pv = prev(it);
            if (sign(cross((*it - *pv).getPoint(), (p - *it).getPoint())) * op < 0) break;
            s.erase(it--);
        }
    }
    it = s.insert(p).first;
}
void insert(const dPoint &p) {
    insert(U, p, 1);
    insert(D, p, -1);
}

int getID(const dPoint &p) {
    for (int i = 1; i <= n; ++i) {
        if (P[i].x == p.x && P[i].y == p.y) {
            return i;
        }
    }
    return -1;
}

int main() {
#ifdef LOCAL_JUDGE
    freopen("0.in", "r", stdin);
#endif
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> h >> g;
    std::cin >> n;

    for (int i = 1; i <= n; ++i) {
        Point A, B;
        A.read(), B.read();
        P[i].x = poly(0, B.x - A.x, A.x);
        P[i].y = poly(0, B.y - A.y, A.y);
    }

    if (n < 2) {
        std::cout << "0\n";
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = i + 1; j <= n; ++j) {
            for (int k = j + 1; k <= n; ++k) {
                auto vec = ((P[j] - P[i]) ^ (P[k] - P[i])).getRoot();
                for (auto t : vec) {
                    if (t >= 0 && t <= 1) {
                        imp.emplace_back(t, i, j, k);
                    }
                }
            }
        }
    }
    imp.emplace_back(0, 0, 0, 0);
    std::sort(imp.begin(), imp.end(), [&](const Import &A, const Import &B) {
        return A.t < B.t;
    });

    int ct = imp.size();
    imp.emplace_back(1, 0, 0, 0);

    cur = (imp[0].t + imp[1].t) / 2.0;
    for (int i = 1; i <= n; ++i) {
        insert(P[i]);
    }

    std::vector<int> C;
    for (auto it = U.begin(); next(it) != U.end(); ++it) {
        C.emplace_back(getID(*it));
    }
    for (auto it = prev(D.end()); it != D.begin(); --it) {
        C.emplace_back(getID(*it));
    }
    area_X = poly();
    C.emplace_back(C[0]);
    for (int i = 0; i < int(C.size()) - 1; ++i) {
        succ[C[i]] = C[i + 1];
        prec[C[i + 1]] = C[i];
        area_X = area_X + (P[C[i + 1]] ^ P[C[i]]);
    }

    for (int i = 0; i < ct; ++i) {
        cur = (imp[i].t + imp[i + 1].t) / 2.0;
        if (i) {
            int x = imp[i].i, y = imp[i].j, z = imp[i].k;
            if (prec[x] == 0) {
                std::swap(x, y);
            }
            if (prec[z] == 0) {
                std::swap(y, z);
            }
            if (prec[x] && prec[z]) {
                if (prec[y]) {
                    if ((prec[x] == y && succ[x] == z) || (prec[x] == z && succ[x] == y)) {
                        std::swap(x, y);
                    } else if ((prec[z] == y && succ[z] == x) || (prec[z] == x && succ[z] == y)) {
                        std::swap(y, z);
                    }
                    if ((prec[y] == x && succ[y] == z) || (prec[y] == z && succ[y] == x)) {
                        x = prec[y], z = succ[y];
                        area_X = area_X - (P[y] ^ P[x]);
                        area_X = area_X - (P[z] ^ P[y]);
                        area_X = area_X + (P[z] ^ P[x]);
                        prec[y] = succ[y] = 0;
                        succ[x] = z, prec[z] = x;
                    }
                } else {
                    if (prec[x] == z) {
                        std::swap(x, z);
                    }
                    if (succ[x] == z) {
                        area_X = area_X - (P[z] ^ P[x]);
                        area_X = area_X + (P[y] ^ P[x]);
                        area_X = area_X + (P[z] ^ P[y]);
                        succ[x] = y, prec[y] = x;
                        succ[y] = z, prec[z] = y;
                    }
                }
            }
        }
        checkMin(ans, area_X.getMinValue(imp[i].t, imp[i + 1].t));

    }

    std::cout << std::fixed << std::setprecision(10) << ans * 0.5 << '\n';

    return 0;
}