#include <bits/stdc++.h>
using namespace std;
using Point = complex<long long>;
long long Dot(Point a, Point b) {
return real(conj(a) * b);
}
long long Cross(Point a, Point b) {
return imag(conj(a) * b);
}
bool Clockwise(Point a, Point b, Point c) {
return Cross(b - a, c - a) > 0;
}
bool CounterClockwise(Point a, Point b, Point c) {
return Cross(b - a, c - a) < 0;
}
namespace std {
template<class T>
istream& operator>>(istream &is, complex<T> &inp) {
T a, b;
is >> a >> b;
inp = complex<T>(a, b);
return is;
}
bool operator<(Point p, Point q) {
return Cross(p, q) > 0;
}
}
class Fenwick {
public:
int sz;
vector<int> tree;
Fenwick() {}
Fenwick(int sz) : sz(sz), tree(sz) {}
void Update(int p, int x) {
for (int i = p; i < sz; i |= i + 1) {
tree[i] += x;
}
}
int Sum(int p) {
int res = 0;
for (int i = p + 1; i > 0; i &= i - 1) {
res += tree[i - 1];
}
return res;
}
int Sum(int l, int r) {
return Sum(r) - Sum(l - 1);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, M;
cin >> N >> M;
vector<int> C(N);
vector<Point> P(N);
for (int i = 0; i < N; i++) {
cin >> P[i] >> C[i];
C[i]--;
}
Point X, Y;
cin >> X >> Y;
vector<bool> side(N);
for (int i = 0; i < N; i++) {
side[i] = Clockwise(X, Y, P[i]);
}
vector<Point> angle_x(N), angle_y(N);
vector<pair<Point, int>> sorted_x(N);
vector<pair<int, Point>> sorted_y(N);
for (int i = 0; i < N; i++) {
angle_x[i] = (side[i] ? P[i] - X : X - P[i]);
sorted_x[i] = make_pair(angle_x[i], i);
angle_y[i] = (side[i] ? P[i] - Y : Y - P[i]);
sorted_y[i] = make_pair(C[i], angle_y[i]);
}
sort(begin(sorted_x), end(sorted_x));
sort(begin(sorted_y), end(sorted_y));
vector<int> pos_in_sorted(N), color_pos(M + 1);
for (int i = 0; i < N; i++) {
pos_in_sorted[i] = lower_bound(begin(sorted_y), end(sorted_y), make_pair(C[i], angle_y[i])) - begin(sorted_y);
}
for (int i = 0; i <= M; i++) {
color_pos[i] = lower_bound(begin(sorted_y), end(sorted_y), make_pair(i, Y - X)) - begin(sorted_y);;
}
int Q;
cin >> Q;
vector<int> A(Q), B(Q);
vector<long long> ans(Q);
vector<vector<pair<int, int>>> query_a(M);
vector<vector<pair<int, int>>> query_b(M);
for (int i = 0; i < Q; i++) {
cin >> A[i] >> B[i];
A[i]--, B[i]--;
query_a[A[i]].emplace_back(B[i], i);
}
// O(N sqrt Q log N)
const int SQRT = round(sqrt(Q));
for (int i = 0; i < M; i++) {
if (query_a[i].size() >= SQRT) {
for (auto j : query_a[i]) {
query_b[j.first].emplace_back(i, j.second);
}
query_a[i].clear();
}
}
{ // Solve query_a
Fenwick f0(N); // side[i] = true
Fenwick f1(N); // side[i] = false
for (int i = 0; i < N; i++) {
if (!side[i]) {
f1.Update(pos_in_sorted[i], +1);
}
}
for (int t = 0; t < N; t++) {
int i = sorted_x[t].second;
int c = C[i];
for (auto q : query_a[c]) {
int pos = lower_bound(begin(sorted_y), end(sorted_y), make_pair(q.first, angle_y[i])) - begin(sorted_y);
ans[q.second] += f0.Sum(pos, color_pos[q.first + 1] - 1);
ans[q.second] += f1.Sum(color_pos[q.first], pos - 1);
}
if (side[i]) {
f0.Update(pos_in_sorted[i], +1);
} else {
f1.Update(pos_in_sorted[i], -1);
}
}
}
{ // Solve query_b
Fenwick f0(N); // side[i] = true
Fenwick f1(N); // side[i] = false
for (int i = 0; i < N; i++) {
f0.Update(pos_in_sorted[i], +1);
}
for (int t = 0; t < N; t++) {
int i = sorted_x[t].second;
int c = C[i];
for (auto q : query_b[c]) {
int pos = lower_bound(begin(sorted_y), end(sorted_y), make_pair(q.first, angle_y[i])) - begin(sorted_y);
if (side[i]) {
ans[q.second] += f0.Sum(color_pos[q.first], pos - 1);
} else {
ans[q.second] += f1.Sum(pos, color_pos[q.first + 1] - 1);
}
}
f0.Update(pos_in_sorted[i], -1);
f1.Update(pos_in_sorted[i], +1);
}
}
for (int i = 0; i < Q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
dragon2.cpp: In function 'int main()':
dragon2.cpp:118:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'const int' [-Wsign-compare]
118 | if (query_a[i].size() >= SQRT) {
| ~~~~~~~~~~~~~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
25 ms |
768 KB |
Output is correct |
4 |
Correct |
75 ms |
3576 KB |
Output is correct |
5 |
Correct |
49 ms |
3960 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
3 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
3600 KB |
Output is correct |
2 |
Correct |
57 ms |
3612 KB |
Output is correct |
3 |
Correct |
34 ms |
3600 KB |
Output is correct |
4 |
Correct |
28 ms |
3732 KB |
Output is correct |
5 |
Correct |
31 ms |
5232 KB |
Output is correct |
6 |
Correct |
27 ms |
3600 KB |
Output is correct |
7 |
Correct |
28 ms |
3600 KB |
Output is correct |
8 |
Correct |
28 ms |
3600 KB |
Output is correct |
9 |
Correct |
20 ms |
3600 KB |
Output is correct |
10 |
Correct |
22 ms |
3712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
25 ms |
768 KB |
Output is correct |
4 |
Correct |
75 ms |
3576 KB |
Output is correct |
5 |
Correct |
49 ms |
3960 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
3 ms |
896 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
30 ms |
3600 KB |
Output is correct |
12 |
Correct |
57 ms |
3612 KB |
Output is correct |
13 |
Correct |
34 ms |
3600 KB |
Output is correct |
14 |
Correct |
28 ms |
3732 KB |
Output is correct |
15 |
Correct |
31 ms |
5232 KB |
Output is correct |
16 |
Correct |
27 ms |
3600 KB |
Output is correct |
17 |
Correct |
28 ms |
3600 KB |
Output is correct |
18 |
Correct |
28 ms |
3600 KB |
Output is correct |
19 |
Correct |
20 ms |
3600 KB |
Output is correct |
20 |
Correct |
22 ms |
3712 KB |
Output is correct |
21 |
Correct |
30 ms |
3600 KB |
Output is correct |
22 |
Correct |
56 ms |
3596 KB |
Output is correct |
23 |
Correct |
342 ms |
3960 KB |
Output is correct |
24 |
Correct |
726 ms |
6740 KB |
Output is correct |
25 |
Correct |
135 ms |
7328 KB |
Output is correct |
26 |
Correct |
102 ms |
8616 KB |
Output is correct |
27 |
Correct |
34 ms |
5904 KB |
Output is correct |
28 |
Correct |
33 ms |
5912 KB |
Output is correct |
29 |
Correct |
82 ms |
9204 KB |
Output is correct |
30 |
Correct |
524 ms |
9896 KB |
Output is correct |
31 |
Correct |
85 ms |
10272 KB |
Output is correct |
32 |
Correct |
89 ms |
10868 KB |
Output is correct |
33 |
Correct |
875 ms |
10024 KB |
Output is correct |
34 |
Correct |
92 ms |
10280 KB |
Output is correct |
35 |
Correct |
89 ms |
10872 KB |
Output is correct |
36 |
Correct |
93 ms |
9888 KB |
Output is correct |
37 |
Correct |
94 ms |
10404 KB |
Output is correct |
38 |
Correct |
280 ms |
10792 KB |
Output is correct |
39 |
Correct |
1004 ms |
10448 KB |
Output is correct |
40 |
Correct |
867 ms |
10028 KB |
Output is correct |
41 |
Correct |
87 ms |
10664 KB |
Output is correct |
42 |
Correct |
94 ms |
10528 KB |
Output is correct |
43 |
Correct |
105 ms |
10656 KB |
Output is correct |
44 |
Correct |
38 ms |
6048 KB |
Output is correct |
45 |
Correct |
38 ms |
6164 KB |
Output is correct |
46 |
Correct |
38 ms |
6144 KB |
Output is correct |
47 |
Correct |
36 ms |
5920 KB |
Output is correct |
48 |
Correct |
37 ms |
6016 KB |
Output is correct |
49 |
Correct |
39 ms |
5920 KB |
Output is correct |