#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;
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;
}
const int bit_n = 30'001;
short bit[bit_n];
void clear(const int n) { memset(bit, 0, sizeof(short) * n); }
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
return ret;
}
int sum(int l, int r) { return sum(r) - sum(l - 1); }
void add(int idx, const short delta) {
for (; idx < bit_n; idx = idx | (idx + 1)) bit[idx] += delta;
}
int query(const vector<Point>& points1, const vector<Point>& points2,
const vector<Point>& ord_b) {
if (points1.empty() || points2.empty()) return 0;
clear(points2.size());
int total = 0;
int j = 0;
for (const auto& q : points2) {
if (!above(q)) add(q.idx, 1);
}
for (const Point& p : points1) {
while (j < (int)points2.size()) {
const Point& q = points2[j];
if (p < q) break;
add(q.idx, above(q) ? 1 : -1);
j++;
}
Point from = p - B;
Point to = B - p;
if (!above(p)) swap(from, to);
const int a = lower_bound(ord_b.begin(), ord_b.end(), from, cmp_by_b) - ord_b.begin();
const int b = lower_bound(ord_b.begin(), ord_b.end(), to, cmp_by_b) - ord_b.begin();
total += sum(a, b - 1);
}
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);
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;
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]) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
584 KB |
Output is correct |
4 |
Correct |
43 ms |
756 KB |
Output is correct |
5 |
Correct |
24 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1172 KB |
Output is correct |
2 |
Correct |
45 ms |
1116 KB |
Output is correct |
3 |
Correct |
17 ms |
1372 KB |
Output is correct |
4 |
Correct |
11 ms |
1368 KB |
Output is correct |
5 |
Correct |
12 ms |
3164 KB |
Output is correct |
6 |
Correct |
13 ms |
1264 KB |
Output is correct |
7 |
Correct |
13 ms |
1244 KB |
Output is correct |
8 |
Correct |
15 ms |
1244 KB |
Output is correct |
9 |
Correct |
10 ms |
1244 KB |
Output is correct |
10 |
Correct |
10 ms |
1212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
584 KB |
Output is correct |
4 |
Correct |
43 ms |
756 KB |
Output is correct |
5 |
Correct |
24 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
18 ms |
1172 KB |
Output is correct |
12 |
Correct |
45 ms |
1116 KB |
Output is correct |
13 |
Correct |
17 ms |
1372 KB |
Output is correct |
14 |
Correct |
11 ms |
1368 KB |
Output is correct |
15 |
Correct |
12 ms |
3164 KB |
Output is correct |
16 |
Correct |
13 ms |
1264 KB |
Output is correct |
17 |
Correct |
13 ms |
1244 KB |
Output is correct |
18 |
Correct |
15 ms |
1244 KB |
Output is correct |
19 |
Correct |
10 ms |
1244 KB |
Output is correct |
20 |
Correct |
10 ms |
1212 KB |
Output is correct |
21 |
Correct |
17 ms |
1244 KB |
Output is correct |
22 |
Correct |
45 ms |
1320 KB |
Output is correct |
23 |
Correct |
302 ms |
1512 KB |
Output is correct |
24 |
Correct |
453 ms |
1776 KB |
Output is correct |
25 |
Correct |
57 ms |
2132 KB |
Output is correct |
26 |
Correct |
38 ms |
3160 KB |
Output is correct |
27 |
Correct |
14 ms |
3672 KB |
Output is correct |
28 |
Correct |
14 ms |
3664 KB |
Output is correct |
29 |
Execution timed out |
4054 ms |
3400 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |