// This is free and unencumbered software released into the public domain. // Anyone is free to copy, modify, publish, use, compile, sell, or // distribute this software, either in source code form or as a compiled // binary, for any purpose, commercial or non-commercial, and by any // means. // In jurisdictions that recognize copyright laws, the author or authors // of this software dedicate any and all copyright interest in the // software to the public domain. We make this dedication for the benefit // of the public at large and to the detriment of our heirs and // successors. We intend this dedication to be an overt act of // relinquishment in perpetuity of all present and future rights to this // software under copyright law. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // For more information, please refer to /****************/ /* template.hpp */ /****************/ #include #include #include #include #include #include using std::cerr; using std::cout; using std::endl; using std::max; using std::min; using std::swap; struct BoolName : std::numpunct { std::string t, f; BoolName(std::string t, std::string f) : t(t), f(f) {} std::string do_truename() const { return t; } std::string do_falsename() const { return f; } }; void setBoolName(std::string t, std::string f) { cout.imbue(std::locale(cout.getloc(), new BoolName(t, f))); } struct Initializer { Initializer() { cout << std::fixed << std::setprecision(15) << std::boolalpha; setBoolName("Yes", "No"); } } initializer; struct Input { bool eof; Input() : eof(false) {} operator char() { char v; while (!(this->eof = (std::scanf("%c", &v) != 1)) && std::isspace(v)) { } return v; } operator int() { int v; this->eof = (std::scanf("%d", &v) != 1); return v; } operator long() { long v; this->eof = (std::scanf("%ld", &v) != 1); return v; } operator long long() { long long v; this->eof = (std::scanf("%lld", &v) != 1); return v; } operator unsigned int() { unsigned int v; this->eof = (std::scanf("%u", &v) != 1); return v; } operator unsigned long() { unsigned long v; this->eof = (std::scanf("%lu", &v) != 1); return v; } operator unsigned long long() { unsigned long long v; this->eof = (std::scanf("%llu", &v) != 1); return v; } operator double() { double v; this->eof = (std::scanf("%lf", &v) != 1); return v; } operator long double() { long double v; this->eof = (std::scanf("%Lf", &v) != 1); return v; } void ignore() const { getchar(); } } in; template T abs(T a) { return a >= 0 ? a : -a; } template bool chmin(T &a, const S &b) { return a > b ? a = b, true : false; } template bool chmax(T &a, const S &b) { return a < b ? a = b, true : false; } template std::function cast() { return [](const T &t) { return static_cast(t); }; } template T copy(const T &a) { return T(a); } class ZeroPadding { public: ZeroPadding(int n) : n(n) {} int n; }; std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) { os << std::setw(z.n) << std::setfill('0'); return os; } template constexpr T inf() { return std::numeric_limits::max() / 2 - 1; } /******************/ /* arithmetic.hpp */ /******************/ template class Addition { public: template T operator+(const V &v) const { return T(static_cast(*this)) += v; } T operator++() { return static_cast(*this) += 1; } }; template class Subtraction { public: template T operator-(const V &v) const { return T(static_cast(*this)) -= v; } }; template class Multiplication { public: template T operator*(const V &v) const { return T(static_cast(*this)) *= v; } }; template class Division { public: template T operator/(const V &v) const { return T(static_cast(*this)) /= v; } }; template class Modulus { public: template T operator%(const V &v) const { return T(static_cast(*this)) %= v; } }; template class IndivisibleArithmetic : public Addition, public Subtraction, public Multiplication {}; template class Arithmetic : public IndivisibleArithmetic, public Division {}; /*********************/ /* bit_operation.hpp */ /*********************/ template int least_bit(T n) { static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size"); if (sizeof(T) == 4) { return __builtin_ffs(n) - 1; } if (sizeof(T) == 8) { return __builtin_ffsll(n) - 1; } } // n must be greater than 0. template int least_bit_fast(T n) { static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size"); if (sizeof(T) == 4) { return __builtin_ctz(n); } if (sizeof(T) == 8) { return __builtin_ctzll(n); } } template int most_bit(T n) { static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size"); if (sizeof(T) == 4) { return n ? 31 - __builtin_clz(n) : -1; } if (sizeof(T) == 8) { return n ? 63 - __builtin_clzll(n) : -1; } } template int count_bit(T n) { static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size"); if (sizeof(T) == 4) { return __builtin_popcount(n); } if (sizeof(T) == 8) { return __builtin_popcountll(n); } } template int bit_parity(T n) { static_assert(sizeof(T) == 4 || sizeof(T) == 8, "unsupported size"); if (sizeof(T) == 4) { return __builtin_parity(n); } if (sizeof(T) == 8) { return __builtin_parityll(n); } } /*************/ /* tuple.hpp */ /*************/ #include template class Tuple : public std::tuple { public: Tuple(Input &in) : std::tuple() { (void)in; } }; template class Tuple : public std::tuple { public: Tuple() : std::tuple() {} Tuple(T t, S... s) : std::tuple(t, s...) {} Tuple(const std::tuple &t) : std::tuple(t) {} Tuple(Input &in) { auto a = std::tuple(in); std::tuple b = Tuple(in); std::tuple c = std::tuple_cat(a, b); *this = c; } template auto &get() { return std::get(*this); } template const auto &get() const { return std::get(*this); } }; template Tuple makeTuple(const T &... args) { return Tuple(args...); } namespace std { template class tuple_size> : public std::integral_constant {}; template class tuple_element> { public: using type = tuple_element_t>; }; } // namespace std /*****************/ /* container.hpp */ /*****************/ #include template class Container : public T { private: using S = typename T::value_type; using Itr = typename T::iterator; public: Container() : T() {} Container(int n) : T(n) {} Container(int n, S s) : T(n, s) {} template Container(Itr first, Itr last) : T(first, last) {} Container(const std::initializer_list &v) : T(v) {} Container(int n, Input &in) { std::vector v(n); for (auto &i : v) { i = in; } *this = Container(v.begin(), v.end()); } S max() const { return *std::max_element(this->begin(), this->end()); } template auto max(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::max_element(res.begin(), res.end())->second; } S min() const { return *std::min_element(this->begin(), this->end()); } Tuple minmax() const { auto itrs = std::minmax_element(this->begin(), this->end()); return Tuple(*itrs.first, *itrs.second); } template auto min(Function func) const { std::vector> res; for (const auto &i : *this) { res.emplace_back(func(i), i); } return std::min_element(res.begin(), res.end())->second; } int argmax() const { return std::distance(this->begin(), std::max_element(this->begin(), this->end())); } int argmin() const { return std::distance(this->begin(), std::min_element(this->begin(), this->end())); } int find(const S &a) const { return std::distance(this->begin(), std::find(this->begin(), this->end(), a)); } bool contains(const S &a) const { return std::find(this->begin(), this->end(), a) != this->end(); } int size() const { return T::size(); } std::pair equal_range(const S &a) { return std::equal_range(this->begin(), this->end(), a); } template bool all_of(Function func) const { return std::all_of(this->begin(), this->end(), func); } template bool any_of(Function func) const { return std::any_of(this->begin(), this->end(), func); } template bool none_of(Function func) const { return std::none_of(this->begin(), this->end(), func); } int count(const S &s) const { return std::count(this->begin(), this->end(), s); } bool is_sorted() const { return std::is_sorted(this->begin(), this->end()); } void output(std::string sep = "\n", std::string end = "\n") const { bool first = true; for (const auto &i : *this) { if (!first) { cout << sep; } first = false; cout << i; } cout << end; } }; /***********/ /* map.hpp */ /***********/ #include template class Map : public Container> { public: Map() : Container>() {} bool contains(const T &a) const { return this->count(a) != 0; } int count(const T &t) const { return std::map::count(t); } }; /***************/ /* ordered.hpp */ /***************/ template class Ordered { public: template bool operator==(const V &v) const { return !(static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v)); } template bool operator!=(const V &v) const { return static_cast(v) < static_cast(*this) || static_cast(*this) < static_cast(v); } template bool operator>(const V &v) const { return static_cast(v) < static_cast(*this); } template bool operator<=(const V &v) const { return !(static_cast(v) < static_cast(*this)); } template bool operator>=(const V &v) const { return !(static_cast(*this) < static_cast(v)); } }; /**************/ /* vector.hpp */ /**************/ #include template class Vector : public Container>, public Ordered> { public: Vector() = default; Vector(const Vector &v) = default; Vector(int n) : Container>(n) {} Vector(int n, T t) : Container>(n, t) {} template Vector(Itr first, Itr last) : Container>(first, last) {} Vector(const std::initializer_list &v) : Container>(v) {} Vector(int n, Input &in) : Container>(n, in) {} Vector &operator+=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] += v[i]; } return *this; } Vector &operator+=(const T &v) { for (auto &i : *this) { i += v; } return *this; } Vector &operator-=(const Vector &v) { if (this->size() < v.size()) { this->resize(v.size()); } for (int i = 0; i < v.size(); ++i) { (*this)[i] -= v[i]; } return *this; } Vector &operator-=(const T &v) { for (auto &i : *this) { i -= v; } return *this; } Vector &operator*=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] *= v[i]; } return *this; } Vector &operator*=(const T &v) { for (auto &i : *this) { i *= v; } return *this; } Vector &operator/=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] /= v[i]; } return *this; } Vector &operator/=(const T &v) { for (auto &i : *this) { i /= v; } return *this; } Vector &operator%=(const Vector &v) { for (int i = 0; i < this->size(); ++i) { (*this)[i] %= v[i]; } return *this; } Vector &operator%=(const T &v) { for (auto &i : *this) { i %= v; } return *this; } Vector operator+(const Vector &v) const { return Vector(*this) += v; } Vector operator+(const T &v) const { return Vector(*this) += v; } Vector operator-(const Vector &v) const { return Vector(*this) -= v; } Vector operator-(const T &v) const { return Vector(*this) -= v; } Vector operator*(const Vector &v) const { return Vector(*this) *= v; } Vector operator*(const T &v) const { return Vector(*this) *= v; } Vector operator/(const Vector &v) const { return Vector(*this) /= v; } Vector operator/(const T &v) const { return Vector(*this) /= v; } Vector operator%(const Vector &v) const { return Vector(*this) %= v; } Vector operator%(const T &v) const { return Vector(*this) %= v; } bool operator<(const Vector &v) const { if (this->size() != v.size()) { return this->size() < v.size(); } for (int i = 0; i < this->size(); ++i) { if ((*this)[i] != v[i]) { return (*this)[i] < v[i]; } } return false; } Vector operator-() const { return *this * -1; } T inner_product(const Vector &v) const { return std::inner_product(this->begin(), this->end(), v.begin(), T(0)); } Vector &partial_sort(int k, bool reverse = false) { if (!reverse) { std::partial_sort(this->begin(), this->begin() + k, this->end()); } else { std::partial_sort(this->begin(), this->begin() + k, this->end(), std::greater()); } return *this; } Vector &sort() { std::sort(this->begin(), this->end()); return *this; } template Vector &sort(Function func) { std::sort(this->begin(), this->end(), func); return *this; } Vector &rsort() { std::sort(this->rbegin(), this->rend()); return *this; } Vector argsort() const { Vector> v; for (int i = 0; i < this->size(); ++i) { v.emplace_back((*this)[i], i); } v.sort(); auto f = [](const Tuple &t) { return t.template get<1>(); }; return v.transform(f); } Vector &nth_element(int n, bool reverse = false) { if (!reverse) { std::nth_element(this->begin(), this->begin() + n, this->end()); } else { std::nth_element(this->begin(), this->begin() + n, this->end(), std::greater()); } return *this; } Vector subvector(int a) const { return Vector(this->begin(), this->begin() + a); } Vector subvector(int a, int b) const { return Vector(this->begin() + a, this->begin() + b); } template auto transform(Function func) const { Vector res; std::transform(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector partial_sum() const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res)); return res; } template Vector partial_sum(Function func) const { Vector res; std::partial_sum(this->begin(), this->end(), std::back_inserter(res), func); return res; } Vector &reverse() { std::reverse(this->begin(), this->end()); return *this; } template int count_if(Function func) const { return std::count_if(this->begin(), this->end(), func); } Vector adjacent_difference() const { Vector res; std::adjacent_difference(this->begin(), this->end(), std::back_inserter(res)); return res; } T lower_bound(T t) const { return std::lower_bound(this->begin(), this->end(), t) - this->begin(); } T upper_bound(T t) const { return std::upper_bound(this->begin(), this->end(), t) - this->begin(); } T accumulate() const { return std::accumulate(this->begin(), this->end(), T()); } template S accumulate(S n, Function func) const { return std::accumulate(this->begin(), this->end(), n, func); } template static Vector makeVector(Int n) { return Vector(n); } template static Vector makeVector(Input &in, Int n) { return Vector(n, in); } template static auto makeVector(Input &in, Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(in, ints...)); } return res; } template static auto makeVector(Int n, Ints... ints) { Vector res; for (int i = 0; i < n; ++i) { res.emplace_back(makeVector(ints...)); } return res; } Vector &unique() { this->erase(std::unique(this->begin(), this->end()), this->end()); return *this; } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } Vector &rotate(int n) { std::rotate(this->begin(), this->begin() + n, this->end()); return *this; } Map countAll() const { Map res; for (const auto &i : *this) { ++res[i]; } return res; } T matmul(const T &a) const { return this->transform([&](const T &i) { return i.inner_product(a); }); } }; template Vector iota(int n, T m = 0) { Vector v(n); std::iota(v.begin(), v.end(), m); return v; } template void read(Vector &t, Vector &s) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); } } template void read(Vector &t, Vector &s, Vector &u) { for (int i = 0; i < t.size(); ++i) { t[i] = T(in); s[i] = S(in); u[i] = U(in); } } template Vector operator+(const T &a, const Vector &b) { return b + a; } template Vector operator-(const T &a, const Vector &b) { return -b + a; } template Vector operator*(const T &a, const Vector &b) { return b * a; } /************/ /* math.hpp */ /************/ #include template constexpr T pi() { return acos(T(-1)); } template T gcd(T t) { return abs(t); } template T gcd(T a, S... s) { a = abs(a); auto b = gcd(s...); if (a == 0 || b == 0) { return max(a, b); } int fa = least_bit_fast(a); int fb = least_bit_fast(b); a >>= fa; b >>= fb; while (a != b) { auto &c = a > b ? a : b; c = abs(a - b); c >>= least_bit_fast(c); } return a << min(fa, fb); } template T gcd(const Vector &v) { T g = abs(v[0]); for (int i = 1; i < int(v.size()); ++i) { g = gcd(g, v[i]); } return g; } template T lcm(T t) { return abs(t); } template T lcm(T t, S... s) { T l = lcm(s...); return abs(t) / gcd(t, l) * l; } template T lcm(const Vector &v) { T l = abs(v[0]); for (int i = 1; i < int(v.size()); ++i) { l = lcm(l, v[i]); } return l; } template T floor(T a, T b) { auto d = std::div(a, b); return d.quot - (d.rem && (a < 0) != (b < 0) ? 1 : 0); } template T ceil(T a, T b) { auto d = std::div(a, b); return d.quot + (d.rem && (a > 0) == (b > 0) ? 1 : 0); } template T round(T a) { return std::round(a); } template T round(T a, T b) { return floor(a + b / 2, b); } template T mod(T a, T b) { T c = a % b; return c < 0 ? c + abs(b) : c; } template T factorial(T n) { return n <= 1 ? 1 : factorial(n - 1) * n; } template Vector factorial_vector(int n) { Vector v(n + 1, 1); for (int i = 1; i <= n; ++i) { v[i] = v[i - 1] * i; } return v; } template T square(T n) { return n * n; } template T cube(T n) { return n * n * n; } template T norm(T x1, T y1, T x2, T y2) { return square(x1 - x2) + square(y1 - y2); } template bool isSquare(T n) { return square(T(sqrt(n))) == n; } template T clamp(T v, T l, T u) { return v < l ? l : v > u ? u : v; } template auto hypot(T a, T b) { return std::hypot(a, b); } template auto pow(T a, T b) { return std::pow(a, b); } template auto log10(T a) { return std::log10(a); } /*********************/ /* geometry/real.hpp */ /*********************/ class Real : public Arithmetic, public Ordered { private: long double val; public: static long double EPS; Real() {} Real(long double val) : val(val) {} Real operator-() const { return -val; } template Real &operator+=(const T &r) { val += static_cast(r); return *this; } template Real &operator-=(const T &r) { val -= static_cast(r); return *this; } template Real &operator*=(const T &r) { val *= static_cast(r); return *this; } template Real &operator/=(const T &r) { val /= static_cast(r); return *this; } template Real operator-(const T &v) const { return Real(*this) -= v; } template Real operator*(const T &v) const { return Real(*this) *= v; } template Real operator/(const T &v) const { return Real(*this) /= v; } template bool operator<(const T r) const { return val < static_cast(r) - EPS; } Real abs() const { return std::abs(val); } Real sqrt() const { return std::sqrt(val); } Real floor() const { return std::floor(val); } operator long double() const { return val; } }; long double Real::EPS = 1e-10; std::ostream &operator<<(std::ostream &os, const Real &a) { os << static_cast(a); return os; } /**********************/ /* geometry/point.hpp */ /**********************/ class Point : public Arithmetic, public Ordered { public: Real x, y; Point() {} Point(const Real &x) : x(x), y(0) {} Point(const Real &x, const Real &y) : x(x), y(y) {} Point(Input &in) : x(in), y(in) {} Point &operator+=(const Point &p) { x += p.x; y += p.y; return *this; } Point &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; } Point &operator*=(const Point &p) { Real xx = x * p.x - y * p.y; y = x * p.y + y * p.x; x = xx; return *this; } Point &operator*=(const Real &r) { x *= r; y *= r; return *this; } Point &operator/=(const Point &p) { Real nrm = p.norm(); Real xx = (x * p.x + y * p.y) / nrm; y = (y * p.x - x * p.y) / nrm; x = xx; return *this; } Point &operator/=(const Real &r) { x /= r; y /= r; return *this; } bool operator<(const Point &p) const { return (x == p.x) ? y < p.y : x < p.x; } Real norm() const { return x * x + y * y; } Real abs() const { return norm().sqrt(); } Point conj() const { return Point(x, -y); } }; Point operator*(const Real &real, const Point &point) { return point * real; } Point operator/(const Real &real, const Point &point) { return point / real; } std::ostream &operator<<(std::ostream &os, const Point &point) { os << point.x << " " << point.y; return os; } /*********************/ /* geometry/line.hpp */ /*********************/ class Line { public: Point a, b; Line() {} Line(const Point &a, const Point &b) : a(a), b(b) {} Line(Input &in) : a(in), b(in) {} bool operator==(const Line &line) const { return ((line.vec() / vec()).y == 0) && (((line.a - a) / vec()).y == 0); } Point vec() const { return b - a; } }; std::ostream &operator<<(std::ostream &os, const Line &line) { os << line.a << " " << line.b; return os; } /****************************/ /* geometry/cross_point.hpp */ /****************************/ Point crossPoint(const Line &line1, const Line &line2) { return line1.a + line1.vec() * ((line2.a - line1.a) / line2.vec()).y / (line1.vec() / line2.vec()).y; } /************************/ /* geometry/segment.hpp */ /************************/ class Segment : public Line, public Ordered { public: Segment() {} Segment(const Point &a, const Point &b) : Line(a, b) {} Segment(Input &in) : Line(in) {} bool operator<(const Segment &segment) const { return a == segment.a ? b < segment.b : a < segment.a; } Real area() const { return (this->a.x * this->b.y - this->a.y * this->b.x) / 2; } }; /********************/ /* geometry/ccw.hpp */ /********************/ enum CCW { LEFT = 1, RIGHT = 2, BACK = 4, FRONT = 8, ON = 16 }; int ccw(const Point &a, const Point &b, const Point &c) { Point p = (c - a) / (b - a); if (p.y > 0) { return LEFT; } if (p.y < 0) { return RIGHT; } if (p.x < 0) { return BACK; } if (p.x > 1) { return FRONT; } return ON; } int ccw(const Segment &segment, const Point &point) { return ccw(segment.a, segment.b, point); } int ccw(const Line &line, const Point &point) { int res = ccw(line.a, line.b, point); if (res == BACK || res == FRONT) { res = ON; } return res; } /**************************/ /* geometry/intersect.hpp */ /**************************/ template bool intersect(const Line &line1, const Line &line2) { if (strict) { return (line1.vec() / line2.vec()).y != 0; } return ((line1.vec() / line2.vec()).y != 0) || (line1 == line2); } template bool intersect(const Line &line, const Segment &segment) { Point p1 = (segment.a - line.a) / line.vec(); Point p2 = (segment.b - line.a) / line.vec(); if (strict) { return p1.y * p2.y < 0; } return p1.y * p2.y <= 0; } template bool intersect(const Segment &segment, const Line &line) { return intersect(line, segment); } template bool intersect(const Segment &segment1, const Segment &segment2) { int ccw1 = ccw(segment1, segment2.a) | ccw(segment1, segment2.b); int ccw2 = ccw(segment2, segment1.a) | ccw(segment2, segment1.b); if (strict) { return (ccw1 & ccw2) == (LEFT | RIGHT); } return ((ccw1 & ccw2) == (LEFT | RIGHT)) || ((ccw1 | ccw2) & ON); } /************************/ /* geometry/polygon.hpp */ /************************/ class Polygon : public Vector { public: Polygon() {} Polygon(int n) : Vector(n) {} Polygon(int n, Input &in) : Vector(n, in) {} Polygon(const std::initializer_list &p) : Vector(p) {} Polygon(const Vector &p) : Vector(p) {} Vector getSides() const { if (size() <= 1) { return {}; } Vector res; Point pre = back(); for (const auto &point : *this) { res.emplace_back(pre, point); pre = point; } return res; } Vector> getCorners() const { if (size() <= 2) { return {}; } Vector> res; Point pre1 = *(end() - 2), pre2 = back(); for (const auto &point : *this) { res.emplace_back(Vector({pre1, pre2, point})); pre1 = pre2; pre2 = point; } return res; } Point &operator[](int i) { return vector::operator[](mod(i, size())); } const Point &operator[](int i) const { return vector::operator[](mod(i, size())); } template bool cover(const Point &point) const { bool res = false; for (auto &side : getSides()) { if (ccw(side, point) == ON) { return strict ? false : true; } if (side.a.y > side.b.y) { std::swap(side.a, side.b); } if (side.a.y <= point.y && point.y < side.b.y && ((side.b - point) / (side.a - point)).y > 0) { res = !res; } } return res; } Real area() const { auto side = this->getSides(); auto f = [](const Real &r, const Segment &segment) { return r + segment.area(); }; return side.accumulate(0, f); } }; /*******************************/ /* geometry/convex_polygon.hpp */ /*******************************/ template class ConvexPolygon : public Polygon { public: ConvexPolygon() {} ConvexPolygon(int n) : Polygon(n) {} ConvexPolygon(Vector points) { int flag = ~(strict ? LEFT : LEFT | FRONT); points.sort(); for (int i = 0; i < points.size(); emplace_back(points[i++])) { while (size() > 1 && ccw(*(end() - 2), back(), points[i]) & flag) { pop_back(); } } for (int i = points.size() - 2, r = size(); i >= 0; emplace_back(points[i--])) { while (size() > r && ccw(*(end() - 2), back(), points[i]) & flag) { pop_back(); } } pop_back(); } Real diameter() { auto sides = getSides(); int i = min_element(sides.begin(), sides.end()) - sides.begin(); int j = max_element(sides.begin(), sides.end()) - sides.begin(); sides.insert(sides.end(), sides.begin(), sides.end()); Real res = 0; for (int k = 0; k < 2 * size(); ++k) { if ((sides[i].vec() / sides[j].vec()).y >= 0) { ++i; } else { ++j; } chmax(res, (sides[i].a - sides[j].a).abs()); } return res; } ConvexPolygon cut(const Line &line) { ConvexPolygon res; auto sides = getSides(); for (const auto &side : sides) { if (ccw(line, side.a) != RIGHT) { res.push_back(side.a); } if (intersect(line, side)) { res.push_back(crossPoint(line, side)); } } return res; } }; /************/ /* main.cpp */ /************/ int main() { setBoolName("YES", "NO"); Vector p(5, in); cout << (ConvexPolygon<>(p).size() == 5) << endl; }