#pragma warning(disable:4996)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int n;
vector<vector<ll>> circle;
vector<pair<ll, int>> sortRadius, sortX, sortY;
int ans[301000];
int parentX[300100], parentY[300100];
//int depthX[300100], depthY[300100];
inline ll GetDist(ll x1, ll y1, ll x2, ll y2)
{
// 아 좌표값 마이너스도 들어오네
ll xx = max(x1, x2) - min(x1, x2);
ll yy = max(y1, y2) - min(y1, y2);
return (xx * xx) + (yy * yy);
}
bool comp(const pair<ll, int>& a, const pair<ll, int>& b)
{
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
}
bool compSort(const pair<ll, int>& a, const pair<ll, int>& b)
{
if (a.first == b.first)
return a.second < b.second;
return a.first > b.first;
}
int find(int a, int parent[])
{
if (parent[a] != a)
parent[a] = find(parent[a], parent);
return parent[a];
}
void merge(int a, int b, int parent[])
{
a = find(a, parent), b = find(b, parent);
if (a > b) swap(a, b);//b가더커야함
parent[a] = b;//더큰걸부모로
}
int main()
{
ll x, y, r, r2;
int i;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%lld %lld %lld", &x, &y, &r);
//x = 1, y = 1, r = 3;
circle.push_back({ x,y,r });
sortRadius.push_back({ r,i });
sortX.push_back({ x,i });
sortY.push_back({ y,i });
parentX[i] = parentY[i] = i;
//depthX[i] = depthY[i] = 1;
}
sort(sortRadius.begin(), sortRadius.end(), compSort);
sort(sortX.begin(), sortX.end());
sort(sortY.begin(), sortY.end());
int cur, curcmp;
int xMin, xMax, yMin, yMax;
int next;
ll rr;
fill(ans, ans + n + 2, -1);
for (int curDelete = 0; curDelete < n; curDelete++)
{
cur = sortRadius[curDelete].second;
if (ans[cur] != -1)
continue;
x = circle[cur][0], y = circle[cur][1], r = circle[cur][2];
if (curDelete < n - 1)
r2 = r + sortRadius[curDelete + 1].first + 1;
else
r2 = r * 2 + 1;
// a이하를 찾아야하는데 lower bound는 a이상인걸 찾으니까.. 근데 그 이하인걸 찾아야하니 1빼줌
xMin = lower_bound(sortX.begin(), sortX.end(), pair<ll, int>(x - r2, -1), comp) - sortX.begin();
xMax = upper_bound(sortX.begin(), sortX.end(), pair<ll, int>(x + r2, 301000), comp) - sortX.begin();
yMin = lower_bound(sortY.begin(), sortY.end(), pair<ll, int>(y - r2, -1), comp) - sortY.begin();
yMax = upper_bound(sortY.begin(), sortY.end(), pair<ll, int>(y + r2, 301000), comp) - sortY.begin();
xMin--;
yMin--;
if (xMin < 0) xMin = 0;
if (yMin < 0) yMin = 0;
if (xMin >= sortX.size()) xMin = sortX.size() - 1;
if (yMin >= sortY.size()) yMin = sortY.size() - 1;
if (xMax >= sortX.size()) xMax = sortX.size() - 1;
if (yMax >= sortY.size()) yMax = sortY.size() - 1;
//xMin = 0, yMin = 0, xMax = n - 1, yMax = n - 1;
ans[cur] = cur;
//xIdx[cur]
if (xMax - xMin < yMax - yMin)
{
i = xMin;
while (i <= xMax)
{
curcmp = sortX[i].second;
next = find(i, parentX) + 1;
if (ans[curcmp] != -1)
{
i = next;
continue;
}
rr = r + circle[curcmp][2];
if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
{
ans[curcmp] = cur;
if (curcmp - 1>= 0 && ans[curcmp - 1] != -1)
merge(curcmp, curcmp - 1, parentX);
if (curcmp + 1 < n && ans[curcmp + 1] != -1)
merge(curcmp, curcmp + 1, parentX);
}
i = next;
}
}
else
{
i = yMin;
while (i <= yMax)
{
curcmp = sortY[i].second;
next = find(i, parentY) + 1;;
if (ans[curcmp] != -1)
{
i = next;
continue;
}
rr = r + circle[curcmp][2];
if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
{
ans[curcmp] = cur;
if (curcmp - 1 >= 0 && ans[curcmp - 1] != -1)
merge(curcmp, curcmp - 1, parentY);
if (curcmp + 1 < n && ans[curcmp + 1] != -1)
merge(curcmp, curcmp + 1, parentY);
}
i = next;
}
/*
for (int i = yMax; i >= yMin; i--)
{
curcmp = sortY[i].second;
if (ans[curcmp]!= -1) continue;
rr = r + circle[curcmp][2];
if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
{
ans[curcmp] = cur;
//sortY.erase(sortY.begin() + i);
}
}*/
}
}
for (int i = 0; i < n; i++)
printf("%d ", ans[i] + 1);
return 0;
}
Compilation message
circle_selection.cpp:1: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
1 | #pragma warning(disable:4996)
|
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | if (xMin >= sortX.size()) xMin = sortX.size() - 1;
| ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:118:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | if (yMin >= sortY.size()) yMin = sortY.size() - 1;
| ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:119:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
119 | if (xMax >= sortX.size()) xMax = sortX.size() - 1;
| ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:120:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | if (yMax >= sortY.size()) yMax = sortY.size() - 1;
| ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
circle_selection.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%lld %lld %lld", &x, &y, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
210 ms |
43212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
211 ms |
14972 KB |
Output is correct |
3 |
Correct |
1107 ms |
44456 KB |
Output is correct |
4 |
Correct |
1107 ms |
44476 KB |
Output is correct |
5 |
Incorrect |
1489 ms |
43404 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
688 ms |
44176 KB |
Output is correct |
2 |
Correct |
524 ms |
43408 KB |
Output is correct |
3 |
Incorrect |
1748 ms |
44328 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |