#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y, idx, tribe;
Point operator-(const Point p) const { return {x - p.x, y - p.y, idx, tribe}; }
Point operator+(const Point p) const { return {x + p.x, y + p.y, idx, tribe}; }
bool ccw(const Point p) const { return (long long)x * p.y - (long long)y * p.x > 0; }
Point negate() const { return {-x, -y, idx, tribe}; }
};
template <class T, unsigned int N>
struct BIT {
void clear(const int n) { memset(bit, 0, sizeof(T) * n); }
T sum(int l, int r) { return sum(r) - sum(l - 1); }
void add(unsigned int idx, const T delta) {
for (; idx < N; idx = idx | (idx + 1)) bit[idx] += delta;
}
private:
T bit[N];
T sum(int r) {
T ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
return ret;
}
};
BIT<int, 30'001> bit;
Point A, B;
vector<unordered_map<int, int>> ans;
vector<vector<Point>> tribes, order_by_b;
bool above(const Point p) { return B.ccw(p); }
Point to_upper(const Point p) { return above(p) ? p : p.negate(); }
bool operator<(const Point p, const Point q) { return to_upper(p).ccw(to_upper(q)); }
bool cmp(const Point p, const Point q) {
return above(p) == above(q) ? p.ccw(q) : above(p);
}
void query(const vector<Point>& multi_tribes, const int tribe_idx, const bool attack) {
const vector<Point>& single_tribe = tribes[tribe_idx];
const vector<Point>& ord = order_by_b[tribe_idx];
bit.clear(single_tribe.size());
const int m = lower_bound(ord.begin(), ord.end(), B.negate(), cmp) - ord.begin();
int j = 0;
for (const Point p : multi_tribes) {
while (j < (int)single_tribe.size() && single_tribe[j] < p) {
bit.add(single_tribe[j++].idx, 1);
}
const int a = lower_bound(ord.begin(), ord.end(), p - B, cmp) - ord.begin();
const int b = lower_bound(ord.begin(), ord.end(), B - p, cmp) - ord.begin();
int& total = attack ? ans[p.tribe][tribe_idx] : ans[tribe_idx][p.tribe];
total += above(p) ? b - m - bit.sum(m, b - 1) : bit.sum(0, m - 1) - b;
if (attack) {
total += above(p) ? bit.sum(a, m - 1) : a - m - bit.sum(m, a - 1);
} else {
total += above(p) ? a - bit.sum(0, a - 1) : bit.sum(a, ord.size() - 1);
}
}
}
void run_queries(const map<int, set<int>>& attacks, const map<int, set<int>>& attacked) {
vector<tuple<long long, int, bool>> queries;
const auto add_weighted_query = [&](const auto& q, const bool is_attack) {
const auto [tribe_idx, other] = q;
const long long weight = tribes[tribe_idx].size() * other.size();
queries.emplace_back(weight, tribe_idx, is_attack);
};
for (const auto& q : attacks) add_weighted_query(q, true);
for (const auto& q : attacked) add_weighted_query(q, false);
sort(queries.rbegin(), queries.rend());
for (const auto& [_, tribe_idx, is_attack] : queries) {
vector<Point> points;
const auto& other_tribes = (is_attack ? attacks : attacked).at(tribe_idx);
for (const int tribe_idx2 : other_tribes) {
int i = tribe_idx, j = tribe_idx2;
if (!is_attack) swap(i, j);
if (ans[i].count(j)) continue;
copy(tribes[tribe_idx2].begin(), tribes[tribe_idx2].end(), back_inserter(points));
ans[i][j] = 0;
}
sort(points.begin(), points.end());
query(points, tribe_idx, !is_attack); // TODO: Fix naming and usage (is_attack)
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, Q;
while (cin >> N >> M) {
tribes.assign(M + 1, vector<Point>());
order_by_b.assign(M + 1, vector<Point>());
ans.assign(M + 1, unordered_map<int, int>());
for (int i = 0; i < N; i++) {
Point p;
cin >> p.x >> p.y >> p.tribe;
tribes[p.tribe].push_back(p);
}
cin >> A.x >> A.y >> B.x >> B.y;
cin >> Q;
B = B - A;
for (int m = 1; m <= M; m++) {
vector<Point>& points = tribes[m];
for (Point& p : points) p = p - A - B;
sort(points.begin(), points.end(), cmp);
for (int i = 0; i < (int)points.size(); i++) points[i].idx = i;
order_by_b[m] = points;
for (Point& p : points) p = p + B;
sort(points.begin(), points.end());
}
map<int, set<int>> attacks, attacked;
vector<pair<int, int>> query_original_order;
while (Q--) {
int i, j;
cin >> i >> j;
attacks[i].emplace(j);
attacked[j].emplace(i);
query_original_order.emplace_back(i, j);
}
run_queries(attacks, attacked);
for (const auto& [i, j] : query_original_order) {
cout << ans[i][j] << '\n';
}
}
}
////
Compilation message
dragon2.cpp: In lambda function:
dragon2.cpp:80:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | const auto [tribe_idx, other] = q;
| ^
dragon2.cpp: In function 'void run_queries(const std::map<int, std::set<int> >&, const std::map<int, std::set<int> >&)':
dragon2.cpp:90:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
90 | for (const auto& [_, tribe_idx, is_attack] : queries) {
| ^
dragon2.cpp: In function 'int main()':
dragon2.cpp:153:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
153 | for (const auto& [i, j] : query_original_order) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
30 ms |
1456 KB |
Output is correct |
4 |
Correct |
151 ms |
16340 KB |
Output is correct |
5 |
Correct |
144 ms |
17092 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1624 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2328 KB |
Output is correct |
2 |
Correct |
55 ms |
2772 KB |
Output is correct |
3 |
Correct |
20 ms |
2392 KB |
Output is correct |
4 |
Correct |
12 ms |
2272 KB |
Output is correct |
5 |
Correct |
13 ms |
5724 KB |
Output is correct |
6 |
Correct |
15 ms |
2540 KB |
Output is correct |
7 |
Correct |
14 ms |
2264 KB |
Output is correct |
8 |
Correct |
16 ms |
2260 KB |
Output is correct |
9 |
Correct |
11 ms |
2272 KB |
Output is correct |
10 |
Correct |
11 ms |
2340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
5 ms |
600 KB |
Output is correct |
3 |
Correct |
30 ms |
1456 KB |
Output is correct |
4 |
Correct |
151 ms |
16340 KB |
Output is correct |
5 |
Correct |
144 ms |
17092 KB |
Output is correct |
6 |
Correct |
4 ms |
1628 KB |
Output is correct |
7 |
Correct |
3 ms |
1624 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
18 ms |
2328 KB |
Output is correct |
12 |
Correct |
55 ms |
2772 KB |
Output is correct |
13 |
Correct |
20 ms |
2392 KB |
Output is correct |
14 |
Correct |
12 ms |
2272 KB |
Output is correct |
15 |
Correct |
13 ms |
5724 KB |
Output is correct |
16 |
Correct |
15 ms |
2540 KB |
Output is correct |
17 |
Correct |
14 ms |
2264 KB |
Output is correct |
18 |
Correct |
16 ms |
2260 KB |
Output is correct |
19 |
Correct |
11 ms |
2272 KB |
Output is correct |
20 |
Correct |
11 ms |
2340 KB |
Output is correct |
21 |
Correct |
18 ms |
2260 KB |
Output is correct |
22 |
Correct |
57 ms |
2600 KB |
Output is correct |
23 |
Correct |
400 ms |
3588 KB |
Output is correct |
24 |
Correct |
689 ms |
17604 KB |
Output is correct |
25 |
Correct |
189 ms |
20664 KB |
Output is correct |
26 |
Correct |
266 ms |
29004 KB |
Output is correct |
27 |
Correct |
35 ms |
13008 KB |
Output is correct |
28 |
Correct |
36 ms |
13208 KB |
Output is correct |
29 |
Correct |
234 ms |
28900 KB |
Output is correct |
30 |
Correct |
220 ms |
27892 KB |
Output is correct |
31 |
Correct |
214 ms |
28780 KB |
Output is correct |
32 |
Correct |
279 ms |
28960 KB |
Output is correct |
33 |
Correct |
492 ms |
27920 KB |
Output is correct |
34 |
Correct |
232 ms |
29080 KB |
Output is correct |
35 |
Correct |
222 ms |
29052 KB |
Output is correct |
36 |
Correct |
277 ms |
27960 KB |
Output is correct |
37 |
Correct |
223 ms |
28964 KB |
Output is correct |
38 |
Correct |
486 ms |
28272 KB |
Output is correct |
39 |
Correct |
519 ms |
28224 KB |
Output is correct |
40 |
Correct |
489 ms |
27800 KB |
Output is correct |
41 |
Correct |
230 ms |
28580 KB |
Output is correct |
42 |
Correct |
235 ms |
28496 KB |
Output is correct |
43 |
Correct |
316 ms |
28608 KB |
Output is correct |
44 |
Correct |
32 ms |
7868 KB |
Output is correct |
45 |
Correct |
34 ms |
7784 KB |
Output is correct |
46 |
Correct |
34 ms |
7888 KB |
Output is correct |
47 |
Correct |
37 ms |
9432 KB |
Output is correct |
48 |
Correct |
36 ms |
9328 KB |
Output is correct |
49 |
Correct |
43 ms |
9600 KB |
Output is correct |