#include <bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
struct Circle
{
int64_t x;
int64_t y;
int64_t r;
int64_t i;
int64_t elimBy;
int64_t minX() const { return x - r; }
int64_t maxX() const { return x + r; }
int64_t minY() const { return y - r; }
int64_t maxY() const { return y + r; }
bool intersects(const Circle& other) const
{
int64_t dx = x - other.x;
int64_t dy = y - other.y;
int64_t rSum = r + other.r;
return dx * dx + dy * dy <= rSum * rSum;
}
};
Circle* outerCircle;
struct OctNode
{
int64_t minX;
int64_t minY;
int64_t maxX;
int64_t maxY;
std::vector<Circle*> circles;
OctNode* children = nullptr;
bool intersects(int64_t x, int64_t y, int64_t r)
{
return !(x - r >= maxX || x + r <= minX || y - r >= maxY || y + r <= minY);
}
bool contains(int64_t x, int64_t y, int64_t r)
{
return x - r >= minX && x + r < maxX && y - r >= minY && y + r < maxY;
}
void mark()
{
if (children)
{
for (int i = 0; i < 4; i++)
{
if (children[i].intersects(outerCircle->x, outerCircle->y, outerCircle->r))
children[i].mark();
}
}
for (Circle* c : circles)
{
if (c->elimBy == -1 && c->intersects(*outerCircle))
{
c->elimBy = outerCircle->i;
}
}
}
};
void genOctree(OctNode* node)
{
int64_t halfSX = (node->maxX - node->minX) / 2;
int64_t halfSY = (node->maxY - node->minY) / 2;
int64_t midX = node->minX + halfSX;
int64_t midY = node->minY + halfSY;
bool subdivide = false;
for (Circle* circle : node->circles)
{
if (circle->maxX() < midX || circle->minX() > midX || circle->maxY() > midY || circle->minY() < midY)
{
subdivide = true;
break;
}
}
if (!subdivide)
return;
node->children = new OctNode[4];
for (int i = 0; i < 4; i++)
{
node->children[i].minX = (i % 2) ? midX : node->minX;
node->children[i].maxX = (i % 2) ? node->maxX : midX;
node->children[i].minY = (i / 2) ? midY : node->minY;
node->children[i].maxY = (i / 2) ? node->maxY : midY;
node->children[i].circles.reserve(node->circles.size());
for (int j = node->circles.size() - 1; j >= 0; j--)
{
if (node->contains(node->circles[j]->x, node->circles[j]->y, node->circles[j]->r))
{
node->children[i].circles.push_back(node->circles[j]);
node->circles[j] = node->circles.back();
node->circles.pop_back();
}
}
}
}
int main()
{
int num;
std::cin >> num;
std::vector<Circle> circles(num);
std::vector<Circle*> cByMin(num);
std::vector<Circle*> cByMax(num);
bool allZeroY = true;
int64_t sameRad = -1;
for (int i = 0; i < num; i++)
{
std::cin >> circles[i].x;
std::cin >> circles[i].y;
std::cin >> circles[i].r;
circles[i].i = i;
circles[i].elimBy = -1;
cByMin[i] = &circles[i];
cByMax[i] = &circles[i];
if (i == 0)
sameRad = circles[i].r;
else if (sameRad != circles[i].r)
sameRad = -1;
if (circles[i].y != 0)
allZeroY = false;
}
if (sameRad == -1)
{
std::sort(ALL(circles), [&] (const Circle& a, const Circle& b)
{
if (a.r == b.r)
return a.i < b.i;
return a.r > b.r;
});
}
if (allZeroY)
{
std::sort(ALL(cByMin), [&] (const Circle* a, const Circle* b)
{
return a->minX() < b->minX();
});
std::sort(ALL(cByMax), [&] (const Circle* a, const Circle* b)
{
return a->maxX() < b->maxX();
});
for (Circle& outer : circles)
{
if (outer.elimBy != -1)
continue;
outer.elimBy = outer.i;
auto minB = std::lower_bound(ALL(cByMin), outer.minX(), [&] (Circle* c, int64_t pos) { return c->minX() < pos; });
auto minE = std::upper_bound(ALL(cByMin), outer.maxX(), [&] (int64_t pos, Circle* c) { return pos < c->minX(); });
auto maxB = std::lower_bound(ALL(cByMax), outer.minX(), [&] (Circle* c, int64_t pos) { return c->maxX() < pos; });
auto maxE = std::upper_bound(ALL(cByMax), outer.maxX(), [&] (int64_t pos, Circle* c) { return pos <= c->maxX(); });
std::for_each(minB, minE, [&] (Circle* c)
{
if (c->elimBy == -1)
c->elimBy = outer.i;
});
std::for_each(maxB, maxE, [&] (Circle* c)
{
if (c->elimBy == -1)
c->elimBy = outer.i;
});
}
}
else if (num <= 5000)
{
for (Circle& outer : circles)
{
if (outer.elimBy != -1)
continue;
outer.elimBy = outer.i;
for (Circle& inner : circles)
{
if (inner.elimBy == -1 && inner.intersects(outer))
inner.elimBy = outer.i;
}
}
}
else
{
int64_t minX;
int64_t minY;
int64_t maxX;
int64_t maxY;
OctNode root;
root.circles.reserve(circles.size());
root.minX = INT64_MAX;
root.minY = INT64_MAX;
root.maxX = INT64_MIN;
root.maxY = INT64_MIN;
for (Circle& circle : circles)
{
root.circles.push_back(&circle);
root.minX = std::min(root.minX, circle.minX());
root.maxX = std::min(root.maxX, circle.maxX());
root.minY = std::min(root.minY, circle.minY());
root.maxY = std::min(root.maxY, circle.maxY());
}
root.maxX++;
root.maxY++;
genOctree(&root);
for (Circle& circle : circles)
{
if (circle.elimBy == -1)
{
circle.elimBy = circle.i;
outerCircle = &circle;
root.mark();
}
}
}
std::vector<int64_t> result(num);
for (int i = 0; i < num; i++)
result[circles[i].i] = circles[i].elimBy + 1;
for (int i = 0; i < num; i++)
std::cout << result[i] << " ";
std::cout << std::endl;
}
Compilation message
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:202:11: warning: unused variable 'minX' [-Wunused-variable]
int64_t minX;
^~~~
circle_selection.cpp:203:11: warning: unused variable 'minY' [-Wunused-variable]
int64_t minY;
^~~~
circle_selection.cpp:204:11: warning: unused variable 'maxX' [-Wunused-variable]
int64_t maxX;
^~~~
circle_selection.cpp:205:11: warning: unused variable 'maxY' [-Wunused-variable]
int64_t maxY;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
392 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
556 KB |
Output is correct |
6 |
Correct |
3 ms |
556 KB |
Output is correct |
7 |
Correct |
3 ms |
556 KB |
Output is correct |
8 |
Correct |
4 ms |
556 KB |
Output is correct |
9 |
Correct |
4 ms |
564 KB |
Output is correct |
10 |
Correct |
3 ms |
564 KB |
Output is correct |
11 |
Correct |
3 ms |
564 KB |
Output is correct |
12 |
Correct |
3 ms |
564 KB |
Output is correct |
13 |
Correct |
4 ms |
572 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
6 ms |
608 KB |
Output is correct |
17 |
Correct |
5 ms |
608 KB |
Output is correct |
18 |
Correct |
7 ms |
616 KB |
Output is correct |
19 |
Correct |
16 ms |
884 KB |
Output is correct |
20 |
Correct |
13 ms |
884 KB |
Output is correct |
21 |
Correct |
18 ms |
888 KB |
Output is correct |
22 |
Correct |
91 ms |
916 KB |
Output is correct |
23 |
Correct |
66 ms |
936 KB |
Output is correct |
24 |
Correct |
71 ms |
1048 KB |
Output is correct |
25 |
Correct |
68 ms |
1048 KB |
Output is correct |
26 |
Correct |
59 ms |
1048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
942 ms |
21460 KB |
Output is correct |
2 |
Correct |
787 ms |
21532 KB |
Output is correct |
3 |
Correct |
762 ms |
21532 KB |
Output is correct |
4 |
Correct |
800 ms |
21532 KB |
Output is correct |
5 |
Correct |
700 ms |
21532 KB |
Output is correct |
6 |
Correct |
985 ms |
21532 KB |
Output is correct |
7 |
Correct |
723 ms |
21532 KB |
Output is correct |
8 |
Correct |
837 ms |
21532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
21532 KB |
Output is correct |
2 |
Execution timed out |
3046 ms |
21532 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3023 ms |
21532 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
392 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
556 KB |
Output is correct |
6 |
Correct |
3 ms |
556 KB |
Output is correct |
7 |
Correct |
3 ms |
556 KB |
Output is correct |
8 |
Correct |
4 ms |
556 KB |
Output is correct |
9 |
Correct |
4 ms |
564 KB |
Output is correct |
10 |
Correct |
3 ms |
564 KB |
Output is correct |
11 |
Correct |
3 ms |
564 KB |
Output is correct |
12 |
Correct |
3 ms |
564 KB |
Output is correct |
13 |
Correct |
4 ms |
572 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
6 ms |
608 KB |
Output is correct |
17 |
Correct |
5 ms |
608 KB |
Output is correct |
18 |
Correct |
7 ms |
616 KB |
Output is correct |
19 |
Correct |
16 ms |
884 KB |
Output is correct |
20 |
Correct |
13 ms |
884 KB |
Output is correct |
21 |
Correct |
18 ms |
888 KB |
Output is correct |
22 |
Correct |
91 ms |
916 KB |
Output is correct |
23 |
Correct |
66 ms |
936 KB |
Output is correct |
24 |
Correct |
71 ms |
1048 KB |
Output is correct |
25 |
Correct |
68 ms |
1048 KB |
Output is correct |
26 |
Correct |
59 ms |
1048 KB |
Output is correct |
27 |
Correct |
33 ms |
21532 KB |
Output is correct |
28 |
Correct |
22 ms |
21532 KB |
Output is correct |
29 |
Correct |
24 ms |
21532 KB |
Output is correct |
30 |
Correct |
366 ms |
21532 KB |
Output is correct |
31 |
Correct |
310 ms |
21532 KB |
Output is correct |
32 |
Correct |
309 ms |
21532 KB |
Output is correct |
33 |
Correct |
326 ms |
21532 KB |
Output is correct |
34 |
Correct |
312 ms |
21532 KB |
Output is correct |
35 |
Correct |
280 ms |
21532 KB |
Output is correct |
36 |
Execution timed out |
3009 ms |
21532 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
392 KB |
Output is correct |
3 |
Correct |
2 ms |
420 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
556 KB |
Output is correct |
6 |
Correct |
3 ms |
556 KB |
Output is correct |
7 |
Correct |
3 ms |
556 KB |
Output is correct |
8 |
Correct |
4 ms |
556 KB |
Output is correct |
9 |
Correct |
4 ms |
564 KB |
Output is correct |
10 |
Correct |
3 ms |
564 KB |
Output is correct |
11 |
Correct |
3 ms |
564 KB |
Output is correct |
12 |
Correct |
3 ms |
564 KB |
Output is correct |
13 |
Correct |
4 ms |
572 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
6 ms |
608 KB |
Output is correct |
17 |
Correct |
5 ms |
608 KB |
Output is correct |
18 |
Correct |
7 ms |
616 KB |
Output is correct |
19 |
Correct |
16 ms |
884 KB |
Output is correct |
20 |
Correct |
13 ms |
884 KB |
Output is correct |
21 |
Correct |
18 ms |
888 KB |
Output is correct |
22 |
Correct |
91 ms |
916 KB |
Output is correct |
23 |
Correct |
66 ms |
936 KB |
Output is correct |
24 |
Correct |
71 ms |
1048 KB |
Output is correct |
25 |
Correct |
68 ms |
1048 KB |
Output is correct |
26 |
Correct |
59 ms |
1048 KB |
Output is correct |
27 |
Correct |
942 ms |
21460 KB |
Output is correct |
28 |
Correct |
787 ms |
21532 KB |
Output is correct |
29 |
Correct |
762 ms |
21532 KB |
Output is correct |
30 |
Correct |
800 ms |
21532 KB |
Output is correct |
31 |
Correct |
700 ms |
21532 KB |
Output is correct |
32 |
Correct |
985 ms |
21532 KB |
Output is correct |
33 |
Correct |
723 ms |
21532 KB |
Output is correct |
34 |
Correct |
837 ms |
21532 KB |
Output is correct |
35 |
Correct |
3 ms |
21532 KB |
Output is correct |
36 |
Execution timed out |
3046 ms |
21532 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |