#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const LD kEps = 1e-13; inline bool IsZero(int x) { return x == 0; } inline bool IsZero(LD x) { return abs(x) < kEps; } template <typename T> struct Point { T x, y; Point operator+(const Point<T> &a) const { return {x + a.x, y + a.y}; } Point operator-(const Point<T> &a) const { return {x - a.x, y - a.y}; } Point operator*(T coef) const { return {x * coef, y * coef}; } T CrossProd(const Point<T> &a) const { return x * a.y - y * a.x; } T CrossProd(const Point<T> &a, const Point<T> &b) const { return (a - *this).CrossProd(b - *this); } T DotProd(const Point<T> &a) const { return x * a.x + y * a.y; } T SqNorm() const { return x * x + y * y; } bool IsZero() const { return ::IsZero(x) && ::IsZero(y); } friend ostream &operator<<(ostream &os, const Point<T> &p) { return os << "(" << p.x << ", " << p.y << ")"; } }; template <typename T> struct Line { Point<T> p[2]; Point<T> &operator[](int a){ return p[a]; } // Z biblioteczki ACM-kowej vector<int> LineEqNormInt() { // seems ok int A = round(p[1].y - p[0].y); int B = round(p[0].x - p[1].x); int C = -(A * p[0].x + B * p[0].y); int gcd = abs(__gcd(A, __gcd(B, C))); vector<int> res{A, B, C}; for (auto& x : res) { x /= gcd; } if (A < 0 || (A == 0 && B < 0)) { for (auto& x : res) { x *= -1; } } return res; } }; // Z biblioteczki ACM-kowej namespace ACMLib { bool InUpper(const Point<LD> &a) { if (abs(a.y) > kEps) { return a.y > 0; } return a.x > 0; } bool angle_cmp(const Point<LD> &a, const Point<LD> &b) { bool u = InUpper(a); bool v = InUpper(b); return u!=v ? u : a.CrossProd(b)>0; } /** @brief a+(b-a)*f \in c+lin(d-c) @returns f */ LD cross(const Point<LD> &a, const Point<LD> &b, const Point<LD> &c, const Point<LD> &d) { return (d - c).CrossProd(a - c) / (d - c).CrossProd(a - b); } struct ClipLine { // valid side is on left ClipLine(Point<LD> A, Point<LD> B) : al(A), bl(B), a(A), b(B) {}; Point<LD> al,bl; // original line points mutable Point<LD> a,b; // actual intersection points Point<LD> dir() const { return bl - al; } bool operator<(const ClipLine& l) const { return angle_cmp(dir(),l.dir()); } Point<LD> cross(const ClipLine& l) { return al + (bl - al) * ACMLib::cross(al, bl, l.al, l.bl); } bool left(Point<LD> p) { return (bl - al).CrossProd(p - al) > 0; } }; struct Clip { Clip(LD r) : area(4*r*r) { Point<LD> a{-r,-r}, b{r,-r}, c{r,r}, d{-r,r}; lines = {ClipLine(a,b), ClipLine(b,c), ClipLine(c,d), ClipLine(d,a)}; } map<vector<int>, Line<int>> line_memo; // doesn't work when two equal lines are inserted // in such case create set of normalized equations of lines with custom == kEps void insert(Line<int> l) { debug(l[0], l[1]); auto line_repr = l.LineEqNormInt(); auto memo_iter = line_memo.find(line_repr); if (memo_iter != line_memo.end()) { auto prev_line = memo_iter->second; auto prev_vec = prev_line[1] - prev_line[0]; auto cur_vec = l[1] - l[0]; if (prev_vec.DotProd(cur_vec) < 0) { lines.clear(); } return; } line_memo[line_repr] = l; Point<LD> p0{(LD)l[0].x, (LD)l[0].y}, p1{(LD)l[1].x, (LD)l[1].y}; insert(ClipLine({p0, p1})); } void insert(ClipLine l) { assert(abs(l.dir().SqNorm()) > kEps); find(l); while (size() && !l.left(it->a) && !l.left(it->b)) { erase(); } if (size()) { while (prev(), size() && !l.left(it->a) && !l.left(it->b)) { erase(); } } if (size() && (!l.left(it->a) || !l.left(it->b))) { l.a = l.cross(*it); area -= l.a.CrossProd(it->b)*.5; it->b = l.a; next(); l.b = l.cross(*it); if ((l.a-l.b).SqNorm() < kEps) { l.b = l.a; } area -= it->a.CrossProd(l.b) * .5; it->a = l.b; if (!(l.a - l.b).IsZero()) { area += l.a.CrossProd(l.b)*.5; lines.insert(l); } } } void find(const ClipLine &l) { it = lines.lower_bound(l); if (it == lines.end()) { it = lines.begin(); } } void recalculate() { area = 0; for (const ClipLine &l : lines) area += l.a.CrossProd(l.b); area *= .5; } int size() { return lines.size(); } void next() { if(++it==lines.end()) it = lines.begin(); } void prev() { if(it==lines.begin()) it = lines.end(); --it; } void erase() { assert(it!=lines.end()); area -= it->a.CrossProd(it->b)*.5; it = lines.erase(it); if(it==lines.end()) it = lines.begin(); } typename set<ClipLine>::iterator it; set<ClipLine> lines; LD area; }; } int N; vector<array<Point<int>, 2>> magicians; vector<Point<int>> all_points; vector<Line<int>> limit_lines; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13); cerr << fixed << setprecision(6); cin >> N; magicians.resize(N); for (auto &magician : magicians) { for (int b : {0, 1}) { cin >> magician[b].x >> magician[b].y; all_points.push_back(magician[b]); } } const int S = SZ(all_points); for (int i = 0; i < S; ++i) { for (int j = i + 1; j < S; ++j) { const Point<int> &A = all_points[i]; const Point<int> &B = all_points[j]; bool can_be_left = true; bool can_be_right = true; for (const auto &magician : magicians) { bool local_can_be_left = false; bool local_can_be_right = false; for (int side : {0, 1}) { const int prod = A.CrossProd(B, magician[side]); if (prod >= 0) local_can_be_left = true; if (prod <= 0) local_can_be_right = true; } can_be_left &= local_can_be_left; can_be_right &= local_can_be_right; } if (can_be_left) limit_lines.push_back(Line<int>{{A, B}}); if (can_be_right) limit_lines.push_back(Line<int>{{B, A}}); } } ACMLib::Clip clip(1000); for (auto &limit_line : limit_lines) clip.insert(limit_line); clip.recalculate(); cout << clip.area << "\n"; }
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const LD kEps = 1e-13; inline bool IsZero(int x) { return x == 0; } inline bool IsZero(LD x) { return abs(x) < kEps; } template <typename T> struct Point { T x, y; Point operator+(const Point<T> &a) const { return {x + a.x, y + a.y}; } Point operator-(const Point<T> &a) const { return {x - a.x, y - a.y}; } Point operator*(T coef) const { return {x * coef, y * coef}; } T CrossProd(const Point<T> &a) const { return x * a.y - y * a.x; } T CrossProd(const Point<T> &a, const Point<T> &b) const { return (a - *this).CrossProd(b - *this); } T DotProd(const Point<T> &a) const { return x * a.x + y * a.y; } T SqNorm() const { return x * x + y * y; } bool IsZero() const { return ::IsZero(x) && ::IsZero(y); } friend ostream &operator<<(ostream &os, const Point<T> &p) { return os << "(" << p.x << ", " << p.y << ")"; } }; template <typename T> struct Line { Point<T> p[2]; Point<T> &operator[](int a){ return p[a]; } // Z biblioteczki ACM-kowej vector<int> LineEqNormInt() { // seems ok int A = round(p[1].y - p[0].y); int B = round(p[0].x - p[1].x); int C = -(A * p[0].x + B * p[0].y); int gcd = abs(__gcd(A, __gcd(B, C))); vector<int> res{A, B, C}; for (auto& x : res) { x /= gcd; } if (A < 0 || (A == 0 && B < 0)) { for (auto& x : res) { x *= -1; } } return res; } }; // Z biblioteczki ACM-kowej namespace ACMLib { bool InUpper(const Point<LD> &a) { if (abs(a.y) > kEps) { return a.y > 0; } return a.x > 0; } bool angle_cmp(const Point<LD> &a, const Point<LD> &b) { bool u = InUpper(a); bool v = InUpper(b); return u!=v ? u : a.CrossProd(b)>0; } /** @brief a+(b-a)*f \in c+lin(d-c) @returns f */ LD cross(const Point<LD> &a, const Point<LD> &b, const Point<LD> &c, const Point<LD> &d) { return (d - c).CrossProd(a - c) / (d - c).CrossProd(a - b); } struct ClipLine { // valid side is on left ClipLine(Point<LD> A, Point<LD> B) : al(A), bl(B), a(A), b(B) {}; Point<LD> al,bl; // original line points mutable Point<LD> a,b; // actual intersection points Point<LD> dir() const { return bl - al; } bool operator<(const ClipLine& l) const { return angle_cmp(dir(),l.dir()); } Point<LD> cross(const ClipLine& l) { return al + (bl - al) * ACMLib::cross(al, bl, l.al, l.bl); } bool left(Point<LD> p) { return (bl - al).CrossProd(p - al) > 0; } }; struct Clip { Clip(LD r) : area(4*r*r) { Point<LD> a{-r,-r}, b{r,-r}, c{r,r}, d{-r,r}; lines = {ClipLine(a,b), ClipLine(b,c), ClipLine(c,d), ClipLine(d,a)}; } map<vector<int>, Line<int>> line_memo; // doesn't work when two equal lines are inserted // in such case create set of normalized equations of lines with custom == kEps void insert(Line<int> l) { debug(l[0], l[1]); auto line_repr = l.LineEqNormInt(); auto memo_iter = line_memo.find(line_repr); if (memo_iter != line_memo.end()) { auto prev_line = memo_iter->second; auto prev_vec = prev_line[1] - prev_line[0]; auto cur_vec = l[1] - l[0]; if (prev_vec.DotProd(cur_vec) < 0) { lines.clear(); } return; } line_memo[line_repr] = l; Point<LD> p0{(LD)l[0].x, (LD)l[0].y}, p1{(LD)l[1].x, (LD)l[1].y}; insert(ClipLine({p0, p1})); } void insert(ClipLine l) { assert(abs(l.dir().SqNorm()) > kEps); find(l); while (size() && !l.left(it->a) && !l.left(it->b)) { erase(); } if (size()) { while (prev(), size() && !l.left(it->a) && !l.left(it->b)) { erase(); } } if (size() && (!l.left(it->a) || !l.left(it->b))) { l.a = l.cross(*it); area -= l.a.CrossProd(it->b)*.5; it->b = l.a; next(); l.b = l.cross(*it); if ((l.a-l.b).SqNorm() < kEps) { l.b = l.a; } area -= it->a.CrossProd(l.b) * .5; it->a = l.b; if (!(l.a - l.b).IsZero()) { area += l.a.CrossProd(l.b)*.5; lines.insert(l); } } } void find(const ClipLine &l) { it = lines.lower_bound(l); if (it == lines.end()) { it = lines.begin(); } } void recalculate() { area = 0; for (const ClipLine &l : lines) area += l.a.CrossProd(l.b); area *= .5; } int size() { return lines.size(); } void next() { if(++it==lines.end()) it = lines.begin(); } void prev() { if(it==lines.begin()) it = lines.end(); --it; } void erase() { assert(it!=lines.end()); area -= it->a.CrossProd(it->b)*.5; it = lines.erase(it); if(it==lines.end()) it = lines.begin(); } typename set<ClipLine>::iterator it; set<ClipLine> lines; LD area; }; } int N; vector<array<Point<int>, 2>> magicians; vector<Point<int>> all_points; vector<Line<int>> limit_lines; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13); cerr << fixed << setprecision(6); cin >> N; magicians.resize(N); for (auto &magician : magicians) { for (int b : {0, 1}) { cin >> magician[b].x >> magician[b].y; all_points.push_back(magician[b]); } } const int S = SZ(all_points); for (int i = 0; i < S; ++i) { for (int j = i + 1; j < S; ++j) { const Point<int> &A = all_points[i]; const Point<int> &B = all_points[j]; bool can_be_left = true; bool can_be_right = true; for (const auto &magician : magicians) { bool local_can_be_left = false; bool local_can_be_right = false; for (int side : {0, 1}) { const int prod = A.CrossProd(B, magician[side]); if (prod >= 0) local_can_be_left = true; if (prod <= 0) local_can_be_right = true; } can_be_left &= local_can_be_left; can_be_right &= local_can_be_right; } if (can_be_left) limit_lines.push_back(Line<int>{{A, B}}); if (can_be_right) limit_lines.push_back(Line<int>{{B, A}}); } } ACMLib::Clip clip(1000); for (auto &limit_line : limit_lines) clip.insert(limit_line); clip.recalculate(); cout << clip.area << "\n"; } |