답안 #774269

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
774269 2023-07-05T13:33:05 Z xetrk 원 고르기 (APIO18_circle_selection) C++17
87 / 100
3000 ms 55804 KB
#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];
int xIdx[300100], yIdx[300100];

int xCnt[1050000], yCnt[1050000];//세그먼트 트리

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 initSeg(int s, int e, int node, int tree[])
{
    if (s == e) return tree[node] = 1;
    int mid = (s + e) / 2;
    return tree[node] = initSeg(s, mid, node * 2, tree) + initSeg(mid + 1, e, node * 2 + 1, tree);
}

int sumSeg(int s, int e, int node, int left, int right, int tree[])
{
    if (left > e || right < s) return 0;

    //범위안에있으면
    if (left <= s && e <= right) return tree[node];

    int mid = (s + e) / 2;
    return sumSeg(s, mid, node * 2, left, right, tree) + sumSeg(mid + 1, e, node * 2 + 1, left, right, tree);
}

void minusSeg(int s, int e, int node, int index, int tree[])
{
    if (index<s || index>e) return;

    tree[node]--;
    if (s == e) return;
    int mid = (s + e) / 2;
    minusSeg(s, mid, node * 2, index, tree);
    minusSeg(mid + 1, e, node * 2 + 1, index, tree);
}


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());


    for (int i = 0; i < n; i++)
    {
        xIdx[sortX[i].second] = i;
        yIdx[sortY[i].second] = i;
    }

    initSeg(0, n - 1, 1, xCnt);
    initSeg(0, n - 1, 1, yCnt);

    int cur, curcmp;
    int xMin, xMax, yMin, yMax;
    int next;
    ll rr;

    int j;


    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;


        j = xIdx[cur];

        if (j - 1 >= 0 && ans[sortX[j - 1].second] != -1)
            merge(j, j - 1, parentX);
        if (j + 1 < n && ans[sortX[j + 1].second] != -1)
            merge(j, j + 1, parentX);
        minusSeg(0, n - 1, 1, j, xCnt);

        j = yIdx[cur];

        if (j - 1 >= 0 && ans[sortY[j - 1].second] != -1)
            merge(j, j - 1, parentY);
        if (j + 1 < n && ans[sortY[j + 1].second] != -1)
            merge(j, j + 1, parentY);
        minusSeg(0, n - 1, 1, j, yCnt);

        //xIdx[cur]

        int xRealCnt = sumSeg(0, n - 1, 1, xMin, xMax, xCnt);
        int yRealCnt = sumSeg(0, n - 1, 1, yMin, yMax, yCnt);



        if (xRealCnt < yRealCnt)
        {
            i = xMin;
            while (i <= xMax)
            {
                curcmp = sortX[i].second;
                
                if (ans[curcmp] != -1)
                {
                    i = find(i, parentX) + 1;
                    continue;
                }
                rr = r + circle[curcmp][2];
                if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
                {
                    ans[curcmp] = cur;
                    minusSeg(0, n - 1, 1, i, xCnt);

                    if (i - 1 >= 0 && ans[sortX[i - 1].second] != -1)
                        merge(i, i - 1, parentX);
                    if (i + 1 < n && ans[sortX[i + 1].second] != -1)
                        merge(i, i + 1, parentX);

                    j = yIdx[curcmp];
                    minusSeg(0, n - 1, 1, j, yCnt);

                    if (j - 1 >= 0 && ans[sortY[j - 1].second] != -1)
                        merge(j, j - 1, parentY);
                    if (j + 1 < n && ans[sortY[j + 1].second] != -1)
                        merge(j, j + 1, parentY);
                   
                }
                i = find(i, parentX) + 1;
            }
        }
        else
        {
            i = yMin;
            while (i <= yMax)
            {
                curcmp = sortY[i].second;
                if (ans[curcmp] != -1)
                {
                    i = find(i, parentY) + 1;
                    continue;
                }
                rr = r + circle[curcmp][2];
                if (GetDist(x, y, circle[curcmp][0], circle[curcmp][1]) <= rr * rr)
                {
                    ans[curcmp] = cur;
                    minusSeg(0, n - 1, 1, i, yCnt);

                    if (i - 1 >= 0 && ans[sortY[i - 1].second] != -1)
                        merge(i, i - 1, parentY);
                    if (i + 1 < n && ans[sortY[i + 1].second] != -1)
                        merge(i, i + 1, parentY);


                    j = xIdx[curcmp];
                    minusSeg(0, n - 1, 1, j, xCnt);

                    if (j - 1 >= 0 && ans[sortX[j - 1].second] != -1)
                        merge(j, j - 1, parentX);
                    if (j + 1 < n && ans[sortX[j + 1].second] != -1)
                        merge(j, j + 1, parentX);

                }
                i = find(i, parentY) + 1;
            }
            /*
            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:163: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]
  163 |         if (xMin >= sortX.size()) xMin = sortX.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:164: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]
  164 |         if (yMin >= sortY.size()) yMin = sortY.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:165: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]
  165 |         if (xMax >= sortX.size()) xMax = sortX.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:166: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]
  166 |         if (yMax >= sortY.size()) yMax = sortY.size() - 1;
      |             ~~~~~^~~~~~~~~~~~~~~
circle_selection.cpp:129:9: warning: unused variable 'next' [-Wunused-variable]
  129 |     int next;
      |         ^~~~
circle_selection.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
circle_selection.cpp:103:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         scanf("%lld %lld %lld", &x, &y, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 7 ms 1492 KB Output is correct
20 Correct 7 ms 1448 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 8 ms 1468 KB Output is correct
23 Correct 9 ms 1364 KB Output is correct
24 Correct 8 ms 1468 KB Output is correct
25 Correct 10 ms 1348 KB Output is correct
26 Correct 9 ms 1512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 303 ms 53160 KB Output is correct
2 Correct 379 ms 53128 KB Output is correct
3 Correct 304 ms 52960 KB Output is correct
4 Correct 310 ms 53116 KB Output is correct
5 Correct 420 ms 51412 KB Output is correct
6 Correct 525 ms 51516 KB Output is correct
7 Correct 467 ms 51720 KB Output is correct
8 Correct 420 ms 51520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 307 ms 17828 KB Output is correct
3 Correct 1490 ms 53000 KB Output is correct
4 Correct 1441 ms 53008 KB Output is correct
5 Correct 1820 ms 52060 KB Output is correct
6 Correct 497 ms 28648 KB Output is correct
7 Correct 149 ms 15060 KB Output is correct
8 Correct 23 ms 3400 KB Output is correct
9 Correct 1513 ms 53524 KB Output is correct
10 Correct 2343 ms 53796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 983 ms 53052 KB Output is correct
2 Correct 806 ms 53008 KB Output is correct
3 Correct 1264 ms 52908 KB Output is correct
4 Correct 858 ms 53164 KB Output is correct
5 Correct 830 ms 53008 KB Output is correct
6 Correct 661 ms 53284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 7 ms 1492 KB Output is correct
20 Correct 7 ms 1448 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 8 ms 1468 KB Output is correct
23 Correct 9 ms 1364 KB Output is correct
24 Correct 8 ms 1468 KB Output is correct
25 Correct 10 ms 1348 KB Output is correct
26 Correct 9 ms 1512 KB Output is correct
27 Correct 9 ms 2312 KB Output is correct
28 Correct 9 ms 2308 KB Output is correct
29 Correct 11 ms 2272 KB Output is correct
30 Correct 16 ms 2232 KB Output is correct
31 Correct 17 ms 2256 KB Output is correct
32 Correct 16 ms 2292 KB Output is correct
33 Correct 107 ms 18160 KB Output is correct
34 Correct 105 ms 18212 KB Output is correct
35 Correct 107 ms 18096 KB Output is correct
36 Correct 231 ms 17732 KB Output is correct
37 Correct 211 ms 17688 KB Output is correct
38 Correct 228 ms 17788 KB Output is correct
39 Correct 555 ms 16136 KB Output is correct
40 Correct 505 ms 16204 KB Output is correct
41 Correct 527 ms 16068 KB Output is correct
42 Correct 276 ms 16608 KB Output is correct
43 Correct 186 ms 17656 KB Output is correct
44 Correct 177 ms 17676 KB Output is correct
45 Correct 194 ms 17660 KB Output is correct
46 Correct 179 ms 17652 KB Output is correct
47 Correct 209 ms 17660 KB Output is correct
48 Correct 218 ms 17716 KB Output is correct
49 Correct 188 ms 17628 KB Output is correct
50 Correct 185 ms 17636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 7 ms 1492 KB Output is correct
20 Correct 7 ms 1448 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 8 ms 1468 KB Output is correct
23 Correct 9 ms 1364 KB Output is correct
24 Correct 8 ms 1468 KB Output is correct
25 Correct 10 ms 1348 KB Output is correct
26 Correct 9 ms 1512 KB Output is correct
27 Correct 303 ms 53160 KB Output is correct
28 Correct 379 ms 53128 KB Output is correct
29 Correct 304 ms 52960 KB Output is correct
30 Correct 310 ms 53116 KB Output is correct
31 Correct 420 ms 51412 KB Output is correct
32 Correct 525 ms 51516 KB Output is correct
33 Correct 467 ms 51720 KB Output is correct
34 Correct 420 ms 51520 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 307 ms 17828 KB Output is correct
37 Correct 1490 ms 53000 KB Output is correct
38 Correct 1441 ms 53008 KB Output is correct
39 Correct 1820 ms 52060 KB Output is correct
40 Correct 497 ms 28648 KB Output is correct
41 Correct 149 ms 15060 KB Output is correct
42 Correct 23 ms 3400 KB Output is correct
43 Correct 1513 ms 53524 KB Output is correct
44 Correct 2343 ms 53796 KB Output is correct
45 Correct 983 ms 53052 KB Output is correct
46 Correct 806 ms 53008 KB Output is correct
47 Correct 1264 ms 52908 KB Output is correct
48 Correct 858 ms 53164 KB Output is correct
49 Correct 830 ms 53008 KB Output is correct
50 Correct 661 ms 53284 KB Output is correct
51 Correct 9 ms 2312 KB Output is correct
52 Correct 9 ms 2308 KB Output is correct
53 Correct 11 ms 2272 KB Output is correct
54 Correct 16 ms 2232 KB Output is correct
55 Correct 17 ms 2256 KB Output is correct
56 Correct 16 ms 2292 KB Output is correct
57 Correct 107 ms 18160 KB Output is correct
58 Correct 105 ms 18212 KB Output is correct
59 Correct 107 ms 18096 KB Output is correct
60 Correct 231 ms 17732 KB Output is correct
61 Correct 211 ms 17688 KB Output is correct
62 Correct 228 ms 17788 KB Output is correct
63 Correct 555 ms 16136 KB Output is correct
64 Correct 505 ms 16204 KB Output is correct
65 Correct 527 ms 16068 KB Output is correct
66 Correct 276 ms 16608 KB Output is correct
67 Correct 186 ms 17656 KB Output is correct
68 Correct 177 ms 17676 KB Output is correct
69 Correct 194 ms 17660 KB Output is correct
70 Correct 179 ms 17652 KB Output is correct
71 Correct 209 ms 17660 KB Output is correct
72 Correct 218 ms 17716 KB Output is correct
73 Correct 188 ms 17628 KB Output is correct
74 Correct 185 ms 17636 KB Output is correct
75 Correct 405 ms 54628 KB Output is correct
76 Correct 410 ms 55804 KB Output is correct
77 Correct 381 ms 54448 KB Output is correct
78 Correct 445 ms 54288 KB Output is correct
79 Correct 508 ms 54088 KB Output is correct
80 Correct 413 ms 54220 KB Output is correct
81 Correct 1402 ms 53340 KB Output is correct
82 Correct 1285 ms 53180 KB Output is correct
83 Correct 1241 ms 53132 KB Output is correct
84 Correct 1872 ms 53756 KB Output is correct
85 Correct 1235 ms 53184 KB Output is correct
86 Correct 1162 ms 53312 KB Output is correct
87 Correct 1449 ms 53336 KB Output is correct
88 Execution timed out 3048 ms 48300 KB Time limit exceeded
89 Halted 0 ms 0 KB -