1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
const long double EPS = 1e-11;
struct Boma {
int id;
long long x, y, r;
};
struct Event {
long double x;
int type; // 0: Start Boma, 1: Query, 2: End Boma
int id;
bool operator<(const Event& other) const {
if (abs(x - other.x) > EPS) return x < other.x;
return type < other.type;
}
};
long double current_sweep_x;
vector<Boma> bomas;
vector<Boma> queries;
// Helper to get Boma object whether it's existing or query
const Boma& getBoma(int id) {
if (id > 0) return bomas[id];
return queries[-(id + 1)];
}
struct Arc {
int id;
bool is_upper;
long double get_y() const {
const Boma& b = getBoma(id);
if (abs(b.x - current_sweep_x) >= b.r) return b.y;
long double dx = current_sweep_x - b.x;
long double r_sq = (long double)b.r * b.r;
long double dist_sq = dx * dx;
long double dy = sqrt(max((long double)0.0, r_sq - dist_sq));
return is_upper ? b.y + dy : b.y - dy;
}
bool operator<(const Arc& other) const {
long double y1 = get_y();
long double y2 = other.get_y();
if (abs(y1 - y2) > EPS) return y1 < y2;
if (id != other.id) return id < other.id;
return is_upper < other.is_upper;
}
};
// Tree structure
vector<int> adj[100005];
int parent[100005];
int query_parent[100005];
long long dp[100005][2]; // 0: no animal in region, 1: animal in region
// KD-Tree for 2D range sum
struct Point {
long long x, y;
long long val0, valMax;
int id;
};
struct KDNode {
long long min_x, max_x, min_y, max_y;
long long sum_val0, sum_valMax;
int left = -1, right = -1;
Point p;
KDNode(Point pt) : p(pt), min_x(pt.x), max_x(pt.x), min_y(pt.y), max_y(pt.y), sum_val0(pt.val0), sum_valMax(pt.valMax) {}
};
vector<KDNode> tree_nodes;
void update_bbox(int node_idx) {
KDNode& node = tree_nodes[node_idx];
if (node.left != -1) {
const KDNode& l = tree_nodes[node.left];
node.min_x = min(node.min_x, l.min_x);
node.max_x = max(node.max_x, l.max_x);
node.min_y = min(node.min_y, l.min_y);
node.max_y = max(node.max_y, l.max_y);
node.sum_val0 += l.sum_val0;
node.sum_valMax += l.sum_valMax;
}
if (node.right != -1) {
const KDNode& r = tree_nodes[node.right];
node.min_x = min(node.min_x, r.min_x);
node.max_x = max(node.max_x, r.max_x);
node.min_y = min(node.min_y, r.min_y);
node.max_y = max(node.max_y, r.max_y);
node.sum_val0 += r.sum_val0;
node.sum_valMax += r.sum_valMax;
}
}
int build_kdtree(vector<Point>& points, int depth, int l, int r) {
if (l > r) return -1;
int mid = (l + r) / 2;
if (depth % 2 == 0) {
nth_element(points.begin() + l, points.begin() + mid, points.begin() + r + 1,
[](const Point& a, const Point& b) { return a.x < b.x; });
} else {
nth_element(points.begin() + l, points.begin() + mid, points.begin() + r + 1,
[](const Point& a, const Point& b) { return a.y < b.y; });
}
tree_nodes.emplace_back(points[mid]);
int idx = tree_nodes.size() - 1;
tree_nodes[idx].left = build_kdtree(points, depth + 1, l, mid - 1);
tree_nodes[idx].right = build_kdtree(points, depth + 1, mid + 1, r);
update_bbox(idx);
return idx;
}
pair<long long, long long> query_kdtree(int node_idx, long long qx, long long qy, long long qr) {
if (node_idx == -1) return {0, 0};
const KDNode& node = tree_nodes[node_idx];
// Check if bbox is completely outside
long long dx = 0, dy = 0;
if (qx < node.min_x) dx = node.min_x - qx;
else if (qx > node.max_x) dx = qx - node.max_x;
if (qy < node.min_y) dy = node.min_y - qy;
else if (qy > node.max_y) dy = qy - node.max_y;
if (dx * dx + dy * dy > qr * qr) return {0, 0};
// Check if bbox is completely inside
long long d1 = (node.min_x - qx) * (node.min_x - qx);
long long d2 = (node.max_x - qx) * (node.max_x - qx);
long long d3 = (node.min_y - qy) * (node.min_y - qy);
long long d4 = (node.max_y - qy) * (node.max_y - qy);
long long max_dist_sq = max({d1 + d3, d1 + d4, d2 + d3, d2 + d4});
if (max_dist_sq <= qr * qr) {
return {node.sum_val0, node.sum_valMax};
}
// Partial overlap
pair<long long, long long> res = {0, 0};
// Check current point
long long pdx = node.p.x - qx;
long long pdy = node.p.y - qy;
if (pdx * pdx + pdy * pdy <= qr * qr) {
res.first += node.p.val0;
res.second += node.p.valMax;
}
pair<long long, long long> l_res = query_kdtree(node.left, qx, qy, qr);
pair<long long, long long> r_res = query_kdtree(node.right, qx, qy, qr);
res.first += l_res.first + r_res.first;
res.second += l_res.second + r_res.second;
return res;
}
void solve_dp(int u) {
long long sum_val0 = 0;
long long sum_valMax = 0;
for (int v : adj[u]) {
solve_dp(v);
sum_val0 += dp[v][0];
sum_valMax += max(dp[v][0], dp[v][1]);
}
dp[u][0] = sum_valMax;
dp[u][1] = 1 + sum_val0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
if (!(cin >> n >> q)) return 0;
bomas.resize(n + 1);
vector<Event> events;
// 0 is the root (outer world)
bomas[0] = {0, 0, 0, 2000000000LL};
for (int i = 1; i <= n; ++i) {
cin >> bomas[i].x >> bomas[i].y >> bomas[i].r;
bomas[i].id = i;
events.push_back({(long double)bomas[i].x - bomas[i].r, 0, i});
events.push_back({(long double)bomas[i].x + bomas[i].r, 2, i});
}
queries.resize(q);
for (int i = 0; i < q; ++i) {
cin >> queries[i].x >> queries[i].y >> queries[i].r;
queries[i].id = i;
events.push_back({(long double)queries[i].x, 1, i});
}
sort(events.begin(), events.end());
set<Arc> active_arcs;
for(int i=1; i<=n; ++i) parent[i] = 0;
for(int i=0; i<q; ++i) query_parent[i] = 0;
for (const auto& ev : events) {
current_sweep_x = ev.x;
if (ev.type == 0) { // Start Boma
int id = ev.id;
Arc up = {id, true};
Arc down = {id, false};
active_arcs.insert(up);
active_arcs.insert(down);
auto it_up = active_arcs.find(up);
auto next_it = next(it_up);
if (next_it != active_arcs.end()) {
if (next_it->is_upper) {
parent[id] = next_it->id;
} else {
parent[id] = parent[next_it->id];
}
} else {
parent[id] = 0;
}
} else if (ev.type == 1) { // Query
int qid = ev.id;
int mapped_id = -(qid + 1);
Arc up = {mapped_id, true};
active_arcs.insert(up);
auto it = active_arcs.find(up);
auto next_it = next(it);
if (next_it != active_arcs.end()) {
if (next_it->is_upper) {
query_parent[qid] = next_it->id;
} else {
query_parent[qid] = parent[next_it->id];
}
} else {
query_parent[qid] = 0;
}
active_arcs.erase(it);
} else { // End Boma
int id = ev.id;
active_arcs.erase({id, true});
active_arcs.erase({id, false});
}
}
// Build Tree
for (int i = 1; i <= n; ++i) {
adj[parent[i]].push_back(i);
}
// DP
solve_dp(0);
// Queries
map<int, vector<int>> queries_by_parent;
for (int i = 0; i < q; ++i) {
queries_by_parent[query_parent[i]].push_back(i);
}
vector<long long> answers(q);
for (auto& entry : queries_by_parent) {
int pid = entry.first;
const vector<int>& q_indices = entry.second;
if (adj[pid].empty()) {
for (int qidx : q_indices) answers[qidx] = 1;
continue;
}
tree_nodes.clear();
tree_nodes.reserve(adj[pid].size() * 4);
vector<Point> points;
points.reserve(adj[pid].size());
for (int child : adj[pid]) {
points.push_back({bomas[child].x, bomas[child].y, dp[child][0], max(dp[child][0], dp[child][1]), child});
}
int root_node = build_kdtree(points, 0, 0, (int)points.size() - 1);
for (int qidx : q_indices) {
pair<long long, long long> res = query_kdtree(root_node, queries[qidx].x, queries[qidx].y, queries[qidx].r);
answers[qidx] = max(res.second, 1 + res.first);
}
}
for (int i = 0; i < q; ++i) {
cout << answers[i] << "\n";
}
return 0;
}
|