#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <utility> #include <vector> using namespace std; namespace { template <typename Scalar, typename Scalar2=Scalar> struct BasePoint { Scalar x; Scalar y; constexpr BasePoint(): x{0}, y{0} { } constexpr BasePoint(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BasePoint(BasePoint<OtherScalar, OtherScalar2> const& p): x(p.x), y(p.y) { } }; template <typename Scalar, typename Scalar2> constexpr bool operator==(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x == p2.x && p1.y == p2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x != p2.x || p1.y != p2.y; } template <typename Scalar, typename Scalar2=Scalar> struct BaseVector { Scalar x; Scalar y; constexpr BaseVector(): x{0}, y{0} { } constexpr BaseVector(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseVector(BaseVector<OtherScalar, OtherScalar2> const& v): x(v.x), y(v.y) { } int quadrant() const { if (x >= 0) { if (y >= 0) return 1; else return 4; } else { if (y >= 0) return 2; else return 3; } } }; template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x + b.x, a.y + b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v) { return v; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& v) { return {-v.x, -v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(BaseVector<Scalar, Scalar2> const& v, Scalar s) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(Scalar s, BaseVector<Scalar, Scalar2> const& v) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr Scalar2 operator*(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.x + Scalar2{a.y} * b.y; } template <typename Scalar, typename Scalar2> constexpr Scalar operator%(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.y - Scalar2{a.y} * b.x; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BasePoint<Scalar, Scalar2> const& a, BasePoint<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v, BasePoint<Scalar, Scalar2> const& p) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BasePoint<Scalar, Scalar2> const& p, BaseVector<Scalar, Scalar2> const& v) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr bool operator==(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x == v2.x && v1.y == v2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x != v2.x || v1.y != v2.y; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.quadrant() == v2.quadrant() && v1 % v2 == 0; } template <typename Scalar, typename Scalar2=Scalar> struct BaseLine { Scalar a; Scalar b; Scalar c; constexpr BaseLine(Scalar a_, Scalar b_, Scalar c_): a{a_}, b{b_}, c{c_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseLine(BaseLine<OtherScalar, OtherScalar2> const& l): a(l.a), b(l.b), c(l.c) { } constexpr static BaseLine of_direction(BaseVector<Scalar, Scalar2> const& v) { return {-v.y, v.x, 0}; } constexpr static BaseLine through(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return BaseLine::of_direction(p2 - p1).parallel_through(p1); } constexpr BaseVector<Scalar, Scalar2> direction() const { return {-b, a}; } constexpr BaseVector<Scalar, Scalar2> normal() const { return {a, b}; } constexpr BaseLine parallel_through(BasePoint<Scalar, Scalar2> const& p) const { return BaseLine{a, b, -(a * p.x + b * p.y)}; } constexpr BaseLine operator+() const { return *this; } constexpr BaseLine operator-() const { return BaseLine{-a, -b, -c}; } constexpr Scalar value_at(BasePoint<Scalar, Scalar2> const& p) { return a * p.x + b * p.y + c; } }; using FloatPoint = BasePoint<long double>; using FloatVector = BaseVector<long double>; using FloatLine = BaseLine<long double>; using Point = BasePoint<int, long long>; using Vector = BaseVector<int, long long>; using Line = BaseLine<int, long long>; FloatPoint intersection(FloatLine const& l1, FloatLine const& l2) { auto den = l1.b * l2.a - l1.a * l2.b; return {(l1.c * l2.b - l1.b * l2.c) / den, (l1.a * l2.c - l1.c * l2.a) / den}; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseLine<Scalar, Scalar2> const& l1, BaseLine<Scalar, Scalar2> const& l2) { return same_direction(l1.direction(), l2.direction()); } struct Tower: Point { int magician; constexpr Tower(int x_, int y_, int m): Point(x_, y_), magician{m} { } }; constexpr bool operator==(Tower const& t1, Tower const& t2) { return t1.x == t2.x && t1.y == t2.y && t1.magician == t2.magician; } constexpr bool operator!=(Tower const& t1, Tower const& t2) { return t1.x != t2.x || t1.y != t2.y || t1.magician != t2.magician; } bool above_intersection(Line const& line, Line const& l1, Line const& l2) { auto den = l1.b * l2.a - l1.a * l2.b; if (den == 0) return true; auto x = l1.c * l2.b - l1.b * l2.c; auto y = l1.a * l2.c - l1.c * l2.a; using LL = long long; if (den > 0) return LL{line.a} * x + LL{line.b} * y + LL{line.c} * den >= 0; else return LL{line.a} * x + LL{line.b} * y + LL{line.c} * den <= 0; } vector<Line> intersect_halfplanes(vector<Line> lines) { int n = lines.size(); sort(lines.begin(), lines.end(), [&](Line const& l1, Line const& l2) { auto v1 = l1.direction(); auto v2 = l2.direction(); auto q1 = v1.quadrant(); auto q2 = v2.quadrant(); if (q1 != q2) return q1 < q2; auto p = v1 % v2; if (p != 0) return p > 0; Line arb{1, 0, 0}; if (same_direction(l1, arb) || same_direction(l1, -arb)) arb = Line{0, 1, 0}; return above_intersection(l1, l2, arb) && !above_intersection(l2, l1, arb); }); vector<Line const*> queue(n); int b = 0; int e = 0; for (auto const& line: lines) { if (e > b && same_direction(line, *queue[e - 1])) continue; while (e - b >= 2 && above_intersection(line, *queue[e - 2], *queue[e - 1])) { --e; } while (e - b >= 2 && above_intersection(line, *queue[b], *queue[b + 1])) { ++b; } queue[e] = &line; ++e; } while (e - b >= 2 && above_intersection(*queue[b], *queue[e - 2], *queue[e - 1])) { --e; } vector<Line> res; res.reserve(e - b); for (int i = b; i < e; ++i) { res.emplace_back(*queue[i]); } return res; } long double triangle_area(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& p3) { return (p2 - p1) % (p3 - p1) / 2; } long double area(vector<Line> const& lines) { int n = lines.size(); if (n < 3) return 0; vector<FloatPoint> points; points.reserve(n); for (int i = 0, j = 1; i < n; ++i, ++j) { while (j >= n) j -= n; points.emplace_back(intersection(FloatLine(lines[i]), FloatLine(lines[j]))); } long double res = 0; for (int i = 2; i < n; ++i) { res += triangle_area(points[0], points[i-1], points[i]); } return abs(res); } long double solve(vector<Tower> towers) { int n = towers.size(); int m = n / 2; vector<Line> lines; for (auto const& t1: towers) { for (auto const& t2: towers) { if (t1.magician == t2.magician) continue; auto line = Line::through(t1, t2); vector<int> cnt(m, 0); for (auto const& t: towers) { auto v = line.value_at(t); if (v > 0) { ++cnt[t.magician]; if (cnt[t.magician] >= 2) { goto bad; } } } lines.emplace_back(move(line)); bad:; } } return area(intersect_halfplanes(move(lines))); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Tower> towers; towers.reserve(2 * n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int x, y; cin >> x >> y; towers.emplace_back(x, y, i); } } auto res = solve(move(towers)); cout << fixed << setprecision(numeric_limits<decltype(res)>::max_digits10) << res << endl; 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <limits> #include <utility> #include <vector> using namespace std; namespace { template <typename Scalar, typename Scalar2=Scalar> struct BasePoint { Scalar x; Scalar y; constexpr BasePoint(): x{0}, y{0} { } constexpr BasePoint(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BasePoint(BasePoint<OtherScalar, OtherScalar2> const& p): x(p.x), y(p.y) { } }; template <typename Scalar, typename Scalar2> constexpr bool operator==(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x == p2.x && p1.y == p2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return p1.x != p2.x || p1.y != p2.y; } template <typename Scalar, typename Scalar2=Scalar> struct BaseVector { Scalar x; Scalar y; constexpr BaseVector(): x{0}, y{0} { } constexpr BaseVector(Scalar x_, Scalar y_): x{x_}, y{y_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseVector(BaseVector<OtherScalar, OtherScalar2> const& v): x(v.x), y(v.y) { } int quadrant() const { if (x >= 0) { if (y >= 0) return 1; else return 4; } else { if (y >= 0) return 2; else return 3; } } }; template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x + b.x, a.y + b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v) { return v; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BaseVector<Scalar, Scalar2> const& v) { return {-v.x, -v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(BaseVector<Scalar, Scalar2> const& v, Scalar s) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator*(Scalar s, BaseVector<Scalar, Scalar2> const& v) { return {s * v.x, s * v.y}; } template <typename Scalar, typename Scalar2> constexpr Scalar2 operator*(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.x + Scalar2{a.y} * b.y; } template <typename Scalar, typename Scalar2> constexpr Scalar operator%(BaseVector<Scalar, Scalar2> const& a, BaseVector<Scalar, Scalar2> const& b) { return Scalar2{a.x} * b.y - Scalar2{a.y} * b.x; } template <typename Scalar, typename Scalar2> constexpr BaseVector<Scalar, Scalar2> operator-(BasePoint<Scalar, Scalar2> const& a, BasePoint<Scalar, Scalar2> const& b) { return {a.x - b.x, a.y - b.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BaseVector<Scalar, Scalar2> const& v, BasePoint<Scalar, Scalar2> const& p) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr BasePoint<Scalar, Scalar2> operator+(BasePoint<Scalar, Scalar2> const& p, BaseVector<Scalar, Scalar2> const& v) { return {v.x + p.x, v.y + p.y}; } template <typename Scalar, typename Scalar2> constexpr bool operator==(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x == v2.x && v1.y == v2.y; } template <typename Scalar, typename Scalar2> constexpr bool operator!=(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.x != v2.x || v1.y != v2.y; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseVector<Scalar, Scalar2> const& v1, BaseVector<Scalar, Scalar2> const& v2) { return v1.quadrant() == v2.quadrant() && v1 % v2 == 0; } template <typename Scalar, typename Scalar2=Scalar> struct BaseLine { Scalar a; Scalar b; Scalar c; constexpr BaseLine(Scalar a_, Scalar b_, Scalar c_): a{a_}, b{b_}, c{c_} { } template <typename OtherScalar, typename OtherScalar2> constexpr BaseLine(BaseLine<OtherScalar, OtherScalar2> const& l): a(l.a), b(l.b), c(l.c) { } constexpr static BaseLine of_direction(BaseVector<Scalar, Scalar2> const& v) { return {-v.y, v.x, 0}; } constexpr static BaseLine through(BasePoint<Scalar, Scalar2> const& p1, BasePoint<Scalar, Scalar2> const& p2) { return BaseLine::of_direction(p2 - p1).parallel_through(p1); } constexpr BaseVector<Scalar, Scalar2> direction() const { return {-b, a}; } constexpr BaseVector<Scalar, Scalar2> normal() const { return {a, b}; } constexpr BaseLine parallel_through(BasePoint<Scalar, Scalar2> const& p) const { return BaseLine{a, b, -(a * p.x + b * p.y)}; } constexpr BaseLine operator+() const { return *this; } constexpr BaseLine operator-() const { return BaseLine{-a, -b, -c}; } constexpr Scalar value_at(BasePoint<Scalar, Scalar2> const& p) { return a * p.x + b * p.y + c; } }; using FloatPoint = BasePoint<long double>; using FloatVector = BaseVector<long double>; using FloatLine = BaseLine<long double>; using Point = BasePoint<int, long long>; using Vector = BaseVector<int, long long>; using Line = BaseLine<int, long long>; FloatPoint intersection(FloatLine const& l1, FloatLine const& l2) { auto den = l1.b * l2.a - l1.a * l2.b; return {(l1.c * l2.b - l1.b * l2.c) / den, (l1.a * l2.c - l1.c * l2.a) / den}; } template <typename Scalar, typename Scalar2> constexpr bool same_direction(BaseLine<Scalar, Scalar2> const& l1, BaseLine<Scalar, Scalar2> const& l2) { return same_direction(l1.direction(), l2.direction()); } struct Tower: Point { int magician; constexpr Tower(int x_, int y_, int m): Point(x_, y_), magician{m} { } }; constexpr bool operator==(Tower const& t1, Tower const& t2) { return t1.x == t2.x && t1.y == t2.y && t1.magician == t2.magician; } constexpr bool operator!=(Tower const& t1, Tower const& t2) { return t1.x != t2.x || t1.y != t2.y || t1.magician != t2.magician; } bool above_intersection(Line const& line, Line const& l1, Line const& l2) { auto den = l1.b * l2.a - l1.a * l2.b; if (den == 0) return true; auto x = l1.c * l2.b - l1.b * l2.c; auto y = l1.a * l2.c - l1.c * l2.a; using LL = long long; if (den > 0) return LL{line.a} * x + LL{line.b} * y + LL{line.c} * den >= 0; else return LL{line.a} * x + LL{line.b} * y + LL{line.c} * den <= 0; } vector<Line> intersect_halfplanes(vector<Line> lines) { int n = lines.size(); sort(lines.begin(), lines.end(), [&](Line const& l1, Line const& l2) { auto v1 = l1.direction(); auto v2 = l2.direction(); auto q1 = v1.quadrant(); auto q2 = v2.quadrant(); if (q1 != q2) return q1 < q2; auto p = v1 % v2; if (p != 0) return p > 0; Line arb{1, 0, 0}; if (same_direction(l1, arb) || same_direction(l1, -arb)) arb = Line{0, 1, 0}; return above_intersection(l1, l2, arb) && !above_intersection(l2, l1, arb); }); vector<Line const*> queue(n); int b = 0; int e = 0; for (auto const& line: lines) { if (e > b && same_direction(line, *queue[e - 1])) continue; while (e - b >= 2 && above_intersection(line, *queue[e - 2], *queue[e - 1])) { --e; } while (e - b >= 2 && above_intersection(line, *queue[b], *queue[b + 1])) { ++b; } queue[e] = &line; ++e; } while (e - b >= 2 && above_intersection(*queue[b], *queue[e - 2], *queue[e - 1])) { --e; } vector<Line> res; res.reserve(e - b); for (int i = b; i < e; ++i) { res.emplace_back(*queue[i]); } return res; } long double triangle_area(FloatPoint const& p1, FloatPoint const& p2, FloatPoint const& p3) { return (p2 - p1) % (p3 - p1) / 2; } long double area(vector<Line> const& lines) { int n = lines.size(); if (n < 3) return 0; vector<FloatPoint> points; points.reserve(n); for (int i = 0, j = 1; i < n; ++i, ++j) { while (j >= n) j -= n; points.emplace_back(intersection(FloatLine(lines[i]), FloatLine(lines[j]))); } long double res = 0; for (int i = 2; i < n; ++i) { res += triangle_area(points[0], points[i-1], points[i]); } return abs(res); } long double solve(vector<Tower> towers) { int n = towers.size(); int m = n / 2; vector<Line> lines; for (auto const& t1: towers) { for (auto const& t2: towers) { if (t1.magician == t2.magician) continue; auto line = Line::through(t1, t2); vector<int> cnt(m, 0); for (auto const& t: towers) { auto v = line.value_at(t); if (v > 0) { ++cnt[t.magician]; if (cnt[t.magician] >= 2) { goto bad; } } } lines.emplace_back(move(line)); bad:; } } return area(intersect_halfplanes(move(lines))); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Tower> towers; towers.reserve(2 * n); for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int x, y; cin >> x >> y; towers.emplace_back(x, y, i); } } auto res = solve(move(towers)); cout << fixed << setprecision(numeric_limits<decltype(res)>::max_digits10) << res << endl; return 0; } |