답안 #498924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
498924 2021-12-26T17:44:08 Z _martynas 원 고르기 (APIO18_circle_selection) C++14
0 / 100
575 ms 35748 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1e9+7;

const int MAX_N = 3e5+5;

int n;
ll X[MAX_N], Y[MAX_N], R[MAX_N];
int P[MAX_N];
bool visited[MAX_N];

template<typename T>
inline T clamp(const T & val, const T & low, const T & high)
{
    return val < low ? low : (val > high ? high : val);
}

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

    Quad()
    {
    }

    Quad(ll _l, ll _r, ll _bot, ll _top)
        : l(_l), r(_r), bot(_bot), top(_top)
    {
        idx = -1;
        sz = 0;
    }

    void add(int x, int y, 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) {
            this->idx = idx;
        }
        else {
            if(childs[0] == nullptr) {
                ll midH, midV;
                midH = l+(r-l)/2;
                midV = bot+(top-bot)/2;

                if(l != r && top != bot) {
                    childs[0] = new Quad(l, midH, bot, midV);
                    childs[1] = new Quad(midH+1, r, bot, midV);
                    childs[2] = new Quad(l, midH, midV+1, top);
                    childs[3] = new Quad(midH+1, r, midV+1, top);
                }
                else if(l != r) {
                    childs[0] = new Quad(l, midH, bot, top);
                    childs[1] = new Quad(midH+1, r, bot, top);
                }
                else if(top != bot) {
                    childs[0] = new Quad(l, r, bot, midV);
                    childs[1] = new Quad(l, r, midV+1, top);
                }
            }

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

    void setInRange(int x, int y, int radius, int idx)
    {
        if(x+2*radius < l || x-2*radius > r || y+2*radius < bot || y-2*radius > top || sz == 0) {
            return;
        }

//        cerr << "l = " << l << ", r = " << r << ", "
//             << "bot = " << bot << ", top = " << top << "\n";
        if(l == r && bot == top) {
            if((R[idx]+R[this->idx])*(R[idx]+R[this->idx])
                >= llabs(X[idx]-X[this->idx])*llabs(X[idx]-X[this->idx])
                +llabs(Y[idx]-Y[this->idx])*llabs(Y[idx]-Y[this->idx])) {
                P[this->idx] = idx;
                visited[this->idx] = true;
                sz--;
            }
            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;
    }
};

Quad Q(0, 15, 0, 15);

int main()
{
//    cerr << "0:\n";
//    Q.add(0, 0, 0);
//    cerr << "1:\n";
//    Q.add(2, 2, 1);
//    cerr << "2:\n";
//    Q.add(-3, -3, 2);
//
//    cerr << "P:\n";
//    Q.setInRange(0, 1, 1, 3);
//    for(int i = 0; i < 3; i++) {
//        cerr << i << ": " << P[i] << "\n";
//    }

    map<pair<int, int>, int> M;

    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> X[i] >> Y[i] >> R[i];
    }
    iota(P+n, P+n, 0);

    // Handle circles at the same position
    for(int i = 0; i < n; i++) {
        auto it = M.find({X[i], Y[i]});
        if(it != M.end()) {
            if(R[it->second] < R[i]) {
                visited[it->second] = true;
                P[it->second] = i;
                M[{X[i], Y[i]}] = i;
            }
            else {
                visited[i] = true;
                P[i] = it->second;
            }
        }
        else {
            M[{X[i], Y[i]}] = i;
        }
    }

    for(int i = 0; i < n; i++) {
        //cerr << i << " started\n";
        if(!visited[i]) {
            Q.add(X[i], Y[i], i);
        }
        //cerr << i << " finished\n";
    }

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

    for(int i = 0; i < n; i++) {
        int idx = I[i];
        if(visited[idx]) continue;
        Q.setInRange(X[idx], Y[idx], R[idx], idx);
    }

//    for(int i = 0; i < n; i++) {
//        cerr << i << ": " << P[i] << "\n";
//    }

    for(int i = 0; i < n; i++) {
        cout << 1+P[i] << " ";
    }
    cout << "\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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Incorrect 1 ms 296 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 575 ms 35040 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 565 ms 35748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Incorrect 1 ms 296 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Incorrect 1 ms 296 KB Output isn't correct
3 Halted 0 ms 0 KB -