#include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <cassert> using namespace std; using LD = long double; using LL = long long; const int N = 101; const LD eps = 1e-15, INF = 1e30, INF_LL = 1LL<<62; struct Point { LD x, y; Point() {} Point(LD x, LD y): x(x), y(y) {} bool operator==(const Point &other) const { return abs(x - other.x) < eps && abs(y - other.y) < eps; } Point operator-(const Point &other) const { return Point(x - other.x, y - other.y); } Point operator+(const Point &other) const { return Point(x + other.x, y + other.y); } bool operator<(const Point &other) const { return make_pair(x, y) < make_pair(other.x, other.y); } }; struct IntPoint { int x, y; IntPoint() {} IntPoint(int x, int y): x(x), y(y) {} IntPoint operator+(const IntPoint &other) const { return IntPoint(x + other.x, y + other.y); } IntPoint operator-(const IntPoint &other) const { return IntPoint(x - other.x, y - other.y); } bool operator<(const IntPoint &other) const { return make_pair(x, y) < make_pair(other.x, other.y); } }; const Point zero(0, 0); const IntPoint int_zero(0, 0); /* ostream& operator<<(ostream &out, const Point &p) { out << "(" << p.x << ", " << p.y << ")"; return out; } */ LD vecprod(const Point &a, const Point &b, const Point &c) { return (b.y - a.y) * (c.x - a.x) - (b.x - a.x) * (c.y - a.y); } LL int_vecprod(const IntPoint &a, const IntPoint &b, const IntPoint &c) { return (b.y - a.y) * 1LL * (c.x - a.x) - (b.x - a.x) * 1LL * (c.y - a.y); } LD scalarprod(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } bool active[N]; int n; IntPoint a[N], b[N]; vector<Point> points; LD dist2(const Point &a, const Point &b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } LD intersection_ratio(const IntPoint &c, const IntPoint &d, const IntPoint &a, const IntPoint &b) { return -((c.y - a.y) * 1LL * (b.x - a.x) - (c.x - a.x) * 1LL * (b.y - a.y)) / (long double)((c.x - d.x) * 1LL * (b.y - a.y) - (c.y - d.y) * 1LL * (b.x - a.x)); } int main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); cin >> n; vector<IntPoint> towers; for (int i=1; i<=n; i++) { cin >> a[i].x >> a[i].y >> b[i].x >> b[i].y; towers.push_back(a[i]); towers.push_back(b[i]); } vector<IntPoint> dirs; int m = towers.size(); for (int i=0; i<m; i++) { const auto &a = towers[i]; for (int j=i+1; j<m; j++) { const auto &b = towers[j]; dirs.emplace_back(a.x - b.x, a.y - b.y); dirs.emplace_back(a.x - b.x, b.y - a.y); } } vector<IntPoint> new_dirs; for (int i=0; i<dirs.size(); i++) { bool unique = true; for (int j=0; j<i && unique; j++) unique = (int_vecprod(int_zero, dirs[i], dirs[j]) != 0); if (unique) { new_dirs.push_back(dirs[i]); new_dirs.push_back(int_zero - dirs[i]); } } dirs = new_dirs; vector<pair<IntPoint, IntPoint>> borders; for (const auto &dir: dirs) { pair<LL, IntPoint> border = {INF_LL, int_zero}; for (int i=1; i<=n; i++) { LL v1 = int_vecprod(int_zero, dir, a[i]), v2 = int_vecprod(int_zero, dir, b[i]); if (v1 > v2) { border = min(border, make_pair(v1, a[i])); } else border = min(border, make_pair(v2, b[i])); } borders.emplace_back(border.second, border.second + dir); } vector<Point> safe_points; random_shuffle(borders.begin(), borders.end()); for (const auto &b: borders) { LD top_ratio = INF, bottom_ratio = -INF; bool top_bound = false, bottom_bound = false; for (const auto &c: borders) { LL v1 = int_vecprod(b.first, b.second, c.first); LL v2 = int_vecprod(b.first, b.second, c.second); if (v1 == v2) continue; LD ratio = intersection_ratio(b.first, b.second, c.first, c.second); if (v1 < v2) { top_ratio = min(top_ratio, ratio); top_bound = true; } else { bottom_ratio = max(bottom_ratio, ratio); bottom_bound = true; } if (top_bound && bottom_bound && top_ratio < bottom_ratio) break; } if (top_bound && bottom_bound && top_ratio - bottom_ratio > eps) { const IntPoint &p = b.first, &q = b.second; // Point x(p.x + top_ratio * (q.x - p.x), p.y + top_ratio * (q.y - p.y)); // Point y(p.x + bottom_ratio * (q.x - p.x), p.y + bottom_ratio * (q.y - p.y)); safe_points.emplace_back(p.x + top_ratio * (q.x - p.x), p.y + top_ratio * (q.y - p.y)); safe_points.emplace_back(p.x + bottom_ratio * (q.x - p.x), p.y + bottom_ratio * (q.y - p.y)); } } if (safe_points.size() < 6) { cout << 0.0 << endl; return 0; } sort(safe_points.begin(), safe_points.end()); safe_points.erase(unique(safe_points.begin(), safe_points.end()), safe_points.end()); for (Point &p: safe_points) { Point &first = safe_points[0]; if (p.x < first.x || (abs(p.x - first.x) < eps && p.y < first.y)) swap(first, p); } const Point &first = safe_points[0]; sort(safe_points.begin()+1, safe_points.end(), [&first](const Point &a, const Point &b) { return vecprod(first, a, b) > 0; }); vector<Point> stack = {safe_points[0], safe_points[1]}; for (int i=2; i<safe_points.size(); i++) { const Point &p = safe_points[i]; LD v; bool ignore = false; while (stack.size() >= 2 && (v = vecprod(stack[stack.size()-2], stack[stack.size()-1], p)) < eps) { if (abs(v) < eps) { if (dist2(stack[stack.size()-2], stack[stack.size()-1]) < dist2(stack[stack.size()-2], p)) stack.pop_back(); else { ignore = true; break; } } else stack.pop_back(); } if (!ignore) stack.push_back(p); } stack.push_back(stack[0]); LD res = 0; for (int i=0; i<stack.size()-1; i++) res += vecprod(zero, stack[i], stack[i+1]); cout << 0.5 * res << "\n"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <cassert> using namespace std; using LD = long double; using LL = long long; const int N = 101; const LD eps = 1e-15, INF = 1e30, INF_LL = 1LL<<62; struct Point { LD x, y; Point() {} Point(LD x, LD y): x(x), y(y) {} bool operator==(const Point &other) const { return abs(x - other.x) < eps && abs(y - other.y) < eps; } Point operator-(const Point &other) const { return Point(x - other.x, y - other.y); } Point operator+(const Point &other) const { return Point(x + other.x, y + other.y); } bool operator<(const Point &other) const { return make_pair(x, y) < make_pair(other.x, other.y); } }; struct IntPoint { int x, y; IntPoint() {} IntPoint(int x, int y): x(x), y(y) {} IntPoint operator+(const IntPoint &other) const { return IntPoint(x + other.x, y + other.y); } IntPoint operator-(const IntPoint &other) const { return IntPoint(x - other.x, y - other.y); } bool operator<(const IntPoint &other) const { return make_pair(x, y) < make_pair(other.x, other.y); } }; const Point zero(0, 0); const IntPoint int_zero(0, 0); /* ostream& operator<<(ostream &out, const Point &p) { out << "(" << p.x << ", " << p.y << ")"; return out; } */ LD vecprod(const Point &a, const Point &b, const Point &c) { return (b.y - a.y) * (c.x - a.x) - (b.x - a.x) * (c.y - a.y); } LL int_vecprod(const IntPoint &a, const IntPoint &b, const IntPoint &c) { return (b.y - a.y) * 1LL * (c.x - a.x) - (b.x - a.x) * 1LL * (c.y - a.y); } LD scalarprod(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } bool active[N]; int n; IntPoint a[N], b[N]; vector<Point> points; LD dist2(const Point &a, const Point &b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } LD intersection_ratio(const IntPoint &c, const IntPoint &d, const IntPoint &a, const IntPoint &b) { return -((c.y - a.y) * 1LL * (b.x - a.x) - (c.x - a.x) * 1LL * (b.y - a.y)) / (long double)((c.x - d.x) * 1LL * (b.y - a.y) - (c.y - d.y) * 1LL * (b.x - a.x)); } int main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); cin >> n; vector<IntPoint> towers; for (int i=1; i<=n; i++) { cin >> a[i].x >> a[i].y >> b[i].x >> b[i].y; towers.push_back(a[i]); towers.push_back(b[i]); } vector<IntPoint> dirs; int m = towers.size(); for (int i=0; i<m; i++) { const auto &a = towers[i]; for (int j=i+1; j<m; j++) { const auto &b = towers[j]; dirs.emplace_back(a.x - b.x, a.y - b.y); dirs.emplace_back(a.x - b.x, b.y - a.y); } } vector<IntPoint> new_dirs; for (int i=0; i<dirs.size(); i++) { bool unique = true; for (int j=0; j<i && unique; j++) unique = (int_vecprod(int_zero, dirs[i], dirs[j]) != 0); if (unique) { new_dirs.push_back(dirs[i]); new_dirs.push_back(int_zero - dirs[i]); } } dirs = new_dirs; vector<pair<IntPoint, IntPoint>> borders; for (const auto &dir: dirs) { pair<LL, IntPoint> border = {INF_LL, int_zero}; for (int i=1; i<=n; i++) { LL v1 = int_vecprod(int_zero, dir, a[i]), v2 = int_vecprod(int_zero, dir, b[i]); if (v1 > v2) { border = min(border, make_pair(v1, a[i])); } else border = min(border, make_pair(v2, b[i])); } borders.emplace_back(border.second, border.second + dir); } vector<Point> safe_points; random_shuffle(borders.begin(), borders.end()); for (const auto &b: borders) { LD top_ratio = INF, bottom_ratio = -INF; bool top_bound = false, bottom_bound = false; for (const auto &c: borders) { LL v1 = int_vecprod(b.first, b.second, c.first); LL v2 = int_vecprod(b.first, b.second, c.second); if (v1 == v2) continue; LD ratio = intersection_ratio(b.first, b.second, c.first, c.second); if (v1 < v2) { top_ratio = min(top_ratio, ratio); top_bound = true; } else { bottom_ratio = max(bottom_ratio, ratio); bottom_bound = true; } if (top_bound && bottom_bound && top_ratio < bottom_ratio) break; } if (top_bound && bottom_bound && top_ratio - bottom_ratio > eps) { const IntPoint &p = b.first, &q = b.second; // Point x(p.x + top_ratio * (q.x - p.x), p.y + top_ratio * (q.y - p.y)); // Point y(p.x + bottom_ratio * (q.x - p.x), p.y + bottom_ratio * (q.y - p.y)); safe_points.emplace_back(p.x + top_ratio * (q.x - p.x), p.y + top_ratio * (q.y - p.y)); safe_points.emplace_back(p.x + bottom_ratio * (q.x - p.x), p.y + bottom_ratio * (q.y - p.y)); } } if (safe_points.size() < 6) { cout << 0.0 << endl; return 0; } sort(safe_points.begin(), safe_points.end()); safe_points.erase(unique(safe_points.begin(), safe_points.end()), safe_points.end()); for (Point &p: safe_points) { Point &first = safe_points[0]; if (p.x < first.x || (abs(p.x - first.x) < eps && p.y < first.y)) swap(first, p); } const Point &first = safe_points[0]; sort(safe_points.begin()+1, safe_points.end(), [&first](const Point &a, const Point &b) { return vecprod(first, a, b) > 0; }); vector<Point> stack = {safe_points[0], safe_points[1]}; for (int i=2; i<safe_points.size(); i++) { const Point &p = safe_points[i]; LD v; bool ignore = false; while (stack.size() >= 2 && (v = vecprod(stack[stack.size()-2], stack[stack.size()-1], p)) < eps) { if (abs(v) < eps) { if (dist2(stack[stack.size()-2], stack[stack.size()-1]) < dist2(stack[stack.size()-2], p)) stack.pop_back(); else { ignore = true; break; } } else stack.pop_back(); } if (!ignore) stack.push_back(p); } stack.push_back(stack[0]); LD res = 0; for (int i=0; i<stack.size()-1; i++) res += vecprod(zero, stack[i], stack[i+1]); cout << 0.5 * res << "\n"; return 0; } |