# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
152469 | Mahotsukai | Organizing the Best Squad (FXCUP4_squad) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
struct point{
ll x, y;
point(pll p): x(p.first), y(p.second){}
point(ll x = 0, ll y = 0): x(x), y(y){}
bool operator<(const point &other) const{
return x < other.x || (x == other.x && y < other.y);
}
point operator+(const point &other) const{
return point(x + other.x, y + other.y);
}
point operator+=(const point &other){
return *this = *this + other;
}
point operator-(const point &other) const{
return point(x - other.x, y - other.y);
}
point operator-=(const point &other){
return *this = *this - other;
}
bool operator==(const point &other) const{
return x == other.x && y == other.y;
}
ll operator*(const point &other) const{
return x * other.x + y * other.y;
}
};
istream &operator>>(istream &in, point &p){
cin >> p.x >> p.y;
return in;
}
ostream &operator<<(ostream &out, const point &p){
cout << "(" << p.x << ", " << p.y << ")";
return out;
}
ll ori(const point &p, const point &q, const point &r){
return p.x * q.y + q.x * r.y + r.x * p.y - p.y * q.x - q.y * r.x - r.y * p.x;
}
struct convhull{
int N, R;
vector<point> ch;
convhull(vector<point> arr = vector<point>(), bool sorted = false){
if(!sorted){
sort(all(arr));
}
arr.resize(unique(all(arr)) - arr.begin());
if(arr.size() <= 1){
ch = arr;
N = R = arr.size();
if(N) R --;
return;
}
vector<point> upper;
point p = arr.front(), q = arr.back();
upper.push_back(p);
for(int i = 1; i < arr.size(); i ++){
if(i == arr.size() - 1 || ori(p, arr[i], q) < 0){
while(upper.size() >= 2 && ori(upper[upper.size() - 2], upper[upper.size() - 1], arr[i]) >= 0){
upper.pop_back();
}
upper.push_back(arr[i]);
}
}
ch.push_back(p), ch.push_back(q);
for(int i = upper.size() - 2; i; i --) ch.push_back(upper[i]);
N = ch.size();
R = 1;
}
vector<point> linearize() const{
if(N <= 1) return ch;
int i = 0, j = N - 1;
vector<point> res(N);
int cnt = 0;
while(i <= j){
if(ch[i] < ch[j]) res[cnt ++] = ch[i ++];
else res[cnt ++] = ch[j --];
}
return res;
}
int max_element(ll x, ll y){
point p(x, y);
int l = R, r = N;
while(r - l > 3){
int m1 = (2 * l + r) / 3, m2 = (l + 2 * r) / 3;
p * ch[m1] <= p * ch[m2] ? l = m1 : r = m2;
}
int res = 0;
for(int i = l; i < r; i ++) if(p * ch[res] < p * ch[i]) res = i;
return res;
}
convhull operator+(const convhull &other) const{
if(!N || !other.N) return convhull();
vector<point> temp1(ch), temp2(other.ch);
for(int i = 0; i + 1 < N; i ++) temp1[i] = ch[i + 1] - ch[i];
for(int i = 0; i + 1 < other.N; i ++) temp2[i] = other.ch[i + 1] - other.ch[i];
temp1.back() = ch.front() - ch.back();
temp2.back() = other.ch.front() - other.ch.back();
function<bool(point, point)> cmp = [&](point p, point q){
int typep = 1, typeq = 1;
if(p.x > 0 || !p.x && p.y >= 0) typep = 0;
if(q.x > 0 || !q.x && q.y >= 0) typeq = 0;
if(typep != typeq) return typep < typeq;
return ori(p, q, point(0, 0)) > 0;
};
vector<point> temp(N + other.N);
int cnt = 0;
int i = 0, j = 0;
while(i < N && j < other.N){
if(cmp(temp1[i], temp2[j])) temp[cnt ++] = temp1[i ++];
else temp[cnt ++] = temp2[j ++];
}
for(; i < N; i ++) temp[cnt ++] = temp1[i ++];
for(; j < other.N; j ++) temp[cnt ++] = temp2[j ++];
convhull res;
res.N = N + other.N;
res.ch.resize(res.N);
cnt = 0;
res.ch[cnt ++] = ch[0] + other.ch[0];
for(int i = 0; i + 1 < res.N; i ++){
res.ch[cnt ++] = res.ch[cnt - 1] + temp[i];
}
res.R = std::max_element(all(res.ch)) - res.ch.begin();
return res;
}
convhull operator^(const convhull &other) const{
if(!N) return other;
if(!other.N) return *this;
auto temp1 = linearize(), temp2 = other.linearize();
vector<point> res(temp1.size() + temp2.size());
merge(all(temp1), all(temp2), res.begin());
return convhull(res, true);
}
convhull operator-() const{
convhull res(*this);
res.R = N - R;
if(N == 1) res.R = 0;
for(int i = 0; i + R < N; i ++) res.ch[i] = point(-ch[i + R].x, -ch[i + R].y);
for(int i = N - R; i < N; i ++) res.ch[i] = point(-ch[i + R - N].x, -ch[i + R - N].y);
return res;
}
};