#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y, 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;
vector<int> mid_it;
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 |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
572 KB |
Output is correct |
3 |
Correct |
19 ms |
348 KB |
Output is correct |
4 |
Correct |
32 ms |
760 KB |
Output is correct |
5 |
Correct |
22 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
352 ms |
1292 KB |
Output is correct |
2 |
Correct |
607 ms |
1276 KB |
Output is correct |
3 |
Correct |
27 ms |
1368 KB |
Output is correct |
4 |
Correct |
11 ms |
1296 KB |
Output is correct |
5 |
Correct |
12 ms |
3164 KB |
Output is correct |
6 |
Correct |
604 ms |
1064 KB |
Output is correct |
7 |
Correct |
443 ms |
1276 KB |
Output is correct |
8 |
Correct |
15 ms |
1244 KB |
Output is correct |
9 |
Correct |
364 ms |
1240 KB |
Output is correct |
10 |
Correct |
9 ms |
1208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
11 ms |
572 KB |
Output is correct |
3 |
Correct |
19 ms |
348 KB |
Output is correct |
4 |
Correct |
32 ms |
760 KB |
Output is correct |
5 |
Correct |
22 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
352 ms |
1292 KB |
Output is correct |
12 |
Correct |
607 ms |
1276 KB |
Output is correct |
13 |
Correct |
27 ms |
1368 KB |
Output is correct |
14 |
Correct |
11 ms |
1296 KB |
Output is correct |
15 |
Correct |
12 ms |
3164 KB |
Output is correct |
16 |
Correct |
604 ms |
1064 KB |
Output is correct |
17 |
Correct |
443 ms |
1276 KB |
Output is correct |
18 |
Correct |
15 ms |
1244 KB |
Output is correct |
19 |
Correct |
364 ms |
1240 KB |
Output is correct |
20 |
Correct |
9 ms |
1208 KB |
Output is correct |
21 |
Correct |
350 ms |
1488 KB |
Output is correct |
22 |
Correct |
597 ms |
1272 KB |
Output is correct |
23 |
Correct |
750 ms |
1372 KB |
Output is correct |
24 |
Correct |
411 ms |
1668 KB |
Output is correct |
25 |
Correct |
44 ms |
2404 KB |
Output is correct |
26 |
Correct |
36 ms |
3408 KB |
Output is correct |
27 |
Correct |
19 ms |
3672 KB |
Output is correct |
28 |
Correct |
15 ms |
3672 KB |
Output is correct |
29 |
Correct |
2512 ms |
3480 KB |
Output is correct |
30 |
Correct |
59 ms |
3168 KB |
Output is correct |
31 |
Correct |
35 ms |
3160 KB |
Output is correct |
32 |
Correct |
52 ms |
3408 KB |
Output is correct |
33 |
Correct |
450 ms |
3444 KB |
Output is correct |
34 |
Correct |
41 ms |
3412 KB |
Output is correct |
35 |
Correct |
33 ms |
3420 KB |
Output is correct |
36 |
Correct |
34 ms |
3360 KB |
Output is correct |
37 |
Correct |
37 ms |
3408 KB |
Output is correct |
38 |
Correct |
686 ms |
3412 KB |
Output is correct |
39 |
Correct |
436 ms |
3408 KB |
Output is correct |
40 |
Correct |
356 ms |
3496 KB |
Output is correct |
41 |
Correct |
2021 ms |
3428 KB |
Output is correct |
42 |
Correct |
1115 ms |
3668 KB |
Output is correct |
43 |
Correct |
740 ms |
3412 KB |
Output is correct |
44 |
Correct |
1968 ms |
2484 KB |
Output is correct |
45 |
Correct |
1040 ms |
2352 KB |
Output is correct |
46 |
Correct |
646 ms |
2288 KB |
Output is correct |
47 |
Correct |
905 ms |
2268 KB |
Output is correct |
48 |
Correct |
488 ms |
2240 KB |
Output is correct |
49 |
Correct |
327 ms |
2540 KB |
Output is correct |