#include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; 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 T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } typedef long double LD; const LD kEps = 1e-9; const LD kPi = 2 * acos(0); LD Sq(LD x) { return x * x; } struct Point { LD x, y; Point() {} Point(LD a, LD b) : x(a), y(b) {} Point(const Point& a) : x(a.x), y(a.y) {} void operator=(const Point& a) { x = a.x; y = a.y; } Point operator+(const Point& a) const { Point p(x + a.x, y + a.y); return p; } Point operator-(const Point& a) const { Point p(x - a.x, y - a.y); return p; } Point operator*(LD a) const { Point p(x * a, y * a); return p; } Point operator/(LD a) const { assert(abs(a) > kEps); Point p(x / a, y / a); return p; } Point& operator+=(const Point& a) { x += a.x; y += a.y; return *this; } Point& operator-=(const Point& a) { x -= a.x; y -= a.y; return *this; } Point& operator*=(LD a) { x *= a; y *= a; return *this;} Point& operator/=(LD a) { assert(abs(a) > kEps); x /= a; y /= a; return *this; } bool IsZero() const { return abs(x) < kEps && abs(y) < kEps; } bool operator==(const Point& a) const { return (*this - a).IsZero(); } LD CrossProd(const Point& a) const { return x * a.y - y * a.x; } LD CrossProd(Point a, Point b) const { a -= *this; b -= *this; return a.CrossProd(b); } LD DotProd(const Point& a) const { return x * a.x + y * a.y; } LD Norm() const { return sqrt(Sq(x) + Sq(y)); } void NormalizeSelf() { *this /= Norm(); } Point Normalize() { Point res(*this); res.NormalizeSelf(); return res; } LD Dist(const Point& a) const { return (*this - a).Norm(); } LD Angle() const { return atan2(y, x); } void RotateSelf(LD angle) { LD c = cos(angle); LD s = sin(angle); LD nx = x * c - y * s; LD ny = y * c + x * s; y = ny; x = nx; } Point Rotate(LD angle) const { Point res(*this); res.RotateSelf(angle); return res; } static bool LexCmp(const Point& a, const Point& b) { if (abs(a.x - b.x) > kEps) { return a.x < b.x; } return a.y < b.y; } LD SqNorm() { return x * x + y * y; } friend ostream& operator<<(ostream& out, Point m); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } struct Circle { Point center; LD r; Circle(LD x, LD y, LD rad) { center = Point(x, y); r = rad; } Circle(const Point& a, LD rad) : center(a), r(rad) {} LD Area() const { return kPi * Sq(r); } LD Perimeter() const { return 2 * kPi * r; } LD Diameter() const { return 2 * r; } Point RotateRightMost(LD ang) const { return center + Point{r * cos(ang), r * sin(ang)}; } bool operator==(const Circle& c) const { return center == c.center && abs(r - c.r) < kEps; } }; struct Line { Point p[2]; bool is_seg; Line(Point a, Point b, bool is_seg_ = false) { p[0] = a; p[1] = b; is_seg = is_seg_; } Line() { } Point& operator[](int a) { return p[a]; } Point NormalVector() { Point perp = p[1] - p[0]; perp.RotateSelf(kPi / 2); perp.NormalizeSelf(); return perp; } // (A, B, C) such that A^2 + B^2 = 1, (A, B) > (0, 0) vector<LD> LineEqNormLD() { // seems ok LD A = p[1].y - p[0].y; LD B = p[0].x - p[1].x; LD C = -(A * p[0].x + B * p[0].y); assert(abs(A * p[1].x + B * p[1].y + C) < kEps); LD norm = sqrt(Sq(A) + Sq(B)); vector<LD> res{A, B, C}; for (auto& x : res) { x /= norm; } if (A < -kEps || (abs(A) < kEps && B < -kEps)) { for (auto& x : res) { x *= -1; } } return res; } // assumes that coordinates are integers! 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; } }; struct Utils { // 0, 1, 2 or 3 pts. In case of 3 pts it means they are equal static vector<Point> InterCircleCircle(Circle a, Circle b) { if (a.r + kEps < b.r) { swap(a, b); } if (a == b) { return vector<Point>{a.RotateRightMost(0), a.RotateRightMost(2 * kPi / 3), a.RotateRightMost(4 * kPi / 3)}; } Point diff = b.center - a.center; LD dis = diff.Norm(); LD ang = diff.Angle(); LD longest = max(max(a.r, b.r), dis); LD per = a.r + b.r + dis; if (2 * longest > per + kEps) { return vector<Point>(); } if (abs(2 * longest - per) < 2 * kEps) { return vector<Point>{a.RotateRightMost(ang)}; } LD ang_dev = acos((Sq(a.r) + Sq(dis) - Sq(b.r)) / (2 * a.r * dis)); return vector<Point>{a.RotateRightMost(ang - ang_dev), a.RotateRightMost(ang + ang_dev)}; } static vector<Point> InterLineLine(Line& a, Line& b) { // working fine Point vec_a = a[1] - a[0]; Point vec_b1 = b[1] - a[0]; Point vec_b0 = b[0] - a[0]; LD tr_area = vec_b1.CrossProd(vec_b0); LD quad_area = vec_b1.CrossProd(vec_a) + vec_a.CrossProd(vec_b0); if (abs(quad_area) < kEps) { // parallel or coinciding if (PtBelongToLine(b, a[0])) { return {a[0], a[1]}; } else { return {}; } } return {a[0] + vec_a * (tr_area / quad_area)}; } static Point ProjPointToLine(Point p, Line l) { ///Tested Point diff = l[1] - l[0]; return l[0] + diff * (diff.DotProd(p - l[0]) / diff.DotProd(diff)); } static Point ReflectPtWRTLine(Point p, Line l) { Point proj = ProjPointToLine(p, l); return proj * 2 - p; } static vector<Point> InterCircleLine(Circle c, Line l) { /// Tested here: http://codeforces.com/gym/100554/submission/10197624 Point proj = ProjPointToLine(c.center, l); LD dis_proj = c.center.Dist(proj); if (dis_proj > c.r + kEps) { return vector<Point>(); } LD a = sqrt(max((LD)0, Sq(c.r) - Sq(dis_proj))); Point dir = l[1] - l[0]; LD dir_norm = dir.Norm(); vector<Point> cands{proj + dir * (a / dir_norm), proj - dir * (a / dir_norm)}; if (cands[0].Dist(cands[1]) < kEps) { return vector<Point>{proj}; } return cands; } static bool PtBelongToLine(Line l, Point p) { return abs(l[0].CrossProd(l[1], p)) < kEps; } static bool PtBelongToSeg(Line l, Point p) { // seems ok return abs(p.Dist(l[0]) + p.Dist(l[1]) - l[0].Dist(l[1])) < kEps; } static vector<Point> InterCircleSeg(Circle c, Line l) { //seems ok vector<Point> from_line = InterCircleLine(c, l); vector<Point> res; for (auto p : from_line) { if (PtBelongToSeg(l, p)) { res.PB(p); } } return res; } static vector<Point> TangencyPtsToCircle(Circle c, Point p) { // seems ok LD d = c.center.Dist(p); if (d < c.r - kEps) { return {}; } if (d < c.r + kEps) { return {p}; } LD from_cent = (p - c.center).Angle(); LD ang_dev = acos(c.r / d); return {c.RotateRightMost(from_cent - ang_dev), c.RotateRightMost(from_cent + ang_dev)}; } // outer and inner tangents tested only locally (however I believe that rigorously) static vector<Line> OuterTangents(Circle c1, Circle c2) { if (c1 == c2) { return {}; } // is it surely best choice? if (c1.r < c2.r) { swap(c1, c2); } if (c2.r + c1.center.Dist(c2.center) < c1.r - kEps) { return {}; } if (abs(c1.r - c2.r) < kEps) { Point diff = c2.center - c1.center; Point R = diff.Rotate(kPi / 2) * (c1.r / diff.Norm()); return {{c1.center + R, c2.center + R}, {c1.center - R, c2.center - R}}; } Point I = c1.center + (c2.center - c1.center) * (c1.r / (c1.r - c2.r)); if (c2.r + c1.center.Dist(c2.center) < c1.r + kEps) { return {{I, I + (c2.center - c1.center).Rotate(kPi / 2)}}; } vector<Point> to1 = TangencyPtsToCircle(c1, I); vector<Point> to2 = TangencyPtsToCircle(c2, I); vector<Line> res{{to1[0], to2[0]}, {to1[1], to2[1]}}; assert(Utils::PtBelongToLine(res[0], I)); assert(Utils::PtBelongToLine(res[1], I)); return res; } // unfortunately big part of code is same as in previous function // can be joined when putting appropriate signs in few places // however those ifs differ a bit hence it may not be good idea // to necessarily join them static vector<Line> InnerTangents(Circle c1, Circle c2) { if (c1 == c2) { return {}; } // this time surely best choice if (c1.r < c2.r) { swap(c1, c2); } LD d = c1.center.Dist(c2.center); if (d < c1.r + c2.r - kEps) { return {}; } Point I = c1.center + (c2.center - c1.center) * (c1.r / (c1.r + c2.r)); if (d < c1.r + c2.r + kEps) { return {{I, I + (c2.center - c1.center).Rotate(kPi / 2)}}; } vector<Point> to1 = TangencyPtsToCircle(c1, I); vector<Point> to2 = TangencyPtsToCircle(c2, I); vector<Line> res{{to1[0], to2[0]}, {to1[1], to2[1]}}; assert(Utils::PtBelongToLine(res[0], I)); assert(Utils::PtBelongToLine(res[1], I)); return res; } static bool AreParallel(Line l1, Line l2) { // seems ok return abs(l1[0].CrossProd(l2[0], l1[1]) - l1[0].CrossProd(l2[1], l1[1])) < kEps; } // returns a vector of points such that their convex hull is intersection of those segments // SZ(res) == 0 => empty intersection, SZ(res) == 1 => intersection is a point, SZ(res) == 2 => intersection is a segment static vector<Point> InterSegs(Line l1, Line l2) { // seems ok if (!Point::LexCmp(l1[0], l1[1])) { swap(l1[0], l1[1]); } if (!Point::LexCmp(l2[0], l2[1])) { swap(l2[0], l2[1]); } if (AreParallel(l1, l2)) { if (!PtBelongToLine(l1, l2[0])) { return vector<Point>(); } vector<Point> ends(2); for (int tr = 0; tr < 2; tr++) { if (Point::LexCmp(l1[tr], l2[tr]) ^ tr) { ends[tr] = l2[tr]; } else { ends[tr] = l1[tr]; } } if ((ends[1] - ends[0]).IsZero()) { ends.pop_back(); } if (SZ(ends) == 2 && Point::LexCmp(ends[1], ends[0])) { return vector<Point>(); } return ends; } else { vector<Point> p = InterLineLine(l1, l2); if (PtBelongToSeg(l1, p[0]) && PtBelongToSeg(l2, p[0])) { return p; } return vector<Point>(); } } static LD Angle(Point P, Point Q, Point R) { // angle PQR LD ang2 = (P - Q).Angle(); LD ang1 = (R - Q).Angle(); LD ans = ang1 - ang2; if (ans < kEps) { ans += 2 * kPi; } return ans; } // tested here: http://codeforces.com/contest/600/submission/14961583 // DON'T change anything as this will lead to precision errors // don't know why, but this is the only version which works precisely even for very mean cases static LD DiskInterArea(Circle c1, Circle c2) { // tested here: http://opentrains.snarknews.info/~ejudge/team.cgi?contest_id=006254 problem I if (c1.r < c2.r) { swap(c1, c2); } LD d = c1.center.Dist(c2.center); if (c1.r + c2.r < d + kEps) { return 0; } if (c1.r - c2.r > d - kEps) { return kPi * Sq(c2.r); } LD alfa = acos((Sq(d) + Sq(c1.r) - Sq(c2.r)) / (2 * d * c1.r)); LD beta = acos((Sq(d) + Sq(c2.r) - Sq(c1.r)) / (2 * d * c2.r)); return alfa * Sq(c1.r) + beta * Sq(c2.r) - sin(2 * alfa) * Sq(c1.r) / 2 - sin(2 * beta) * Sq(c2.r) / 2; } static Line RadAxis(Circle c1, Circle c2) { LD d = c1.center.Dist(c2.center); LD a = (Sq(c1.r) - Sq(c2.r) + Sq(d)) / (2 * d); Point Q = c1.center + (c2.center - c1.center) * (a / d); Point R = Q + (c2.center - c1.center).Rotate(kPi / 2); return Line(Q, R); } }; struct Polygon { vector<Point> pts; Polygon(vector<Point> pts_) : pts(pts_) {} Polygon() : Polygon(vector<Point>()) {} void Add(Point p) { pts.push_back(p); } // positive for counterclockwise double Area() { double area = 0; for (int i = 0; i < SZ(pts); i++) { area += pts[i].CrossProd(pts[(i + 1) % SZ(pts)]); } area /= 2; return area; } void OrientCounterclockwise() { if (Area() < 0) { reverse(pts.begin(), pts.end()); } } int next(int a) { if (a + 1 < SZ(pts)) { return a + 1; } return 0; } pair<int, int> FurthestPair() { // tested here: http://codeforces.com/contest/333/submission/11058065 MakeConvexHull(); OrientCounterclockwise(); int furth = 1; pair<int, int> best_pair = make_pair(0, 0); double best_dis = 0; for (int i = 0; i < SZ(pts); i++) { Point side = pts[next(i)] - pts[i]; while (side.CrossProd(pts[furth] - pts[i]) < side.CrossProd(pts[next(furth)] - pts[i])) { furth = next(furth); } vector<int> vec{i, next(i)}; for (auto ind : vec) { if (pts[ind].Dist(pts[furth]) > best_dis) { best_pair = make_pair(ind, furth); best_dis = pts[ind].Dist(pts[furth]); } } cerr<<"Furthest from: "<<pts[i]<<"-"<<pts[next(i)]<<" is "<<pts[furth]<<endl; } return best_pair; } // for square 34 // 12 holds one_way_hull = {{1,3,4},{1,2,4}} // resulting polygon is counterclockwise {1, 2, 4, 3} vector<vector<Point>> MakeConvexHull() { // tested everywhere http://codeforces.com/contest/333/submission/11058065 vector<vector<Point>> one_way_hull(2); sort(pts.begin(), pts.end(), Point::LexCmp); for (int dir = -1; dir <= 1; dir += 2) { int hull_num = (dir + 1) / 2; auto& H = one_way_hull[hull_num]; one_way_hull[hull_num].push_back(pts[0]); if (SZ(pts) > 1) { H.push_back(pts[1]); } for (int i = 2; i < SZ(pts); i++) { while (SZ(H) >= 2 && dir * (pts[i] - H[SZ(H) - 2]).CrossProd(H.back() - H[SZ(H) - 2]) > -kEps) { H.pop_back(); } H.push_back(pts[i]); } if (SZ(H) > 1 && (H[0] - H.back()).IsZero()) { H.pop_back(); } } pts.clear(); for (auto p : one_way_hull[1]) { pts.push_back(p); } for (int i = SZ(one_way_hull[0]) - 2; i >= 1; i--) { pts.push_back(one_way_hull[0][i]); } return one_way_hull; } // without sides vector<vector<bool>> InsideDiagonalsMatrix() { // tested here: http://codeforces.com/contest/438/submission/11063385 int n = pts.size(); vector<vector<bool>> res(n, vector<bool>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Line diag(pts[i], pts[j]); if (i == j || abs(i - j) == 1 || abs(i - j) == n - 1) { continue; } res[i][j] = 1; for (int k = 0; k < n; k++) { int kk = next(k); Line side(pts[k], pts[kk]); if (k == i || k == j || kk == i || kk == j) { continue; } vector<Point> inter = Utils::InterSegs(diag, side); if (SZ(inter)) { res[i][j] = 0; } } int act = next(i); LD areas[2] = {0, 0}; int passed_j = 0; while (act != i) { passed_j |= (act == j); areas[passed_j] += pts[i].CrossProd(pts[act], pts[next(act)]); act = next(act); } if (areas[0] * areas[1] < kEps) { res[i][j] = 0; } } } return res; } // P needs to be strictly outside polygon // polygon needs to be STRICTLY convex and counterclockwise oriented (as MakeConvexHull does) // returns {L, R} so that PL, PR are tangents and PL is on left vector<Point> Tangents(Point p) { // tested here: https://icpc.kattis.com/problems/spin (1169964) vector<Point> res; REP (tr, 2) { auto GrThan = [&](int fir, int sec) { // fir on sec's left return p.CrossProd(pts[sec], pts[fir]) > kEps; }; bool up = false; int cr = 0; if (SZ(pts) >= 2) { cr = p.CrossProd(pts[0], pts[1]); } if (abs(cr) < kEps && SZ(pts) >= 3) { cr = p.CrossProd(pts[0], pts[2]); } up = (cr > 0); VI bd{1, SZ(pts) - 1}; int faj = 0; while (bd[0] + 6 <= bd[1]) { // better don't replace with smaller constants VI h(2); REP (hh, 2) { h[hh] = (bd[0] + bd[1] + bd[hh]) / 3; } if (!GrThan(h[up ^ tr], 0) ^ tr) { bd[up ^ tr] = h[up ^ tr]; } else { int gr = GrThan(h[0], h[1]); bd[gr ^ tr] = h[gr ^ tr]; } } FOR (i, bd[0], bd[1]) { if (GrThan(i, faj) ^ tr) { faj = i; } } res.PB(pts[faj]); } return res; } }; struct ConvexPolHalves { // tested here: https://icpc.kattis.com/problems/spin (1169964) vector<vector<Point>> chains; // initialized by MakeConvexHull bool BelongTo(Point p) { // including borders if (SZ(chains[0]) == 1) { return (chains[0][0] - p).IsZero(); } if (p.x + kEps < chains[0][0].x || p.x - kEps > chains[0].back().x) { return false; } REP (tr, 2) { int kl = 0, kp = SZ(chains[tr]) - 2, faj = 0; while (kl <= kp) { int aktc = (kl + kp) / 2; if (chains[tr][aktc].x < p.x + kEps) { kl = aktc + 1; faj = aktc; } else { kp = aktc - 1; } } Point fir = chains[tr][faj], sec = chains[tr][faj + 1]; if (abs(fir.x - sec.x) < kEps) { if (tr == 0) { if (sec.y + kEps < p.y) { return false; } } else { if (fir.y - kEps > p.y) { return false; } } } else { LD cr = fir.CrossProd(sec, p); if (abs(cr) < kEps) { return true; } if ((cr > 0) ^ tr) { return false; } } } return true; } }; // CLIP START bool InUpper(Point a) { if (abs(a.y) > kEps) { return a.y > 0; } return a.x > 0; } bool angle_cmp(const Point a, const Point 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(Point a, Point b, Point c, Point d) { return (d - c).CrossProd(a - c) / (d - c).CrossProd(a - b); } struct ClipLine { // valid side is on left ClipLine(Point A, Point B) : al(A), bl(B), a(A), b(B) {}; Point al,bl; // original line points mutable Point a,b; // actual intersection points Point dir() const { return bl - al; } bool operator<(const ClipLine& l) const { return angle_cmp(dir(),l.dir()); } Point cross(const ClipLine& l) { return al + (bl - al) * ::cross(al, bl, l.al, l.bl); } bool left(Point p) { return (bl - al).CrossProd(p - al) > 0; } }; set<VI> put; struct Clip { Clip(LD r) : area(4*r*r) { Point 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)}; } void insert(Line l) { VI abc = l.LineEqNormInt(); if (put.count(abc)) { return; } put.insert(abc); insert(ClipLine(l[0], l[1])); } 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); } } //assert(l.dir().SqNorm()>1e-13); } 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; }; // CLIP ENDS // CENTERS BEGIN Point Bary(Point A, Point B, Point C, LD a, LD b, LD c) { return (A * a + B * b + C * c) / (a + b + c); } Point Centroid(Point A, Point B, Point C) { return Bary(A, B, C, 1, 1, 1); } Point Circumcenter(Point A, Point B, Point C) { LD a = (B - C).SqNorm(), b = (C - A).SqNorm(), c = (A - B).SqNorm(); return Bary(A, B, C, a * (b + c - a), b * (c + a - b), c * (a + b - c)); } Point Incenter(Point A, Point B, Point C) { return Bary(A, B, C, (B - C).Norm(), (A - C).Norm(), (A - B).Norm()); } Point Orthocenter(Point A, Point B, Point C) { LD a = (B - C).SqNorm(), b = (C - A).SqNorm(), c = (A - B).SqNorm(); return Bary(A, B, C, (a+b-c)*(c+a-b), (b+c-a)*(a+b-c), (c+a-b)*(b+c-a)); } Point Excenter(Point A, Point B, Point C) { // opposite to A LD a = (B - C).Norm(), b = (A - C).Norm(), c = (A - B).Norm(); return Bary(A, B, C, -a, b, c); } // const LD kEps = 1e-9; // const LD kPi = 2 * acos(0); struct PointInt { int x, y; PointInt operator-(PointInt p) { return {x - p.x, y - p.y}; } PointInt operator+(PointInt p) { return {x + p.x, y + p.y}; } // Point operator*(LD f) { return {x * f, y * f}; } // bool operator<(const Point& oth) const { // if (abs(x - oth.x) > kEps) { return x < oth.x; } // if (abs(y - oth.y) > kEps) { return y < oth.y; } // return false; // } // LD Angle() { // return atan2(y, x); // } }; // LD Normalize(LD ang) { // if (ang < -kPi) { // ang += 2 * kPi; // } // if (ang > kPi) { // ang -= 2 * kPi; // } // return ang; // } int CrossProd(PointInt A, PointInt B, PointInt C) { B = B - A; C = C - A; return B.x * C.y - B.y * C.x; } // vector<Point> InterLineLine(Point A, Point B, Point P, Point Q) { // LD tr_area = CrossProd(A, P, Q); // LD quad_area = CrossProd(A, P, B) + CrossProd(A, B, Q); // if (abs(quad_area) < 1e-9) { // return {}; // } // return {A + (B - A) * (tr_area / quad_area)}; // } const int N = 222; int n; vector<PointInt> segs[N]; // bool CheckPoint(Point p) { // int bil = 0; // vector<pair<LD, int>> evs; // RE (i, n) { // Point D1 = segs[i][0] - p; // Point D2 = segs[i][1] - p; // LD ang1 = D1.Angle(); // LD ang2 = D2.Angle(); // LD diff = Normalize(ang2 - ang1); // if (diff < 0) { // swap(D1, D2); // swap(ang1, ang2); // } // LD from = Normalize(ang2 - kPi / 2); // LD to = Normalize(ang1 + kPi / 2); // evs.PB({from, 1}); // evs.PB({to, -1}); // if (from > to) { // bil++; // } // } // sort(ALL(evs)); // if (bil == 0) { return false; } // for (auto ev : evs) { // bil += ev.nd; // if (bil == 0) { return false; } // } // return true; // } Point PFromPI(PointInt P) { debug(P.x, P.y); return {(LD)P.x, (LD)P.y}; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; cin>>n; vector<Point> pts; RE (i, n) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; segs[i].PB(PointInt{x1, y1}); segs[i].PB(PointInt{x2, y2}); //pts.PB(Point{LD(x1), LD(y1)}); //pts.PB(Point{LD(x2), LD(y2)}); } //map<Point, int> ids; Clip clip(1e4); set<VI> put_lines; RE (i, n) { RE (j, i - 1) { REP (ti, 2) { REP (tj, 2) { bool can_all_atmost0 = 1, can_all_atleast0 = 1; PointInt P = segs[i][ti], Q = segs[j][tj]; RE (k, n) { if (k == i || k == j) { continue; } int cr0 = CrossProd(P, Q, segs[k][0]); int cr1 = CrossProd(P, Q, segs[k][1]); can_all_atleast0 &= (cr0 >= 0 || cr1 >= 0); can_all_atmost0 &= (cr0 <= 0 || cr1 <= 0); } if (can_all_atleast0 && can_all_atmost0) { cout<<(LD)0<<endl; return 0; } if (can_all_atleast0) { debug(">=0"); Line l = Line{PFromPI(P), PFromPI(Q)}; clip.insert(l); } if (can_all_atmost0) { debug("<=0"); clip.insert(Line{PFromPI(Q), PFromPI(P)}); } } } } } clip.recalculate(); cout<<clip.area<<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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | #include <bits/stdc++.h> #define MP make_pair #define PB push_back #define int long long #define st first #define nd second #define rd third #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define REP(i, n) for(int i = 0;i <(n); ++i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define __builtin_ctz __builtin_ctzll #define __builtin_clz __builtin_clzll #define __builtin_popcount __builtin_popcountll using namespace std; template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; } template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...); } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(...) (__VA_ARGS__) #define debugv(x) #define cerr if(0)cout #endif #define next ____next #define prev ____prev #define left ____left #define hash ____hash typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; 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 T1, class T2> ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";} template<class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } }; template<class T> void ResizeVec(T&, vector<int>) {} template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } typedef Triple<int, int, int> TIII; template<class A, class B, class C> ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; } template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; } typedef long double LD; const LD kEps = 1e-9; const LD kPi = 2 * acos(0); LD Sq(LD x) { return x * x; } struct Point { LD x, y; Point() {} Point(LD a, LD b) : x(a), y(b) {} Point(const Point& a) : x(a.x), y(a.y) {} void operator=(const Point& a) { x = a.x; y = a.y; } Point operator+(const Point& a) const { Point p(x + a.x, y + a.y); return p; } Point operator-(const Point& a) const { Point p(x - a.x, y - a.y); return p; } Point operator*(LD a) const { Point p(x * a, y * a); return p; } Point operator/(LD a) const { assert(abs(a) > kEps); Point p(x / a, y / a); return p; } Point& operator+=(const Point& a) { x += a.x; y += a.y; return *this; } Point& operator-=(const Point& a) { x -= a.x; y -= a.y; return *this; } Point& operator*=(LD a) { x *= a; y *= a; return *this;} Point& operator/=(LD a) { assert(abs(a) > kEps); x /= a; y /= a; return *this; } bool IsZero() const { return abs(x) < kEps && abs(y) < kEps; } bool operator==(const Point& a) const { return (*this - a).IsZero(); } LD CrossProd(const Point& a) const { return x * a.y - y * a.x; } LD CrossProd(Point a, Point b) const { a -= *this; b -= *this; return a.CrossProd(b); } LD DotProd(const Point& a) const { return x * a.x + y * a.y; } LD Norm() const { return sqrt(Sq(x) + Sq(y)); } void NormalizeSelf() { *this /= Norm(); } Point Normalize() { Point res(*this); res.NormalizeSelf(); return res; } LD Dist(const Point& a) const { return (*this - a).Norm(); } LD Angle() const { return atan2(y, x); } void RotateSelf(LD angle) { LD c = cos(angle); LD s = sin(angle); LD nx = x * c - y * s; LD ny = y * c + x * s; y = ny; x = nx; } Point Rotate(LD angle) const { Point res(*this); res.RotateSelf(angle); return res; } static bool LexCmp(const Point& a, const Point& b) { if (abs(a.x - b.x) > kEps) { return a.x < b.x; } return a.y < b.y; } LD SqNorm() { return x * x + y * y; } friend ostream& operator<<(ostream& out, Point m); }; ostream& operator<<(ostream& out, Point p) { out << "(" << p.x << ", " << p.y << ")"; return out; } struct Circle { Point center; LD r; Circle(LD x, LD y, LD rad) { center = Point(x, y); r = rad; } Circle(const Point& a, LD rad) : center(a), r(rad) {} LD Area() const { return kPi * Sq(r); } LD Perimeter() const { return 2 * kPi * r; } LD Diameter() const { return 2 * r; } Point RotateRightMost(LD ang) const { return center + Point{r * cos(ang), r * sin(ang)}; } bool operator==(const Circle& c) const { return center == c.center && abs(r - c.r) < kEps; } }; struct Line { Point p[2]; bool is_seg; Line(Point a, Point b, bool is_seg_ = false) { p[0] = a; p[1] = b; is_seg = is_seg_; } Line() { } Point& operator[](int a) { return p[a]; } Point NormalVector() { Point perp = p[1] - p[0]; perp.RotateSelf(kPi / 2); perp.NormalizeSelf(); return perp; } // (A, B, C) such that A^2 + B^2 = 1, (A, B) > (0, 0) vector<LD> LineEqNormLD() { // seems ok LD A = p[1].y - p[0].y; LD B = p[0].x - p[1].x; LD C = -(A * p[0].x + B * p[0].y); assert(abs(A * p[1].x + B * p[1].y + C) < kEps); LD norm = sqrt(Sq(A) + Sq(B)); vector<LD> res{A, B, C}; for (auto& x : res) { x /= norm; } if (A < -kEps || (abs(A) < kEps && B < -kEps)) { for (auto& x : res) { x *= -1; } } return res; } // assumes that coordinates are integers! 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; } }; struct Utils { // 0, 1, 2 or 3 pts. In case of 3 pts it means they are equal static vector<Point> InterCircleCircle(Circle a, Circle b) { if (a.r + kEps < b.r) { swap(a, b); } if (a == b) { return vector<Point>{a.RotateRightMost(0), a.RotateRightMost(2 * kPi / 3), a.RotateRightMost(4 * kPi / 3)}; } Point diff = b.center - a.center; LD dis = diff.Norm(); LD ang = diff.Angle(); LD longest = max(max(a.r, b.r), dis); LD per = a.r + b.r + dis; if (2 * longest > per + kEps) { return vector<Point>(); } if (abs(2 * longest - per) < 2 * kEps) { return vector<Point>{a.RotateRightMost(ang)}; } LD ang_dev = acos((Sq(a.r) + Sq(dis) - Sq(b.r)) / (2 * a.r * dis)); return vector<Point>{a.RotateRightMost(ang - ang_dev), a.RotateRightMost(ang + ang_dev)}; } static vector<Point> InterLineLine(Line& a, Line& b) { // working fine Point vec_a = a[1] - a[0]; Point vec_b1 = b[1] - a[0]; Point vec_b0 = b[0] - a[0]; LD tr_area = vec_b1.CrossProd(vec_b0); LD quad_area = vec_b1.CrossProd(vec_a) + vec_a.CrossProd(vec_b0); if (abs(quad_area) < kEps) { // parallel or coinciding if (PtBelongToLine(b, a[0])) { return {a[0], a[1]}; } else { return {}; } } return {a[0] + vec_a * (tr_area / quad_area)}; } static Point ProjPointToLine(Point p, Line l) { ///Tested Point diff = l[1] - l[0]; return l[0] + diff * (diff.DotProd(p - l[0]) / diff.DotProd(diff)); } static Point ReflectPtWRTLine(Point p, Line l) { Point proj = ProjPointToLine(p, l); return proj * 2 - p; } static vector<Point> InterCircleLine(Circle c, Line l) { /// Tested here: http://codeforces.com/gym/100554/submission/10197624 Point proj = ProjPointToLine(c.center, l); LD dis_proj = c.center.Dist(proj); if (dis_proj > c.r + kEps) { return vector<Point>(); } LD a = sqrt(max((LD)0, Sq(c.r) - Sq(dis_proj))); Point dir = l[1] - l[0]; LD dir_norm = dir.Norm(); vector<Point> cands{proj + dir * (a / dir_norm), proj - dir * (a / dir_norm)}; if (cands[0].Dist(cands[1]) < kEps) { return vector<Point>{proj}; } return cands; } static bool PtBelongToLine(Line l, Point p) { return abs(l[0].CrossProd(l[1], p)) < kEps; } static bool PtBelongToSeg(Line l, Point p) { // seems ok return abs(p.Dist(l[0]) + p.Dist(l[1]) - l[0].Dist(l[1])) < kEps; } static vector<Point> InterCircleSeg(Circle c, Line l) { //seems ok vector<Point> from_line = InterCircleLine(c, l); vector<Point> res; for (auto p : from_line) { if (PtBelongToSeg(l, p)) { res.PB(p); } } return res; } static vector<Point> TangencyPtsToCircle(Circle c, Point p) { // seems ok LD d = c.center.Dist(p); if (d < c.r - kEps) { return {}; } if (d < c.r + kEps) { return {p}; } LD from_cent = (p - c.center).Angle(); LD ang_dev = acos(c.r / d); return {c.RotateRightMost(from_cent - ang_dev), c.RotateRightMost(from_cent + ang_dev)}; } // outer and inner tangents tested only locally (however I believe that rigorously) static vector<Line> OuterTangents(Circle c1, Circle c2) { if (c1 == c2) { return {}; } // is it surely best choice? if (c1.r < c2.r) { swap(c1, c2); } if (c2.r + c1.center.Dist(c2.center) < c1.r - kEps) { return {}; } if (abs(c1.r - c2.r) < kEps) { Point diff = c2.center - c1.center; Point R = diff.Rotate(kPi / 2) * (c1.r / diff.Norm()); return {{c1.center + R, c2.center + R}, {c1.center - R, c2.center - R}}; } Point I = c1.center + (c2.center - c1.center) * (c1.r / (c1.r - c2.r)); if (c2.r + c1.center.Dist(c2.center) < c1.r + kEps) { return {{I, I + (c2.center - c1.center).Rotate(kPi / 2)}}; } vector<Point> to1 = TangencyPtsToCircle(c1, I); vector<Point> to2 = TangencyPtsToCircle(c2, I); vector<Line> res{{to1[0], to2[0]}, {to1[1], to2[1]}}; assert(Utils::PtBelongToLine(res[0], I)); assert(Utils::PtBelongToLine(res[1], I)); return res; } // unfortunately big part of code is same as in previous function // can be joined when putting appropriate signs in few places // however those ifs differ a bit hence it may not be good idea // to necessarily join them static vector<Line> InnerTangents(Circle c1, Circle c2) { if (c1 == c2) { return {}; } // this time surely best choice if (c1.r < c2.r) { swap(c1, c2); } LD d = c1.center.Dist(c2.center); if (d < c1.r + c2.r - kEps) { return {}; } Point I = c1.center + (c2.center - c1.center) * (c1.r / (c1.r + c2.r)); if (d < c1.r + c2.r + kEps) { return {{I, I + (c2.center - c1.center).Rotate(kPi / 2)}}; } vector<Point> to1 = TangencyPtsToCircle(c1, I); vector<Point> to2 = TangencyPtsToCircle(c2, I); vector<Line> res{{to1[0], to2[0]}, {to1[1], to2[1]}}; assert(Utils::PtBelongToLine(res[0], I)); assert(Utils::PtBelongToLine(res[1], I)); return res; } static bool AreParallel(Line l1, Line l2) { // seems ok return abs(l1[0].CrossProd(l2[0], l1[1]) - l1[0].CrossProd(l2[1], l1[1])) < kEps; } // returns a vector of points such that their convex hull is intersection of those segments // SZ(res) == 0 => empty intersection, SZ(res) == 1 => intersection is a point, SZ(res) == 2 => intersection is a segment static vector<Point> InterSegs(Line l1, Line l2) { // seems ok if (!Point::LexCmp(l1[0], l1[1])) { swap(l1[0], l1[1]); } if (!Point::LexCmp(l2[0], l2[1])) { swap(l2[0], l2[1]); } if (AreParallel(l1, l2)) { if (!PtBelongToLine(l1, l2[0])) { return vector<Point>(); } vector<Point> ends(2); for (int tr = 0; tr < 2; tr++) { if (Point::LexCmp(l1[tr], l2[tr]) ^ tr) { ends[tr] = l2[tr]; } else { ends[tr] = l1[tr]; } } if ((ends[1] - ends[0]).IsZero()) { ends.pop_back(); } if (SZ(ends) == 2 && Point::LexCmp(ends[1], ends[0])) { return vector<Point>(); } return ends; } else { vector<Point> p = InterLineLine(l1, l2); if (PtBelongToSeg(l1, p[0]) && PtBelongToSeg(l2, p[0])) { return p; } return vector<Point>(); } } static LD Angle(Point P, Point Q, Point R) { // angle PQR LD ang2 = (P - Q).Angle(); LD ang1 = (R - Q).Angle(); LD ans = ang1 - ang2; if (ans < kEps) { ans += 2 * kPi; } return ans; } // tested here: http://codeforces.com/contest/600/submission/14961583 // DON'T change anything as this will lead to precision errors // don't know why, but this is the only version which works precisely even for very mean cases static LD DiskInterArea(Circle c1, Circle c2) { // tested here: http://opentrains.snarknews.info/~ejudge/team.cgi?contest_id=006254 problem I if (c1.r < c2.r) { swap(c1, c2); } LD d = c1.center.Dist(c2.center); if (c1.r + c2.r < d + kEps) { return 0; } if (c1.r - c2.r > d - kEps) { return kPi * Sq(c2.r); } LD alfa = acos((Sq(d) + Sq(c1.r) - Sq(c2.r)) / (2 * d * c1.r)); LD beta = acos((Sq(d) + Sq(c2.r) - Sq(c1.r)) / (2 * d * c2.r)); return alfa * Sq(c1.r) + beta * Sq(c2.r) - sin(2 * alfa) * Sq(c1.r) / 2 - sin(2 * beta) * Sq(c2.r) / 2; } static Line RadAxis(Circle c1, Circle c2) { LD d = c1.center.Dist(c2.center); LD a = (Sq(c1.r) - Sq(c2.r) + Sq(d)) / (2 * d); Point Q = c1.center + (c2.center - c1.center) * (a / d); Point R = Q + (c2.center - c1.center).Rotate(kPi / 2); return Line(Q, R); } }; struct Polygon { vector<Point> pts; Polygon(vector<Point> pts_) : pts(pts_) {} Polygon() : Polygon(vector<Point>()) {} void Add(Point p) { pts.push_back(p); } // positive for counterclockwise double Area() { double area = 0; for (int i = 0; i < SZ(pts); i++) { area += pts[i].CrossProd(pts[(i + 1) % SZ(pts)]); } area /= 2; return area; } void OrientCounterclockwise() { if (Area() < 0) { reverse(pts.begin(), pts.end()); } } int next(int a) { if (a + 1 < SZ(pts)) { return a + 1; } return 0; } pair<int, int> FurthestPair() { // tested here: http://codeforces.com/contest/333/submission/11058065 MakeConvexHull(); OrientCounterclockwise(); int furth = 1; pair<int, int> best_pair = make_pair(0, 0); double best_dis = 0; for (int i = 0; i < SZ(pts); i++) { Point side = pts[next(i)] - pts[i]; while (side.CrossProd(pts[furth] - pts[i]) < side.CrossProd(pts[next(furth)] - pts[i])) { furth = next(furth); } vector<int> vec{i, next(i)}; for (auto ind : vec) { if (pts[ind].Dist(pts[furth]) > best_dis) { best_pair = make_pair(ind, furth); best_dis = pts[ind].Dist(pts[furth]); } } cerr<<"Furthest from: "<<pts[i]<<"-"<<pts[next(i)]<<" is "<<pts[furth]<<endl; } return best_pair; } // for square 34 // 12 holds one_way_hull = {{1,3,4},{1,2,4}} // resulting polygon is counterclockwise {1, 2, 4, 3} vector<vector<Point>> MakeConvexHull() { // tested everywhere http://codeforces.com/contest/333/submission/11058065 vector<vector<Point>> one_way_hull(2); sort(pts.begin(), pts.end(), Point::LexCmp); for (int dir = -1; dir <= 1; dir += 2) { int hull_num = (dir + 1) / 2; auto& H = one_way_hull[hull_num]; one_way_hull[hull_num].push_back(pts[0]); if (SZ(pts) > 1) { H.push_back(pts[1]); } for (int i = 2; i < SZ(pts); i++) { while (SZ(H) >= 2 && dir * (pts[i] - H[SZ(H) - 2]).CrossProd(H.back() - H[SZ(H) - 2]) > -kEps) { H.pop_back(); } H.push_back(pts[i]); } if (SZ(H) > 1 && (H[0] - H.back()).IsZero()) { H.pop_back(); } } pts.clear(); for (auto p : one_way_hull[1]) { pts.push_back(p); } for (int i = SZ(one_way_hull[0]) - 2; i >= 1; i--) { pts.push_back(one_way_hull[0][i]); } return one_way_hull; } // without sides vector<vector<bool>> InsideDiagonalsMatrix() { // tested here: http://codeforces.com/contest/438/submission/11063385 int n = pts.size(); vector<vector<bool>> res(n, vector<bool>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Line diag(pts[i], pts[j]); if (i == j || abs(i - j) == 1 || abs(i - j) == n - 1) { continue; } res[i][j] = 1; for (int k = 0; k < n; k++) { int kk = next(k); Line side(pts[k], pts[kk]); if (k == i || k == j || kk == i || kk == j) { continue; } vector<Point> inter = Utils::InterSegs(diag, side); if (SZ(inter)) { res[i][j] = 0; } } int act = next(i); LD areas[2] = {0, 0}; int passed_j = 0; while (act != i) { passed_j |= (act == j); areas[passed_j] += pts[i].CrossProd(pts[act], pts[next(act)]); act = next(act); } if (areas[0] * areas[1] < kEps) { res[i][j] = 0; } } } return res; } // P needs to be strictly outside polygon // polygon needs to be STRICTLY convex and counterclockwise oriented (as MakeConvexHull does) // returns {L, R} so that PL, PR are tangents and PL is on left vector<Point> Tangents(Point p) { // tested here: https://icpc.kattis.com/problems/spin (1169964) vector<Point> res; REP (tr, 2) { auto GrThan = [&](int fir, int sec) { // fir on sec's left return p.CrossProd(pts[sec], pts[fir]) > kEps; }; bool up = false; int cr = 0; if (SZ(pts) >= 2) { cr = p.CrossProd(pts[0], pts[1]); } if (abs(cr) < kEps && SZ(pts) >= 3) { cr = p.CrossProd(pts[0], pts[2]); } up = (cr > 0); VI bd{1, SZ(pts) - 1}; int faj = 0; while (bd[0] + 6 <= bd[1]) { // better don't replace with smaller constants VI h(2); REP (hh, 2) { h[hh] = (bd[0] + bd[1] + bd[hh]) / 3; } if (!GrThan(h[up ^ tr], 0) ^ tr) { bd[up ^ tr] = h[up ^ tr]; } else { int gr = GrThan(h[0], h[1]); bd[gr ^ tr] = h[gr ^ tr]; } } FOR (i, bd[0], bd[1]) { if (GrThan(i, faj) ^ tr) { faj = i; } } res.PB(pts[faj]); } return res; } }; struct ConvexPolHalves { // tested here: https://icpc.kattis.com/problems/spin (1169964) vector<vector<Point>> chains; // initialized by MakeConvexHull bool BelongTo(Point p) { // including borders if (SZ(chains[0]) == 1) { return (chains[0][0] - p).IsZero(); } if (p.x + kEps < chains[0][0].x || p.x - kEps > chains[0].back().x) { return false; } REP (tr, 2) { int kl = 0, kp = SZ(chains[tr]) - 2, faj = 0; while (kl <= kp) { int aktc = (kl + kp) / 2; if (chains[tr][aktc].x < p.x + kEps) { kl = aktc + 1; faj = aktc; } else { kp = aktc - 1; } } Point fir = chains[tr][faj], sec = chains[tr][faj + 1]; if (abs(fir.x - sec.x) < kEps) { if (tr == 0) { if (sec.y + kEps < p.y) { return false; } } else { if (fir.y - kEps > p.y) { return false; } } } else { LD cr = fir.CrossProd(sec, p); if (abs(cr) < kEps) { return true; } if ((cr > 0) ^ tr) { return false; } } } return true; } }; // CLIP START bool InUpper(Point a) { if (abs(a.y) > kEps) { return a.y > 0; } return a.x > 0; } bool angle_cmp(const Point a, const Point 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(Point a, Point b, Point c, Point d) { return (d - c).CrossProd(a - c) / (d - c).CrossProd(a - b); } struct ClipLine { // valid side is on left ClipLine(Point A, Point B) : al(A), bl(B), a(A), b(B) {}; Point al,bl; // original line points mutable Point a,b; // actual intersection points Point dir() const { return bl - al; } bool operator<(const ClipLine& l) const { return angle_cmp(dir(),l.dir()); } Point cross(const ClipLine& l) { return al + (bl - al) * ::cross(al, bl, l.al, l.bl); } bool left(Point p) { return (bl - al).CrossProd(p - al) > 0; } }; set<VI> put; struct Clip { Clip(LD r) : area(4*r*r) { Point 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)}; } void insert(Line l) { VI abc = l.LineEqNormInt(); if (put.count(abc)) { return; } put.insert(abc); insert(ClipLine(l[0], l[1])); } 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); } } //assert(l.dir().SqNorm()>1e-13); } 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; }; // CLIP ENDS // CENTERS BEGIN Point Bary(Point A, Point B, Point C, LD a, LD b, LD c) { return (A * a + B * b + C * c) / (a + b + c); } Point Centroid(Point A, Point B, Point C) { return Bary(A, B, C, 1, 1, 1); } Point Circumcenter(Point A, Point B, Point C) { LD a = (B - C).SqNorm(), b = (C - A).SqNorm(), c = (A - B).SqNorm(); return Bary(A, B, C, a * (b + c - a), b * (c + a - b), c * (a + b - c)); } Point Incenter(Point A, Point B, Point C) { return Bary(A, B, C, (B - C).Norm(), (A - C).Norm(), (A - B).Norm()); } Point Orthocenter(Point A, Point B, Point C) { LD a = (B - C).SqNorm(), b = (C - A).SqNorm(), c = (A - B).SqNorm(); return Bary(A, B, C, (a+b-c)*(c+a-b), (b+c-a)*(a+b-c), (c+a-b)*(b+c-a)); } Point Excenter(Point A, Point B, Point C) { // opposite to A LD a = (B - C).Norm(), b = (A - C).Norm(), c = (A - B).Norm(); return Bary(A, B, C, -a, b, c); } // const LD kEps = 1e-9; // const LD kPi = 2 * acos(0); struct PointInt { int x, y; PointInt operator-(PointInt p) { return {x - p.x, y - p.y}; } PointInt operator+(PointInt p) { return {x + p.x, y + p.y}; } // Point operator*(LD f) { return {x * f, y * f}; } // bool operator<(const Point& oth) const { // if (abs(x - oth.x) > kEps) { return x < oth.x; } // if (abs(y - oth.y) > kEps) { return y < oth.y; } // return false; // } // LD Angle() { // return atan2(y, x); // } }; // LD Normalize(LD ang) { // if (ang < -kPi) { // ang += 2 * kPi; // } // if (ang > kPi) { // ang -= 2 * kPi; // } // return ang; // } int CrossProd(PointInt A, PointInt B, PointInt C) { B = B - A; C = C - A; return B.x * C.y - B.y * C.x; } // vector<Point> InterLineLine(Point A, Point B, Point P, Point Q) { // LD tr_area = CrossProd(A, P, Q); // LD quad_area = CrossProd(A, P, B) + CrossProd(A, B, Q); // if (abs(quad_area) < 1e-9) { // return {}; // } // return {A + (B - A) * (tr_area / quad_area)}; // } const int N = 222; int n; vector<PointInt> segs[N]; // bool CheckPoint(Point p) { // int bil = 0; // vector<pair<LD, int>> evs; // RE (i, n) { // Point D1 = segs[i][0] - p; // Point D2 = segs[i][1] - p; // LD ang1 = D1.Angle(); // LD ang2 = D2.Angle(); // LD diff = Normalize(ang2 - ang1); // if (diff < 0) { // swap(D1, D2); // swap(ang1, ang2); // } // LD from = Normalize(ang2 - kPi / 2); // LD to = Normalize(ang1 + kPi / 2); // evs.PB({from, 1}); // evs.PB({to, -1}); // if (from > to) { // bil++; // } // } // sort(ALL(evs)); // if (bil == 0) { return false; } // for (auto ev : evs) { // bil += ev.nd; // if (bil == 0) { return false; } // } // return true; // } Point PFromPI(PointInt P) { debug(P.x, P.y); return {(LD)P.x, (LD)P.y}; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); cerr << fixed << setprecision(15); cin.tie(0); //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC; cin>>n; vector<Point> pts; RE (i, n) { int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2; segs[i].PB(PointInt{x1, y1}); segs[i].PB(PointInt{x2, y2}); //pts.PB(Point{LD(x1), LD(y1)}); //pts.PB(Point{LD(x2), LD(y2)}); } //map<Point, int> ids; Clip clip(1e4); set<VI> put_lines; RE (i, n) { RE (j, i - 1) { REP (ti, 2) { REP (tj, 2) { bool can_all_atmost0 = 1, can_all_atleast0 = 1; PointInt P = segs[i][ti], Q = segs[j][tj]; RE (k, n) { if (k == i || k == j) { continue; } int cr0 = CrossProd(P, Q, segs[k][0]); int cr1 = CrossProd(P, Q, segs[k][1]); can_all_atleast0 &= (cr0 >= 0 || cr1 >= 0); can_all_atmost0 &= (cr0 <= 0 || cr1 <= 0); } if (can_all_atleast0 && can_all_atmost0) { cout<<(LD)0<<endl; return 0; } if (can_all_atleast0) { debug(">=0"); Line l = Line{PFromPI(P), PFromPI(Q)}; clip.insert(l); } if (can_all_atmost0) { debug("<=0"); clip.insert(Line{PFromPI(Q), PFromPI(P)}); } } } } } clip.recalculate(); cout<<clip.area<<endl; return 0; } |