src/jiangly/math/11B-Convex-Hull.hpp
Code
/** 静态凸包(with. Point,新版)
* 2024-04-06: https://qoj.ac/submission/379920)
**/
struct Point {
i64 x;
i64 y;
Point() : x{0}, y{0} {}
Point(i64 x_, i64 y_) : x{x_}, y{y_} {}
};
i64 dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
i64 cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
Point operator+(Point a, Point b) {
return Point(a.x + b.x, a.y + b.y);
}
Point operator-(Point a, Point b) {
return Point(a.x - b.x, a.y - b.y);
}
auto getHull(std::vector<Point> p) {
std::sort(p.begin(), p.end(),
[&](auto a, auto b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
});
std::vector<Point> hi, lo;
for (auto p : p) {
while (hi.size() > 1 && cross(hi.back() - hi[hi.size() - 2], p - hi.back()) >= 0) {
hi.pop_back();
}
while (!hi.empty() && hi.back().x == p.x) {
hi.pop_back();
}
hi.push_back(p);
while (lo.size() > 1 && cross(lo.back() - lo[lo.size() - 2], p - lo.back()) <= 0) {
lo.pop_back();
}
if (lo.empty() || lo.back().x < p.x) {
lo.push_back(p);
}
}
return std::make_pair(hi, lo);
}
const double inf = INFINITY;
#line 1 "src/jiangly/math/11B-Convex-Hull.hpp"
/** 静态凸包(with. Point,新版)
* 2024-04-06: https://qoj.ac/submission/379920)
**/
struct Point {
i64 x;
i64 y;
Point() : x{0}, y{0} {}
Point(i64 x_, i64 y_) : x{x_}, y{y_} {}
};
i64 dot(Point a, Point b) {
return a.x * b.x + a.y * b.y;
}
i64 cross(Point a, Point b) {
return a.x * b.y - a.y * b.x;
}
Point operator+(Point a, Point b) {
return Point(a.x + b.x, a.y + b.y);
}
Point operator-(Point a, Point b) {
return Point(a.x - b.x, a.y - b.y);
}
auto getHull(std::vector<Point> p) {
std::sort(p.begin(), p.end(),
[&](auto a, auto b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
});
std::vector<Point> hi, lo;
for (auto p : p) {
while (hi.size() > 1 && cross(hi.back() - hi[hi.size() - 2], p - hi.back()) >= 0) {
hi.pop_back();
}
while (!hi.empty() && hi.back().x == p.x) {
hi.pop_back();
}
hi.push_back(p);
while (lo.size() > 1 && cross(lo.back() - lo[lo.size() - 2], p - lo.back()) <= 0) {
lo.pop_back();
}
if (lo.empty() || lo.back().x < p.x) {
lo.push_back(p);
}
}
return std::make_pair(hi, lo);
}
const double inf = INFINITY;
Back to top page