#include<bits/stdc++.h>
using namespace std;
#define vec vector
#define int long long
struct Circle {
int x;
int y;
int r;
int i;
};
bool intersect(Circle& c1, Circle& c2) {
return (c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y) <= (c1.r + c2.r) * (c1.r + c2.r);
}
struct Grid {
int cell_size = 2e9;
vec<vec<Circle>> data;
Grid(vec<Circle> idata, int cell_size) : cell_size(cell_size) {
sort(idata.begin(), idata.end(), [&](Circle c1, Circle c2) { return c1.x / cell_size < c2.x / cell_size || (c1.x / cell_size == c2.x / cell_size && c1.y < c2.y); });
data = {};
vec<Circle> cur{idata[0]};
for(int i = 1; i<idata.size(); i++) {
if(idata[i].x/cell_size == cur.back().x/cell_size) {
cur.push_back(idata[i]);
}
else {
data.push_back(cur);
cur = {idata[i]};
}
}
data.push_back(cur);
}
vec<Circle> query(Circle c) {
int cell_span = c.r*2 / cell_size + 1;
int cx = c.x / cell_size;
int cy = c.y / cell_size;
auto it = lower_bound(data.begin(), data.end(), cx-cell_span, [&](vec<Circle>& col, int x) { return col[0].x / cell_size < x; });
vec<Circle> res{};
for(int i = it-data.begin(); i < data.size() && data[i][0].x / cell_size <= cx + cell_span; i++) {
auto it = lower_bound(data[i].begin(), data[i].end(), cy-cell_span, [&](Circle& oc, int val) {return oc.y / cell_size < val;});
int j = it-data[i].begin();
for(; j < data[i].size() && data[i][j].y / cell_size <= cy + cell_span; j++) {
res.push_back(data[i][j]);
}
}
return res;
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vec<Circle> cs(n);
for(int i = 0; i<n; i++) {
cs[i].i = i;
cin >> cs[i].x >> cs[i].y >> cs[i].r;
}
vec<int> ans(n, -1);
sort(cs.begin(), cs.end(), [](Circle c1, Circle c2) { return c1.r > c2.r || (c1.r == c2.r && c1.i < c2.i); });
Grid grid(cs, 1e9);
for(int i = 0; i<n; i++) {
if(ans[cs[i].i] != -1) continue;
while(grid.cell_size > 2*cs[i].r) {
grid = Grid(cs, grid.cell_size/2);
}
vec<Circle> near = grid.query(cs[i]);
for(Circle& c : near) {
if(ans[c.i] != -1) continue;
if(intersect(cs[i], c)) {
ans[c.i] = cs[i].i;
}
}
}
for(int i = 0; i<n; i++) {
assert(ans[i] != -1);
cout << ans[i]+1 << ' ';
}
cout << '\n';
}
Compilation message
circle_selection.cpp: In constructor 'Grid::Grid(std::vector<Circle>, long long int)':
circle_selection.cpp:26:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<Circle>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 1; i<idata.size(); i++) {
| ~^~~~~~~~~~~~~
circle_selection.cpp: In member function 'std::vector<Circle> Grid::query(Circle)':
circle_selection.cpp:45:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<Circle> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = it-data.begin(); i < data.size() && data[i][0].x / cell_size <= cx + cell_span; i++) {
| ~~^~~~~~~~~~~~~
circle_selection.cpp:48:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<Circle>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(; j < data[i].size() && data[i][j].y / cell_size <= cy + cell_span; j++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
1240 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
7 ms |
1464 KB |
Output is correct |
22 |
Correct |
21 ms |
2056 KB |
Output is correct |
23 |
Correct |
21 ms |
1956 KB |
Output is correct |
24 |
Correct |
22 ms |
2036 KB |
Output is correct |
25 |
Correct |
27 ms |
2040 KB |
Output is correct |
26 |
Correct |
23 ms |
1956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
58664 KB |
Output is correct |
2 |
Correct |
252 ms |
58172 KB |
Output is correct |
3 |
Correct |
171 ms |
67708 KB |
Output is correct |
4 |
Correct |
204 ms |
49848 KB |
Output is correct |
5 |
Correct |
997 ms |
70904 KB |
Output is correct |
6 |
Correct |
1206 ms |
86836 KB |
Output is correct |
7 |
Correct |
1083 ms |
77772 KB |
Output is correct |
8 |
Correct |
1151 ms |
81528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
563 ms |
28092 KB |
Output is correct |
3 |
Correct |
1587 ms |
103780 KB |
Output is correct |
4 |
Correct |
1625 ms |
101116 KB |
Output is correct |
5 |
Correct |
1545 ms |
95748 KB |
Output is correct |
6 |
Correct |
463 ms |
34980 KB |
Output is correct |
7 |
Correct |
231 ms |
19588 KB |
Output is correct |
8 |
Correct |
52 ms |
5004 KB |
Output is correct |
9 |
Correct |
1676 ms |
95132 KB |
Output is correct |
10 |
Correct |
848 ms |
60584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
913 ms |
59800 KB |
Output is correct |
2 |
Correct |
1477 ms |
91960 KB |
Output is correct |
3 |
Correct |
460 ms |
66944 KB |
Output is correct |
4 |
Correct |
1124 ms |
77896 KB |
Output is correct |
5 |
Correct |
1161 ms |
88512 KB |
Output is correct |
6 |
Correct |
441 ms |
67048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
1240 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
7 ms |
1464 KB |
Output is correct |
22 |
Correct |
21 ms |
2056 KB |
Output is correct |
23 |
Correct |
21 ms |
1956 KB |
Output is correct |
24 |
Correct |
22 ms |
2036 KB |
Output is correct |
25 |
Correct |
27 ms |
2040 KB |
Output is correct |
26 |
Correct |
23 ms |
1956 KB |
Output is correct |
27 |
Correct |
15 ms |
2592 KB |
Output is correct |
28 |
Correct |
7 ms |
2592 KB |
Output is correct |
29 |
Correct |
6 ms |
2392 KB |
Output is correct |
30 |
Correct |
44 ms |
3684 KB |
Output is correct |
31 |
Correct |
42 ms |
3628 KB |
Output is correct |
32 |
Correct |
43 ms |
3348 KB |
Output is correct |
33 |
Correct |
50 ms |
17848 KB |
Output is correct |
34 |
Correct |
50 ms |
17864 KB |
Output is correct |
35 |
Correct |
273 ms |
22792 KB |
Output is correct |
36 |
Correct |
532 ms |
30216 KB |
Output is correct |
37 |
Correct |
530 ms |
29784 KB |
Output is correct |
38 |
Correct |
563 ms |
29084 KB |
Output is correct |
39 |
Correct |
414 ms |
24588 KB |
Output is correct |
40 |
Correct |
377 ms |
24584 KB |
Output is correct |
41 |
Correct |
419 ms |
24588 KB |
Output is correct |
42 |
Correct |
249 ms |
20548 KB |
Output is correct |
43 |
Correct |
308 ms |
27304 KB |
Output is correct |
44 |
Correct |
339 ms |
27548 KB |
Output is correct |
45 |
Correct |
329 ms |
27056 KB |
Output is correct |
46 |
Correct |
308 ms |
27708 KB |
Output is correct |
47 |
Correct |
344 ms |
26884 KB |
Output is correct |
48 |
Correct |
316 ms |
27524 KB |
Output is correct |
49 |
Correct |
319 ms |
27144 KB |
Output is correct |
50 |
Correct |
327 ms |
27660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
1240 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
7 ms |
1464 KB |
Output is correct |
22 |
Correct |
21 ms |
2056 KB |
Output is correct |
23 |
Correct |
21 ms |
1956 KB |
Output is correct |
24 |
Correct |
22 ms |
2036 KB |
Output is correct |
25 |
Correct |
27 ms |
2040 KB |
Output is correct |
26 |
Correct |
23 ms |
1956 KB |
Output is correct |
27 |
Correct |
214 ms |
58664 KB |
Output is correct |
28 |
Correct |
252 ms |
58172 KB |
Output is correct |
29 |
Correct |
171 ms |
67708 KB |
Output is correct |
30 |
Correct |
204 ms |
49848 KB |
Output is correct |
31 |
Correct |
997 ms |
70904 KB |
Output is correct |
32 |
Correct |
1206 ms |
86836 KB |
Output is correct |
33 |
Correct |
1083 ms |
77772 KB |
Output is correct |
34 |
Correct |
1151 ms |
81528 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
563 ms |
28092 KB |
Output is correct |
37 |
Correct |
1587 ms |
103780 KB |
Output is correct |
38 |
Correct |
1625 ms |
101116 KB |
Output is correct |
39 |
Correct |
1545 ms |
95748 KB |
Output is correct |
40 |
Correct |
463 ms |
34980 KB |
Output is correct |
41 |
Correct |
231 ms |
19588 KB |
Output is correct |
42 |
Correct |
52 ms |
5004 KB |
Output is correct |
43 |
Correct |
1676 ms |
95132 KB |
Output is correct |
44 |
Correct |
848 ms |
60584 KB |
Output is correct |
45 |
Correct |
913 ms |
59800 KB |
Output is correct |
46 |
Correct |
1477 ms |
91960 KB |
Output is correct |
47 |
Correct |
460 ms |
66944 KB |
Output is correct |
48 |
Correct |
1124 ms |
77896 KB |
Output is correct |
49 |
Correct |
1161 ms |
88512 KB |
Output is correct |
50 |
Correct |
441 ms |
67048 KB |
Output is correct |
51 |
Correct |
15 ms |
2592 KB |
Output is correct |
52 |
Correct |
7 ms |
2592 KB |
Output is correct |
53 |
Correct |
6 ms |
2392 KB |
Output is correct |
54 |
Correct |
44 ms |
3684 KB |
Output is correct |
55 |
Correct |
42 ms |
3628 KB |
Output is correct |
56 |
Correct |
43 ms |
3348 KB |
Output is correct |
57 |
Correct |
50 ms |
17848 KB |
Output is correct |
58 |
Correct |
50 ms |
17864 KB |
Output is correct |
59 |
Correct |
273 ms |
22792 KB |
Output is correct |
60 |
Correct |
532 ms |
30216 KB |
Output is correct |
61 |
Correct |
530 ms |
29784 KB |
Output is correct |
62 |
Correct |
563 ms |
29084 KB |
Output is correct |
63 |
Correct |
414 ms |
24588 KB |
Output is correct |
64 |
Correct |
377 ms |
24584 KB |
Output is correct |
65 |
Correct |
419 ms |
24588 KB |
Output is correct |
66 |
Correct |
249 ms |
20548 KB |
Output is correct |
67 |
Correct |
308 ms |
27304 KB |
Output is correct |
68 |
Correct |
339 ms |
27548 KB |
Output is correct |
69 |
Correct |
329 ms |
27056 KB |
Output is correct |
70 |
Correct |
308 ms |
27708 KB |
Output is correct |
71 |
Correct |
344 ms |
26884 KB |
Output is correct |
72 |
Correct |
316 ms |
27524 KB |
Output is correct |
73 |
Correct |
319 ms |
27144 KB |
Output is correct |
74 |
Correct |
327 ms |
27660 KB |
Output is correct |
75 |
Correct |
723 ms |
66056 KB |
Output is correct |
76 |
Correct |
414 ms |
66256 KB |
Output is correct |
77 |
Correct |
221 ms |
68392 KB |
Output is correct |
78 |
Correct |
153 ms |
56360 KB |
Output is correct |
79 |
Correct |
927 ms |
70608 KB |
Output is correct |
80 |
Correct |
147 ms |
56768 KB |
Output is correct |
81 |
Correct |
1584 ms |
94112 KB |
Output is correct |
82 |
Correct |
1627 ms |
100176 KB |
Output is correct |
83 |
Correct |
1669 ms |
101620 KB |
Output is correct |
84 |
Correct |
1674 ms |
97640 KB |
Output is correct |
85 |
Correct |
1643 ms |
99320 KB |
Output is correct |
86 |
Correct |
1710 ms |
95768 KB |
Output is correct |
87 |
Correct |
1576 ms |
101672 KB |
Output is correct |
88 |
Correct |
1334 ms |
79588 KB |
Output is correct |
89 |
Correct |
1292 ms |
79560 KB |
Output is correct |
90 |
Correct |
1301 ms |
79424 KB |
Output is correct |
91 |
Correct |
1270 ms |
79556 KB |
Output is correct |
92 |
Correct |
1399 ms |
79476 KB |
Output is correct |
93 |
Correct |
1505 ms |
98480 KB |
Output is correct |
94 |
Correct |
1251 ms |
82800 KB |
Output is correct |
95 |
Correct |
1459 ms |
101096 KB |
Output is correct |
96 |
Correct |
1445 ms |
97004 KB |
Output is correct |
97 |
Correct |
1332 ms |
67964 KB |
Output is correct |
98 |
Correct |
1336 ms |
91720 KB |
Output is correct |
99 |
Correct |
1515 ms |
96020 KB |
Output is correct |
100 |
Correct |
1601 ms |
99264 KB |
Output is correct |
101 |
Correct |
1348 ms |
92116 KB |
Output is correct |
102 |
Correct |
1425 ms |
99284 KB |
Output is correct |
103 |
Correct |
1357 ms |
67208 KB |
Output is correct |
104 |
Correct |
1562 ms |
98380 KB |
Output is correct |
105 |
Correct |
811 ms |
64420 KB |
Output is correct |
106 |
Correct |
899 ms |
69940 KB |
Output is correct |
107 |
Correct |
911 ms |
71324 KB |
Output is correct |
108 |
Correct |
894 ms |
66712 KB |
Output is correct |
109 |
Correct |
925 ms |
70300 KB |
Output is correct |
110 |
Correct |
896 ms |
69724 KB |
Output is correct |
111 |
Correct |
959 ms |
70784 KB |
Output is correct |
112 |
Correct |
962 ms |
66720 KB |
Output is correct |
113 |
Correct |
936 ms |
68640 KB |
Output is correct |
114 |
Correct |
957 ms |
67512 KB |
Output is correct |
115 |
Correct |
1010 ms |
71164 KB |
Output is correct |
116 |
Correct |
967 ms |
72904 KB |
Output is correct |