#include <bits/stdc++.h>
using namespace std;
vector<int> mid_it;
struct Point {
int x, y;
int idx;
Point operator-(const Point& p) const { return {x - p.x, y - p.y, idx}; }
Point operator+(const Point& p) const { return {x + p.x, y + p.y, idx}; }
long long cross(const Point& p) const {
return x * (long long)p.y - y * (long long)p.x;
}
Point negate() const { return {-x, -y, idx}; }
};
short orientation(const Point& o, const Point& a, const Point& b) {
const long long x = (a - o).cross(b - o);
return (x > 0) - (x < 0);
}
Point A, B;
bool above(const Point& p) { return B.cross(p) > 0; }
Point to_upper(const Point& p) { return above(p) ? p : p.negate(); }
bool operator<(const Point& p, const Point& q) {
return to_upper(p).cross(to_upper(q)) > 0;
}
bool cmp_by_b(const Point& p, const Point& q) {
const bool a1 = above(p);
const bool a2 = above(q);
if (a1 != a2) return a1;
return p.cross(q) > 0;
}
int query(const vector<Point>& points1, const vector<Point>& points2,
const vector<Point>& ord_b, const int attacked_idx) {
if (points1.empty() || points2.empty()) return 0;
int total = 0;
int events[]{0, 0};
int j = 0;
for (const Point& p : points1) {
while (j < (int)points2.size()) {
const Point& q = points2[j];
if (p < q) break;
events[above(q)]++;
j++;
}
const auto mid = ord_b.begin() + mid_it[attacked_idx];
if (above(p)) {
const auto it = lower_bound(mid, ord_b.end(), B - p, cmp_by_b);
const int count = it - mid;
total += count - events[0];
for (auto it = mid == ord_b.end() ? mid - 1 : mid;; it--) {
const Point q = *it + B;
total += above(q) && p.cross(q) < 0 && orientation(B, p, q) > 0;
if (it == ord_b.begin()) break;
}
} else {
const auto it = lower_bound(ord_b.begin(), mid, B - p, cmp_by_b);
const int count = it - ord_b.begin();
total += events[1] - count;
for (auto it = mid; it != ord_b.end(); it++) {
const Point q = *it + B;
total += p.cross(q) > 0 && orientation(B, p, q) < 0;
}
}
}
return total;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, Q;
while (cin >> N >> M) {
vector<vector<Point>> tribe_points(M + 1);
vector<vector<Point>> order_by_b(M + 1);
mid_it.resize(M + 1);
for (int i = 0; i < N; i++) {
Point p;
int tribe;
cin >> p.x >> p.y >> tribe;
tribe_points[tribe].push_back(p);
}
cin >> A.x >> A.y;
cin >> B.x >> B.y;
cin >> Q;
B = B - A;
for (int m = 0; m <= M; m++) {
auto& points = tribe_points[m];
for (auto& p : points) p = p - A;
for (auto& p : points) p = p - B;
sort(points.begin(), points.end(), cmp_by_b);
for (int i = 0; i < (int)points.size(); i++) points[i].idx = i;
auto& b = order_by_b[m];
b = points;
mid_it[m] = lower_bound(b.begin(), b.end(), B.negate(), cmp_by_b) - b.begin();
for (auto& p : points) p = p + B;
sort(points.begin(), points.end());
}
while (Q--) {
int i, j;
cin >> i >> j;
cout << query(tribe_points[i], tribe_points[j], order_by_b[j], j) << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
348 KB |
Output is correct |
3 |
Correct |
19 ms |
348 KB |
Output is correct |
4 |
Correct |
31 ms |
772 KB |
Output is correct |
5 |
Correct |
22 ms |
904 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
351 ms |
1288 KB |
Output is correct |
2 |
Correct |
603 ms |
1276 KB |
Output is correct |
3 |
Correct |
26 ms |
1284 KB |
Output is correct |
4 |
Correct |
11 ms |
1372 KB |
Output is correct |
5 |
Correct |
12 ms |
3280 KB |
Output is correct |
6 |
Correct |
597 ms |
1492 KB |
Output is correct |
7 |
Correct |
430 ms |
1280 KB |
Output is correct |
8 |
Correct |
14 ms |
1244 KB |
Output is correct |
9 |
Correct |
344 ms |
1276 KB |
Output is correct |
10 |
Correct |
9 ms |
1212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
13 ms |
348 KB |
Output is correct |
3 |
Correct |
19 ms |
348 KB |
Output is correct |
4 |
Correct |
31 ms |
772 KB |
Output is correct |
5 |
Correct |
22 ms |
904 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
351 ms |
1288 KB |
Output is correct |
12 |
Correct |
603 ms |
1276 KB |
Output is correct |
13 |
Correct |
26 ms |
1284 KB |
Output is correct |
14 |
Correct |
11 ms |
1372 KB |
Output is correct |
15 |
Correct |
12 ms |
3280 KB |
Output is correct |
16 |
Correct |
597 ms |
1492 KB |
Output is correct |
17 |
Correct |
430 ms |
1280 KB |
Output is correct |
18 |
Correct |
14 ms |
1244 KB |
Output is correct |
19 |
Correct |
344 ms |
1276 KB |
Output is correct |
20 |
Correct |
9 ms |
1212 KB |
Output is correct |
21 |
Correct |
345 ms |
1244 KB |
Output is correct |
22 |
Correct |
596 ms |
1276 KB |
Output is correct |
23 |
Correct |
728 ms |
1728 KB |
Output is correct |
24 |
Correct |
381 ms |
1744 KB |
Output is correct |
25 |
Correct |
44 ms |
2128 KB |
Output is correct |
26 |
Correct |
35 ms |
3416 KB |
Output is correct |
27 |
Correct |
14 ms |
3676 KB |
Output is correct |
28 |
Correct |
14 ms |
3908 KB |
Output is correct |
29 |
Correct |
2564 ms |
3372 KB |
Output is correct |
30 |
Correct |
62 ms |
3132 KB |
Output is correct |
31 |
Correct |
34 ms |
3160 KB |
Output is correct |
32 |
Correct |
51 ms |
3328 KB |
Output is correct |
33 |
Correct |
459 ms |
3500 KB |
Output is correct |
34 |
Correct |
32 ms |
3408 KB |
Output is correct |
35 |
Correct |
35 ms |
3420 KB |
Output is correct |
36 |
Correct |
34 ms |
3408 KB |
Output is correct |
37 |
Correct |
35 ms |
3408 KB |
Output is correct |
38 |
Correct |
657 ms |
3408 KB |
Output is correct |
39 |
Correct |
422 ms |
3420 KB |
Output is correct |
40 |
Correct |
359 ms |
3408 KB |
Output is correct |
41 |
Correct |
2029 ms |
3576 KB |
Output is correct |
42 |
Correct |
1156 ms |
3380 KB |
Output is correct |
43 |
Correct |
750 ms |
3152 KB |
Output is correct |
44 |
Correct |
2015 ms |
2532 KB |
Output is correct |
45 |
Correct |
1031 ms |
2136 KB |
Output is correct |
46 |
Correct |
638 ms |
2240 KB |
Output is correct |
47 |
Correct |
870 ms |
2472 KB |
Output is correct |
48 |
Correct |
479 ms |
2388 KB |
Output is correct |
49 |
Correct |
314 ms |
2280 KB |
Output is correct |