Submission #499532

# Submission time Handle Problem Language Result Execution time Memory
499532 2021-12-28T14:54:48 Z _martynas Circle selection (APIO18_circle_selection) C++14
100 / 100
2609 ms 729968 KB
#pragma GCC optimize("O3,inline")
#include <bits/stdc++.h>

using namespace std;
using namespace std::chrono;

using ll = long long;

const ll MOD = 1e9+7;

const int MAX_N = 3e5+5;

template<typename T>
inline T clamp(T x, T mn, T mx)
{
    return x > mx ? mx : (x < mn ? mn : x);
}

int n;
struct Node
{
    ll x, y, r;
    int p;
    bool visited;
} A[MAX_N];

struct LLNode
{
    int idx;
    LLNode* next = nullptr;

    inline LLNode()
    {
    }

    inline LLNode(const int & val)
        : idx(val)
    {
    }
} LLNodes[MAX_N];
int llcounter = 0;

inline LLNode* getLLNode()
{
    return &LLNodes[llcounter++];
}

int dup;
int inRange;

struct Quad;
inline Quad* getQuadNode();

struct Quad
{
    ll l, r, bot, top;
    array<Quad*, 4> childs = {};
    LLNode* llnode = nullptr;
    int sz;

    inline Quad()
    {
    }

    inline Quad(const ll & _l, const ll & _r, const ll & _bot, const ll & _top)
        : l(_l), r(_r), bot(_bot), top(_top)
    {
        sz = 0;
    }

    void add(const ll & x, const ll & y, const int & idx)
    {
        if(x < l || x > r || y < bot || y > top) {
            return;
        }

        sz++;
//        cerr << "l = " << l << ", r = " << r << ", "
//             << "bot = " << bot << ", top = " << top << "\n";
        if(l == r && bot == top) {
            if(llnode == nullptr) {
                llnode = getLLNode();
                llnode->idx = idx;
            }
            else {
                dup++;
                LLNode* temp = getLLNode();
                temp->idx = idx;
                temp->next = llnode;
                llnode = temp;
            }
        }
        else {
            ll midH, midV;
            midH = l+(r-l)/2;
            midV = bot+(top-bot)/2;

            if(l != r && top != bot) {
                if(childs[0] == nullptr && !(x < l || x > midH || y < bot || y > midV)) {
                    childs[0] = getQuadNode();
                    childs[0]->l = l;
                    childs[0]->r = midH;
                    childs[0]->bot = bot;
                    childs[0]->top = midV;
                }
                else if(childs[1] == nullptr && !(x < midH+1 || x > r || y < bot || y > midV)) {
                    childs[1] = getQuadNode();
                    childs[1]->l = midH+1;
                    childs[1]->r = r;
                    childs[1]->bot = bot;
                    childs[1]->top = midV;
                }
                else if(childs[2] == nullptr && !(x < l || x > midH || y < midV+1 || y > top)) {
                    childs[2] = getQuadNode();
                    childs[2]->l = l;
                    childs[2]->r = midH;
                    childs[2]->bot = midV+1;
                    childs[2]->top = top;
                }
                else if(childs[3] == nullptr && !(x < midH+1 || x > r || y < midV+1 || y > top)) {
                    childs[3] = getQuadNode();
                    childs[3]->l = midH+1;
                    childs[3]->r = r;
                    childs[3]->bot = midV+1;
                    childs[3]->top = top;
                }
            }
            else if(l != r) {
                if(childs[0] == nullptr && !(x < l || x > midH || y < bot || y > top)) {
                    childs[0] = getQuadNode();
                    childs[0]->l = l;
                    childs[0]->r = midH;
                    childs[0]->bot = bot;
                    childs[0]->top = top;
                }
                else if(childs[1] == nullptr && !(x < midH+1 || x > r || y < bot || y > top)) {
                    childs[1] = getQuadNode();
                    childs[1]->l = midH+1;
                    childs[1]->r = r;
                    childs[1]->bot = bot;
                    childs[1]->top = top;
                }
            }
            else if(top != bot) {
                if(childs[0] == nullptr && !(x < l || x > r || y < bot || y > midV)) {
                    childs[0] = getQuadNode();
                    childs[0]->l = l;
                    childs[0]->r = r;
                    childs[0]->bot = bot;
                    childs[0]->top = midV;
                }
                else if(childs[1] == nullptr && !(x < l || x > r || y < midV+1 || y > top)) {
                    childs[1] = getQuadNode();
                    childs[1]->l = l;
                    childs[1]->r = r;
                    childs[1]->bot = midV+1;
                    childs[1]->top = top;
                }
            }

            for(int k = 0; k < 4; k++) {
                if(childs[k] != nullptr) {
                    childs[k]->add(x, y, idx);
                }
            }
        }
    }

    void setInRange(const ll & x, const ll & y, const ll & radius, const int & idx)
    {
        if(x+radius < l || x-radius > r || y+radius < bot || y-radius > top || sz == 0) {
            return;
        }
        bool circleIntersectsRectangle =
            (l <= x && x <= r && bot <= y && y <= top) // circle center inside rectangle
            || ((y-clamp(y, bot, top))*(y-clamp(y, bot, top))+(x-l)*(x-l)<=radius*radius) // circle intersects or covers left side
            || ((y-clamp(y, bot, top))*(y-clamp(y, bot, top))+(x-r)*(x-r)<=radius*radius) // circle intersects or covers right side
            || ((y-bot)*(y-bot)+(x-clamp(x, l, r))*(x-clamp(x, l, r))<=radius*radius) // circle intersects or covers bot side
            || ((y-top)*(y-top)+(x-clamp(x, l, r))*(x-clamp(x, l, r))<=radius*radius) // circle intersects or covers top side
            ;
        if(!circleIntersectsRectangle) {
            return;
        }

//        cerr << "l = " << l << ", r = " << r << ", "
//             << "bot = " << bot << ", top = " << top << "\n";
        if(l == r && bot == top) {
            inRange++;
            while(llnode != nullptr &&
                (A[idx].r+A[llnode->idx].r)*(A[idx].r+A[llnode->idx].r)
                >= llabs(A[idx].x-A[llnode->idx].x)*llabs(A[idx].x-A[llnode->idx].x)
                +llabs(A[idx].y-A[llnode->idx].y)*llabs(A[idx].y-A[llnode->idx].y)) {
                A[llnode->idx].p = idx;
                A[llnode->idx].visited = true;
                sz--;
                llnode = llnode->next;
            }
            return;
        }

        int cnt = 0;
        for(int k = 0; k < 4; k++) {
            if(childs[k] != nullptr) {
                childs[k]->setInRange(x, y, radius, idx);
                cnt += childs[k]->sz;
            }
        }
        sz = cnt;
    }
} QuadNodes[MAX_N*30];
int quadcounter = 0;
inline Quad* getQuadNode()
{
    return &QuadNodes[quadcounter++];
}

Quad Q(-1000000000, 1000000000, -1000000000, 1000000000);

inline int nextInt()
{
	int c = getchar();
	while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
	}
	int x = 0;
	bool neg = false;
	if (c == '-') {
		neg = true;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		x = x*10+(c-'0');
		c = getchar();
	}
	if (neg)
		return -x;
	return x;
}

int main()
{
    auto stop = steady_clock::now();

    n = nextInt();
    for(int i = 0; i < n; i++) {
        A[i].x = nextInt();
        A[i].y = nextInt();
        A[i].r = nextInt();
    }

    //cerr << "Exec: " << duration_cast<milliseconds>(steady_clock::now()-stop).count() << " ms\n";

    vector<int> I(n);
    iota(I.begin(), I.end(), 0);
    sort(I.begin(), I.end(),
    [&](int a, int b)
    {
        return A[a].r > A[b].r || (A[a].r == A[b].r && a < b);
    });

    for(int i = n-1; i >= 0; i--) {
        int idx = I[i];
        //cerr << i << " started\n";
        Q.add(A[idx].x, A[idx].y, idx);
        //cerr << i << " finished\n";
    }

    for(int i = 0; i < n; i++) {
        int idx = I[i];
        if(A[idx].visited) continue;
        Q.setInRange(A[idx].x, A[idx].y, A[idx].r+(i+1<n?A[I[i+1]].r:0), idx);
    }

    for(int i = 0; i < n; i++) {
        cout << 1+A[i].p << " ";
    }
    cout << "\n";

//    cerr << "Dupe: " << dup << "\n";
//    cerr << "In range: " << inRange << "\n";
//    cerr << "Exec: " << duration_cast<milliseconds>(steady_clock::now()-stop).count() << " ms\n";

    return 0;
}
/*
11
9 9 2
13 2 1
11 8 2
3 3 2
3 12 1
12 14 1
9 8 5
2 8 2
5 2 1
14 4 2
14 14 1
*/

Compilation message

circle_selection.cpp: In function 'int main()':
circle_selection.cpp:242:10: warning: variable 'stop' set but not used [-Wunused-but-set-variable]
  242 |     auto stop = steady_clock::now();
      |          ^~~~
# Verdict Execution time Memory Grader output
1 Correct 252 ms 709484 KB Output is correct
2 Correct 255 ms 709424 KB Output is correct
3 Correct 257 ms 709488 KB Output is correct
4 Correct 248 ms 709392 KB Output is correct
5 Correct 251 ms 709572 KB Output is correct
6 Correct 256 ms 709444 KB Output is correct
7 Correct 279 ms 709448 KB Output is correct
8 Correct 271 ms 709456 KB Output is correct
9 Correct 292 ms 709420 KB Output is correct
10 Correct 338 ms 709400 KB Output is correct
11 Correct 276 ms 709476 KB Output is correct
12 Correct 327 ms 709488 KB Output is correct
13 Correct 362 ms 709492 KB Output is correct
14 Correct 299 ms 709476 KB Output is correct
15 Correct 339 ms 709452 KB Output is correct
16 Correct 329 ms 709532 KB Output is correct
17 Correct 276 ms 709448 KB Output is correct
18 Correct 280 ms 709544 KB Output is correct
19 Correct 264 ms 709760 KB Output is correct
20 Correct 266 ms 709904 KB Output is correct
21 Correct 255 ms 709768 KB Output is correct
22 Correct 254 ms 709760 KB Output is correct
23 Correct 265 ms 709696 KB Output is correct
24 Correct 281 ms 709796 KB Output is correct
25 Correct 256 ms 709728 KB Output is correct
26 Correct 270 ms 709888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 958 ms 724552 KB Output is correct
2 Correct 1010 ms 724404 KB Output is correct
3 Correct 978 ms 724268 KB Output is correct
4 Correct 982 ms 724412 KB Output is correct
5 Correct 712 ms 724160 KB Output is correct
6 Correct 945 ms 724280 KB Output is correct
7 Correct 824 ms 724292 KB Output is correct
8 Correct 839 ms 724224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 709444 KB Output is correct
2 Correct 586 ms 715984 KB Output is correct
3 Correct 1395 ms 724428 KB Output is correct
4 Correct 1424 ms 724428 KB Output is correct
5 Correct 1363 ms 724196 KB Output is correct
6 Correct 736 ms 718356 KB Output is correct
7 Correct 489 ms 715076 KB Output is correct
8 Correct 298 ms 710540 KB Output is correct
9 Correct 1496 ms 724428 KB Output is correct
10 Correct 1342 ms 724548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1366 ms 724436 KB Output is correct
2 Correct 1243 ms 724436 KB Output is correct
3 Correct 1111 ms 724044 KB Output is correct
4 Correct 1309 ms 724420 KB Output is correct
5 Correct 1329 ms 724436 KB Output is correct
6 Correct 1077 ms 723732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 252 ms 709484 KB Output is correct
2 Correct 255 ms 709424 KB Output is correct
3 Correct 257 ms 709488 KB Output is correct
4 Correct 248 ms 709392 KB Output is correct
5 Correct 251 ms 709572 KB Output is correct
6 Correct 256 ms 709444 KB Output is correct
7 Correct 279 ms 709448 KB Output is correct
8 Correct 271 ms 709456 KB Output is correct
9 Correct 292 ms 709420 KB Output is correct
10 Correct 338 ms 709400 KB Output is correct
11 Correct 276 ms 709476 KB Output is correct
12 Correct 327 ms 709488 KB Output is correct
13 Correct 362 ms 709492 KB Output is correct
14 Correct 299 ms 709476 KB Output is correct
15 Correct 339 ms 709452 KB Output is correct
16 Correct 329 ms 709532 KB Output is correct
17 Correct 276 ms 709448 KB Output is correct
18 Correct 280 ms 709544 KB Output is correct
19 Correct 264 ms 709760 KB Output is correct
20 Correct 266 ms 709904 KB Output is correct
21 Correct 255 ms 709768 KB Output is correct
22 Correct 254 ms 709760 KB Output is correct
23 Correct 265 ms 709696 KB Output is correct
24 Correct 281 ms 709796 KB Output is correct
25 Correct 256 ms 709728 KB Output is correct
26 Correct 270 ms 709888 KB Output is correct
27 Correct 270 ms 710092 KB Output is correct
28 Correct 271 ms 710116 KB Output is correct
29 Correct 278 ms 710132 KB Output is correct
30 Correct 271 ms 710124 KB Output is correct
31 Correct 270 ms 710120 KB Output is correct
32 Correct 281 ms 710172 KB Output is correct
33 Correct 488 ms 715920 KB Output is correct
34 Correct 525 ms 715876 KB Output is correct
35 Correct 534 ms 715836 KB Output is correct
36 Correct 572 ms 715928 KB Output is correct
37 Correct 610 ms 715836 KB Output is correct
38 Correct 582 ms 715908 KB Output is correct
39 Correct 819 ms 714572 KB Output is correct
40 Correct 821 ms 714576 KB Output is correct
41 Correct 889 ms 714692 KB Output is correct
42 Correct 505 ms 715124 KB Output is correct
43 Correct 617 ms 715836 KB Output is correct
44 Correct 581 ms 715900 KB Output is correct
45 Correct 592 ms 715872 KB Output is correct
46 Correct 558 ms 715832 KB Output is correct
47 Correct 543 ms 715844 KB Output is correct
48 Correct 541 ms 715872 KB Output is correct
49 Correct 550 ms 715844 KB Output is correct
50 Correct 560 ms 715836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 252 ms 709484 KB Output is correct
2 Correct 255 ms 709424 KB Output is correct
3 Correct 257 ms 709488 KB Output is correct
4 Correct 248 ms 709392 KB Output is correct
5 Correct 251 ms 709572 KB Output is correct
6 Correct 256 ms 709444 KB Output is correct
7 Correct 279 ms 709448 KB Output is correct
8 Correct 271 ms 709456 KB Output is correct
9 Correct 292 ms 709420 KB Output is correct
10 Correct 338 ms 709400 KB Output is correct
11 Correct 276 ms 709476 KB Output is correct
12 Correct 327 ms 709488 KB Output is correct
13 Correct 362 ms 709492 KB Output is correct
14 Correct 299 ms 709476 KB Output is correct
15 Correct 339 ms 709452 KB Output is correct
16 Correct 329 ms 709532 KB Output is correct
17 Correct 276 ms 709448 KB Output is correct
18 Correct 280 ms 709544 KB Output is correct
19 Correct 264 ms 709760 KB Output is correct
20 Correct 266 ms 709904 KB Output is correct
21 Correct 255 ms 709768 KB Output is correct
22 Correct 254 ms 709760 KB Output is correct
23 Correct 265 ms 709696 KB Output is correct
24 Correct 281 ms 709796 KB Output is correct
25 Correct 256 ms 709728 KB Output is correct
26 Correct 270 ms 709888 KB Output is correct
27 Correct 958 ms 724552 KB Output is correct
28 Correct 1010 ms 724404 KB Output is correct
29 Correct 978 ms 724268 KB Output is correct
30 Correct 982 ms 724412 KB Output is correct
31 Correct 712 ms 724160 KB Output is correct
32 Correct 945 ms 724280 KB Output is correct
33 Correct 824 ms 724292 KB Output is correct
34 Correct 839 ms 724224 KB Output is correct
35 Correct 247 ms 709444 KB Output is correct
36 Correct 586 ms 715984 KB Output is correct
37 Correct 1395 ms 724428 KB Output is correct
38 Correct 1424 ms 724428 KB Output is correct
39 Correct 1363 ms 724196 KB Output is correct
40 Correct 736 ms 718356 KB Output is correct
41 Correct 489 ms 715076 KB Output is correct
42 Correct 298 ms 710540 KB Output is correct
43 Correct 1496 ms 724428 KB Output is correct
44 Correct 1342 ms 724548 KB Output is correct
45 Correct 1366 ms 724436 KB Output is correct
46 Correct 1243 ms 724436 KB Output is correct
47 Correct 1111 ms 724044 KB Output is correct
48 Correct 1309 ms 724420 KB Output is correct
49 Correct 1329 ms 724436 KB Output is correct
50 Correct 1077 ms 723732 KB Output is correct
51 Correct 270 ms 710092 KB Output is correct
52 Correct 271 ms 710116 KB Output is correct
53 Correct 278 ms 710132 KB Output is correct
54 Correct 271 ms 710124 KB Output is correct
55 Correct 270 ms 710120 KB Output is correct
56 Correct 281 ms 710172 KB Output is correct
57 Correct 488 ms 715920 KB Output is correct
58 Correct 525 ms 715876 KB Output is correct
59 Correct 534 ms 715836 KB Output is correct
60 Correct 572 ms 715928 KB Output is correct
61 Correct 610 ms 715836 KB Output is correct
62 Correct 582 ms 715908 KB Output is correct
63 Correct 819 ms 714572 KB Output is correct
64 Correct 821 ms 714576 KB Output is correct
65 Correct 889 ms 714692 KB Output is correct
66 Correct 505 ms 715124 KB Output is correct
67 Correct 617 ms 715836 KB Output is correct
68 Correct 581 ms 715900 KB Output is correct
69 Correct 592 ms 715872 KB Output is correct
70 Correct 558 ms 715832 KB Output is correct
71 Correct 543 ms 715844 KB Output is correct
72 Correct 541 ms 715872 KB Output is correct
73 Correct 550 ms 715844 KB Output is correct
74 Correct 560 ms 715836 KB Output is correct
75 Correct 1088 ms 724264 KB Output is correct
76 Correct 1038 ms 724408 KB Output is correct
77 Correct 1040 ms 724408 KB Output is correct
78 Correct 1148 ms 724208 KB Output is correct
79 Correct 1282 ms 724292 KB Output is correct
80 Correct 1091 ms 724152 KB Output is correct
81 Correct 1448 ms 724308 KB Output is correct
82 Correct 1478 ms 724288 KB Output is correct
83 Correct 1479 ms 724324 KB Output is correct
84 Correct 1401 ms 724280 KB Output is correct
85 Correct 1422 ms 724280 KB Output is correct
86 Correct 1441 ms 724284 KB Output is correct
87 Correct 1551 ms 724280 KB Output is correct
88 Correct 2375 ms 724152 KB Output is correct
89 Correct 2521 ms 725232 KB Output is correct
90 Correct 2462 ms 725232 KB Output is correct
91 Correct 2609 ms 725228 KB Output is correct
92 Correct 2587 ms 725208 KB Output is correct
93 Correct 1280 ms 728864 KB Output is correct
94 Correct 1279 ms 729500 KB Output is correct
95 Correct 1261 ms 728896 KB Output is correct
96 Correct 1337 ms 728948 KB Output is correct
97 Correct 1239 ms 728872 KB Output is correct
98 Correct 1073 ms 729320 KB Output is correct
99 Correct 1238 ms 729108 KB Output is correct
100 Correct 1195 ms 728824 KB Output is correct
101 Correct 1235 ms 729292 KB Output is correct
102 Correct 1215 ms 729000 KB Output is correct
103 Correct 1239 ms 729020 KB Output is correct
104 Correct 1292 ms 728760 KB Output is correct
105 Correct 1002 ms 726720 KB Output is correct
106 Correct 1274 ms 729756 KB Output is correct
107 Correct 1225 ms 729760 KB Output is correct
108 Correct 1231 ms 729768 KB Output is correct
109 Correct 1245 ms 729968 KB Output is correct
110 Correct 1237 ms 729760 KB Output is correct
111 Correct 1244 ms 729760 KB Output is correct
112 Correct 1232 ms 729812 KB Output is correct
113 Correct 1222 ms 729760 KB Output is correct
114 Correct 1241 ms 729816 KB Output is correct
115 Correct 1227 ms 729820 KB Output is correct
116 Correct 1218 ms 729736 KB Output is correct