This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// TODO: New index category: point inside polygon (log N), and polygon tangent.
ll Bx, By;
struct Point {
ll x, y;
Point operator-(const Point p) const { return {x - p.x, y - p.y}; }
Point operator+(const Point p) const { return {x + p.x, y + p.y}; }
ll cross(const Point p) const { return x * p.y - y * p.x; }
bool operator<(const Point p) const { return to_upper().cross(p.to_upper()) > 0; }
Point to_upper() const { return above() ? *this : negate(); }
bool above() const {
const Point B{Bx, By};
return B.cross(*this) > 0;
}
ll operator*(const Point p) const { return x * p.x + y * p.y; }
bool deg0(const Point p) const { return cross(p) == 0 && *this * p >= 0; }
bool deg180(const Point p) const { return cross(p) == 0 && *this * p < 0; }
Point negate() const { return {-x, -y}; }
};
ostream& operator<<(ostream& os, const Point& p) {
return os << "(" << p.x << ", " << p.y << ")";
}
short orientation(const Point o, const Point a, const Point b) {
const ll cross = (a - o).cross(b - o);
assert(cross != 0);
return (cross > 0) - (cross < 0);
}
int handle_query(const vector<Point>& points1, const vector<Point>& points2,
const Point B) {
set<Point> active;
for (const auto q : points2) {
if (!q.above()) {
active.emplace(q);
}
}
int total = 0;
int j = 0;
for (const Point p : points1) {
while (j < (int)points2.size()) {
const Point q = points2[j];
if (q.to_upper().cross(p.to_upper()) < 0) {
break;
}
if (q.above()) {
assert(!active.count(q));
active.emplace(q);
assert(active.count(q));
} else {
assert(active.count(q));
active.erase(q);
assert(!active.count(q));
}
j++;
}
// TODO: (Brute force)
for (const Point q : points2) {
if (!active.count(q)) continue;
if (p.above()) {
if (orientation(B, p, q) > 0) total++;
} else {
if (orientation(p, B, q) > 0) total++;
}
}
}
return total;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, Q;
while (cin >> N >> M) {
vector<Point> points;
unordered_map<int, vector<Point>> tribe_points;
for (int i = 0; i < N; i++) {
Point p;
int tribe;
cin >> p.x >> p.y >> tribe;
tribe_points[tribe].push_back(p);
}
Point A;
cin >> A.x >> A.y;
cin >> Bx >> By;
cin >> Q;
Bx -= A.x;
By -= A.y;
const Point B{Bx, By};
for (auto& [_, points] : tribe_points) {
for (auto& p : points) {
p = p - A;
}
sort(points.begin(), points.end());
}
while (Q--) {
int i, j;
cin >> i >> j;
cout << handle_query(tribe_points[i], tribe_points[j], B) << '\n';
}
}
}
Compilation message (stderr)
dragon2.cpp: In function 'int main()':
dragon2.cpp:103:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
103 | for (auto& [_, points] : tribe_points) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |