Submission #272852

# Submission time Handle Problem Language Result Execution time Memory
272852 2020-08-18T16:18:15 Z MKopchev Circle selection (APIO18_circle_selection) C++14
100 / 100
2391 ms 269132 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize ("unroll-loops")

#include<bits/stdc++.h>
using namespace std;
const int nmax=3e5+42,inf=1e9+42,MX=(1<<20)+42;

struct circle
{
    int x,y,r,id;
};

int n;
circle inp[nmax],sorted_inp[nmax],other_sorted[nmax];

bool touch(circle a,circle b)
{
    long long d=1LL*(a.x-b.x)*(a.x-b.x)+1LL*(a.y-b.y)*(a.y-b.y);

    long long mx=a.r+b.r;

    mx=mx*mx;

    return d<=mx;
}

bool cmp(circle a,circle b)
{
    if(a.r!=b.r)return a.r>b.r;
    return a.id<b.id;
}

bool cmp_y(circle a,circle b)
{
    if(a.y!=b.y)return a.y<b.y;
    return a.id<b.id;
}
int outp[nmax];

vector< pair<int/*y*/,int/*id*/> > nodes[MX];
vector<int> prv[MX];
vector<int> nxt[MX];

void update(int node,int l,int r,int pos,circle cur)
{
    //cout<<node<<" -> "<<cur.y<<" "<<cur.id<<endl;
    nodes[node].push_back({cur.y,cur.id});

    if(l==r)return;

    int av=(l+r)/2;

    if(pos<=av)update(node*2,l,av,pos,cur);
    else update(node*2+1,av+1,r,pos,cur);
}

int find_first(int where,int start)
{
    if(start>=nodes[where].size())return -1;

    if(nxt[where][start]==-1)return -1;

    if(outp[nodes[where][start].second]==0)return start;

    nxt[where][start]=find_first(where,nxt[where][start]);

    return nxt[where][start];
}

int pick(int a,int b)
{
    long long sum=a;
    sum=sum+b;

    if(inf<sum)sum=inf;
    if(-inf>sum)sum=-inf;

    return sum;
}

void match(int node,int l,int r,int lq,int rq,circle cur)
{
    if(l==lq&&r==rq)
    {
        int y1=pick(cur.y,-2*cur.r);
        int y2=pick(cur.y,2*cur.r);

        int start=lower_bound(nodes[node].begin(),nodes[node].end(),make_pair(y1,0))-nodes[node].begin();

        if(start==nodes[node].size())return;
        /*
        cout<<"circle "<<l<<" "<<r<<" "<<cur.id<<endl;

        for(auto k:nodes[node])
            cout<<k.first<<" "<<k.second<<"\t";cout<<endl;

        cout<<"y1= "<<y1<<" y2= "<<y2<<" start= "<<start<<endl;
        */
        if(start==-1)return;

        vector<int> to_pop={};

        while(start<nodes[node].size()&&nodes[node][start].first<=y2)
        {
            //cout<<"start= "<<start<<" "<<nodes[node][start].second<<" "<<inp[nodes[node][start].second].x<<" "<<inp[nodes[node][start].second].y<<" "<<inp[nodes[node][start].second].r<<endl;

            if(outp[nodes[node][start].second]==0&&touch(inp[nodes[node][start].second],cur))
            {
                //cout<<"touch "<<nodes[node][start].second<<" "<<cur.id<<" "<<endl;

                outp[nodes[node][start].second]=cur.id;
            }

            if(outp[nodes[node][start].second])to_pop.push_back(start);

            start=nxt[node][start];
        }

        for(auto k:to_pop)
        {
            if(0<=prv[node][k])
            {
                nxt[node][prv[node][k]]=nxt[node][k];
            }
            if(nxt[node][k]<nodes[node].size())
            {
                prv[node][nxt[node][k]]=prv[node][k];
            }
        }

        return;
    }

    int av=(l+r)/2;

    if(lq<=av)match(node*2,l,av,lq,min(av,rq),cur);
    if(av<rq)match(node*2+1,av+1,r,max(av+1,lq),rq,cur);
}

int help_x[nmax];

void push(int which)
{
    //outp[sorted_inp[which].id]=sorted_inp[which].id;

    int x1=pick(sorted_inp[which].x,-2*sorted_inp[which].r);
    int x2=pick(sorted_inp[which].x,+2*sorted_inp[which].r);

    int le=lower_bound(help_x+1,help_x+n+1,x1)-help_x;
    int ri=upper_bound(help_x+1,help_x+n+1,x2)-help_x;
    ri--;

    //cout<<"le= "<<le<<" ri= "<<ri<<endl;

    if(le>ri)return;

    match(1,1,n,le,ri,sorted_inp[which]);
}
signed main()
{
    scanf("%i",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%i%i%i",&inp[i].x,&inp[i].y,&inp[i].r);
        inp[i].id=i;
    }

    for(int i=1;i<=n;i++)sorted_inp[i]=inp[i],other_sorted[i]=inp[i];

    sort(other_sorted+1,other_sorted+n+1,cmp_y);

    sort(sorted_inp+1,sorted_inp+n+1,cmp);

    for(int i=1;i<=n;i++)help_x[i]=sorted_inp[i].x;

    sort(help_x+1,help_x+n+1);

    for(int i=1;i<=n;i++)
    {
        int pos=lower_bound(help_x+1,help_x+n+1,other_sorted[i].x)-help_x;

        update(1,1,n,pos,other_sorted[i]);
    }
    for(int i=1;i<MX;i++)
        if(nodes[i].size())
        {
            for(int j=0;j<nodes[i].size();j++)
            {
                prv[i].push_back(j-1);
                nxt[i].push_back(j+1);
            }
        }

    for(int i=1;i<=n;i++)
        if(outp[sorted_inp[i].id]==0)
            push(i);

    for(int i=1;i<=n;i++)printf("%i ",outp[i]);printf("\n");
    return 0;
}

Compilation message

circle_selection.cpp: In function 'int find_first(int, int)':
circle_selection.cpp:60:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     if(start>=nodes[where].size())return -1;
      |        ~~~~~^~~~~~~~~~~~~~~~~~~~~
circle_selection.cpp: In function 'void match(int, int, int, int, int, circle)':
circle_selection.cpp:91:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         if(start==nodes[node].size())return;
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~
circle_selection.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         while(start<nodes[node].size()&&nodes[node][start].first<=y2)
      |               ~~~~~^~~~~~~~~~~~~~~~~~~
circle_selection.cpp:126:28: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |             if(nxt[node][k]<nodes[node].size())
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:188:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |             for(int j=0;j<nodes[i].size();j++)
      |                         ~^~~~~~~~~~~~~~~~
circle_selection.cpp:199:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  199 |     for(int i=1;i<=n;i++)printf("%i ",outp[i]);printf("\n");
      |     ^~~
circle_selection.cpp:199:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  199 |     for(int i=1;i<=n;i++)printf("%i ",outp[i]);printf("\n");
      |                                                ^~~~~~
circle_selection.cpp:162:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  162 |     scanf("%i",&n);
      |     ~~~~~^~~~~~~~~
circle_selection.cpp:165:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  165 |         scanf("%i%i%i",&inp[i].x,&inp[i].y,&inp[i].r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 55 ms 74232 KB Output is correct
2 Correct 54 ms 74232 KB Output is correct
3 Correct 54 ms 74240 KB Output is correct
4 Correct 55 ms 74360 KB Output is correct
5 Correct 61 ms 74232 KB Output is correct
6 Correct 61 ms 74232 KB Output is correct
7 Correct 58 ms 74252 KB Output is correct
8 Correct 54 ms 74232 KB Output is correct
9 Correct 57 ms 74232 KB Output is correct
10 Correct 57 ms 74232 KB Output is correct
11 Correct 54 ms 74232 KB Output is correct
12 Correct 54 ms 74232 KB Output is correct
13 Correct 56 ms 74232 KB Output is correct
14 Correct 55 ms 74232 KB Output is correct
15 Correct 55 ms 74232 KB Output is correct
16 Correct 58 ms 74616 KB Output is correct
17 Correct 58 ms 74616 KB Output is correct
18 Correct 59 ms 74616 KB Output is correct
19 Correct 68 ms 76920 KB Output is correct
20 Correct 74 ms 76920 KB Output is correct
21 Correct 66 ms 76920 KB Output is correct
22 Correct 70 ms 76920 KB Output is correct
23 Correct 69 ms 76844 KB Output is correct
24 Correct 70 ms 76920 KB Output is correct
25 Correct 69 ms 76792 KB Output is correct
26 Correct 70 ms 76920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1190 ms 263240 KB Output is correct
2 Correct 1198 ms 263756 KB Output is correct
3 Correct 1235 ms 263960 KB Output is correct
4 Correct 1222 ms 262988 KB Output is correct
5 Correct 1195 ms 251208 KB Output is correct
6 Correct 1413 ms 261960 KB Output is correct
7 Correct 1250 ms 260112 KB Output is correct
8 Correct 1315 ms 260932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 74232 KB Output is correct
2 Correct 648 ms 127772 KB Output is correct
3 Correct 2021 ms 262136 KB Output is correct
4 Correct 1910 ms 262448 KB Output is correct
5 Correct 1736 ms 256900 KB Output is correct
6 Correct 837 ms 168440 KB Output is correct
7 Correct 404 ms 121572 KB Output is correct
8 Correct 124 ms 83828 KB Output is correct
9 Correct 1893 ms 259684 KB Output is correct
10 Correct 1837 ms 261228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1799 ms 261952 KB Output is correct
2 Correct 1507 ms 261316 KB Output is correct
3 Correct 1364 ms 260872 KB Output is correct
4 Correct 1542 ms 261264 KB Output is correct
5 Correct 1569 ms 261256 KB Output is correct
6 Correct 1246 ms 260688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 74232 KB Output is correct
2 Correct 54 ms 74232 KB Output is correct
3 Correct 54 ms 74240 KB Output is correct
4 Correct 55 ms 74360 KB Output is correct
5 Correct 61 ms 74232 KB Output is correct
6 Correct 61 ms 74232 KB Output is correct
7 Correct 58 ms 74252 KB Output is correct
8 Correct 54 ms 74232 KB Output is correct
9 Correct 57 ms 74232 KB Output is correct
10 Correct 57 ms 74232 KB Output is correct
11 Correct 54 ms 74232 KB Output is correct
12 Correct 54 ms 74232 KB Output is correct
13 Correct 56 ms 74232 KB Output is correct
14 Correct 55 ms 74232 KB Output is correct
15 Correct 55 ms 74232 KB Output is correct
16 Correct 58 ms 74616 KB Output is correct
17 Correct 58 ms 74616 KB Output is correct
18 Correct 59 ms 74616 KB Output is correct
19 Correct 68 ms 76920 KB Output is correct
20 Correct 74 ms 76920 KB Output is correct
21 Correct 66 ms 76920 KB Output is correct
22 Correct 70 ms 76920 KB Output is correct
23 Correct 69 ms 76844 KB Output is correct
24 Correct 70 ms 76920 KB Output is correct
25 Correct 69 ms 76792 KB Output is correct
26 Correct 70 ms 76920 KB Output is correct
27 Correct 79 ms 79480 KB Output is correct
28 Correct 80 ms 79504 KB Output is correct
29 Correct 80 ms 79480 KB Output is correct
30 Correct 89 ms 79480 KB Output is correct
31 Correct 85 ms 79480 KB Output is correct
32 Correct 87 ms 79480 KB Output is correct
33 Correct 381 ms 127424 KB Output is correct
34 Correct 386 ms 127488 KB Output is correct
35 Correct 395 ms 127756 KB Output is correct
36 Correct 568 ms 126968 KB Output is correct
37 Correct 562 ms 127076 KB Output is correct
38 Correct 559 ms 127084 KB Output is correct
39 Correct 395 ms 116320 KB Output is correct
40 Correct 389 ms 116448 KB Output is correct
41 Correct 397 ms 117352 KB Output is correct
42 Correct 285 ms 115808 KB Output is correct
43 Correct 386 ms 129640 KB Output is correct
44 Correct 381 ms 129508 KB Output is correct
45 Correct 388 ms 129652 KB Output is correct
46 Correct 387 ms 129512 KB Output is correct
47 Correct 399 ms 129612 KB Output is correct
48 Correct 387 ms 129544 KB Output is correct
49 Correct 383 ms 129508 KB Output is correct
50 Correct 391 ms 129560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 74232 KB Output is correct
2 Correct 54 ms 74232 KB Output is correct
3 Correct 54 ms 74240 KB Output is correct
4 Correct 55 ms 74360 KB Output is correct
5 Correct 61 ms 74232 KB Output is correct
6 Correct 61 ms 74232 KB Output is correct
7 Correct 58 ms 74252 KB Output is correct
8 Correct 54 ms 74232 KB Output is correct
9 Correct 57 ms 74232 KB Output is correct
10 Correct 57 ms 74232 KB Output is correct
11 Correct 54 ms 74232 KB Output is correct
12 Correct 54 ms 74232 KB Output is correct
13 Correct 56 ms 74232 KB Output is correct
14 Correct 55 ms 74232 KB Output is correct
15 Correct 55 ms 74232 KB Output is correct
16 Correct 58 ms 74616 KB Output is correct
17 Correct 58 ms 74616 KB Output is correct
18 Correct 59 ms 74616 KB Output is correct
19 Correct 68 ms 76920 KB Output is correct
20 Correct 74 ms 76920 KB Output is correct
21 Correct 66 ms 76920 KB Output is correct
22 Correct 70 ms 76920 KB Output is correct
23 Correct 69 ms 76844 KB Output is correct
24 Correct 70 ms 76920 KB Output is correct
25 Correct 69 ms 76792 KB Output is correct
26 Correct 70 ms 76920 KB Output is correct
27 Correct 1190 ms 263240 KB Output is correct
28 Correct 1198 ms 263756 KB Output is correct
29 Correct 1235 ms 263960 KB Output is correct
30 Correct 1222 ms 262988 KB Output is correct
31 Correct 1195 ms 251208 KB Output is correct
32 Correct 1413 ms 261960 KB Output is correct
33 Correct 1250 ms 260112 KB Output is correct
34 Correct 1315 ms 260932 KB Output is correct
35 Correct 54 ms 74232 KB Output is correct
36 Correct 648 ms 127772 KB Output is correct
37 Correct 2021 ms 262136 KB Output is correct
38 Correct 1910 ms 262448 KB Output is correct
39 Correct 1736 ms 256900 KB Output is correct
40 Correct 837 ms 168440 KB Output is correct
41 Correct 404 ms 121572 KB Output is correct
42 Correct 124 ms 83828 KB Output is correct
43 Correct 1893 ms 259684 KB Output is correct
44 Correct 1837 ms 261228 KB Output is correct
45 Correct 1799 ms 261952 KB Output is correct
46 Correct 1507 ms 261316 KB Output is correct
47 Correct 1364 ms 260872 KB Output is correct
48 Correct 1542 ms 261264 KB Output is correct
49 Correct 1569 ms 261256 KB Output is correct
50 Correct 1246 ms 260688 KB Output is correct
51 Correct 79 ms 79480 KB Output is correct
52 Correct 80 ms 79504 KB Output is correct
53 Correct 80 ms 79480 KB Output is correct
54 Correct 89 ms 79480 KB Output is correct
55 Correct 85 ms 79480 KB Output is correct
56 Correct 87 ms 79480 KB Output is correct
57 Correct 381 ms 127424 KB Output is correct
58 Correct 386 ms 127488 KB Output is correct
59 Correct 395 ms 127756 KB Output is correct
60 Correct 568 ms 126968 KB Output is correct
61 Correct 562 ms 127076 KB Output is correct
62 Correct 559 ms 127084 KB Output is correct
63 Correct 395 ms 116320 KB Output is correct
64 Correct 389 ms 116448 KB Output is correct
65 Correct 397 ms 117352 KB Output is correct
66 Correct 285 ms 115808 KB Output is correct
67 Correct 386 ms 129640 KB Output is correct
68 Correct 381 ms 129508 KB Output is correct
69 Correct 388 ms 129652 KB Output is correct
70 Correct 387 ms 129512 KB Output is correct
71 Correct 399 ms 129612 KB Output is correct
72 Correct 387 ms 129544 KB Output is correct
73 Correct 383 ms 129508 KB Output is correct
74 Correct 391 ms 129560 KB Output is correct
75 Correct 1282 ms 265988 KB Output is correct
76 Correct 1291 ms 265296 KB Output is correct
77 Correct 1245 ms 265128 KB Output is correct
78 Correct 1269 ms 265196 KB Output is correct
79 Correct 1290 ms 263540 KB Output is correct
80 Correct 1244 ms 265792 KB Output is correct
81 Correct 1909 ms 262572 KB Output is correct
82 Correct 1809 ms 261996 KB Output is correct
83 Correct 1978 ms 261828 KB Output is correct
84 Correct 1979 ms 261496 KB Output is correct
85 Correct 1857 ms 262316 KB Output is correct
86 Correct 1798 ms 262224 KB Output is correct
87 Correct 2391 ms 261540 KB Output is correct
88 Correct 1393 ms 221900 KB Output is correct
89 Correct 1504 ms 222280 KB Output is correct
90 Correct 1425 ms 221828 KB Output is correct
91 Correct 1454 ms 221664 KB Output is correct
92 Correct 1467 ms 221788 KB Output is correct
93 Correct 1564 ms 268492 KB Output is correct
94 Correct 866 ms 204224 KB Output is correct
95 Correct 1613 ms 267984 KB Output is correct
96 Correct 1576 ms 268420 KB Output is correct
97 Correct 1625 ms 214768 KB Output is correct
98 Correct 1455 ms 269132 KB Output is correct
99 Correct 1616 ms 268668 KB Output is correct
100 Correct 1586 ms 268484 KB Output is correct
101 Correct 1578 ms 268096 KB Output is correct
102 Correct 1617 ms 268668 KB Output is correct
103 Correct 1596 ms 235008 KB Output is correct
104 Correct 1589 ms 268112 KB Output is correct
105 Correct 939 ms 222668 KB Output is correct
106 Correct 1150 ms 266236 KB Output is correct
107 Correct 1177 ms 266432 KB Output is correct
108 Correct 1214 ms 266476 KB Output is correct
109 Correct 1201 ms 266344 KB Output is correct
110 Correct 1175 ms 266424 KB Output is correct
111 Correct 1179 ms 266568 KB Output is correct
112 Correct 1148 ms 266540 KB Output is correct
113 Correct 1154 ms 266556 KB Output is correct
114 Correct 1182 ms 266544 KB Output is correct
115 Correct 1213 ms 266640 KB Output is correct
116 Correct 1117 ms 266548 KB Output is correct