跳到正文

剪贴板

剪贴板 tlbnzyno

原文
剪贴板信息

剪贴板信息

收录
更新

Codes - P4631 【[APIO2018] Circle selection 选圆圈】

Code for Solution 1

C++
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
const int N = 3e5 + 5;
const int inf = 1e9;

int n;
struct Triad {
    int x, y, r;
    inline void read() {
        cin >> x >> y >> r;
        x += inf, y += inf;
    }
} circ[N];
int ans[N];

int cir_ord[N];
vector<Triad> f;
vector<int> g[N];
int bsiz;

inline long long sqr(int x) {
    return x * 1ll * x;
}
inline bool connect(Triad& a, Triad& b) {
    return sqr(a.x - b.x) + sqr(a.y - b.y) <= sqr(a.r + b.r);
}

typedef vector<Triad>::iterator vecIt;
inline void init_blocks(int L) {
    if (bsiz && bsiz / 2 < L) return;
    bsiz = L; f.clear();
    for (int i = 1; i <= n; i++) if (!ans[i])
        f.push_back(Triad{circ[i].x / bsiz, circ[i].y / bsiz, i});
    sort(f.begin(), f.end(), [](Triad& a, Triad& b) {
        return a.x != b.x ? a.x < b.x : a.y < b.y;
    });
    int cnt = 0;
    for (vecIt i = f.begin(), j; i != f.end(); i = j, ++cnt) {
        for (j = i; j != f.end(); j++)
            if (i->x != j->x || i->y != j->y) break;
        f[cnt] = *i, g[cnt].clear();
        for (vecIt k = i; k != j; k++) g[cnt].push_back(k->r);
    }
    f.resize(cnt);
}

void solve(int p) {
    if (ans[p]) return;
    init_blocks(circ[p].r * 2);

    int x = circ[p].x / bsiz;
    int y = circ[p].y / bsiz;
    for (int i = x - 1; i <= x + 1; i++)
        for (int j = y - 1; j <= y + 1; j++) {
            if (i < 0 || j < 0) continue;
            int c = lower_bound(f.begin(), f.end(), Triad{i, j, 0}, [](Triad& a, const Triad& b) {
                return a.x != b.x ? a.x < b.x : a.y < b.y;
            }) - f.begin();
            if (c == int(f.size()) || f[c].x != i || f[c].y != j)
                continue;

            vector<int> buf;
            for (auto k : g[c])
                if (connect(circ[k], circ[p])) ans[k] = p;
                else buf.push_back(k);
            g[c].swap(buf);
        }
}

signed main() {
    ios::sync_with_stdio(false);

    cin >> n;
    for (int i = 1; i <= n; i++)
        circ[i].read(), cir_ord[i] = i;
    sort(cir_ord + 1, cir_ord + 1 + n, [](int& a, int& b) {
        return circ[a].r != circ[b].r ? circ[a].r > circ[b].r : a < b;
    });

    for (int i = 1; i <= n; i++)
        solve(cir_ord[i]);
    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';
    return cout << endl, 0;
}

Code for Solution 2

C++
#include <algorithm>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>

using namespace std;
const int N = 3e5 + 5;

int n;
struct circle {
    int x, y, R, idx;
} C[N];
int ans[N];
int fin[N];

inline long long sqr(int x) {
    return x * 1ll * x;
}
inline bool connect(circle& a, circle& b) {
    return sqr(a.x - b.x) + sqr(a.y - b.y) <= sqr(a.R + b.R);
}

struct event {
    int time, cmd, pid;
};
bool self[N];

void cdqSolve(int l, int r) {
    if (l == r) {
        if (ans[l] > n) ans[l] = l, self[l] = 1;
        return;
    }
    int mid = (l + r) >> 1;
    cdqSolve(l, mid);

    set<pair<int, int> > s;
    set<pair<int, int> >::iterator it;
    vector<event> v;

    for (int i = l; i <= mid; i++) if (self[i]) {
        v.push_back(event{C[i].x - C[i].R, 1, i}); // ins
        v.push_back(event{C[i].x + C[i].R, 3, i}); // del
    }
    for (int i = mid + 1; i <= r; i++) {
        v.push_back(event{C[i].x - C[i].R, 2, i});
        v.push_back(event{C[i].x + C[i].R, 2, i}); // qry
    }
    sort(v.begin(), v.end(), [](event& a, event& b) {
        return a.time != b.time ? a.time < b.time : a.cmd < b.cmd;
    });
    for (auto i : v)
        if (i.cmd == 1) {
            s.insert({C[i.pid].y, i.pid});
        } else if (i.cmd == 3) {
            s.erase({C[i.pid].y, i.pid});
        } else {
            it = s.lower_bound({C[i.pid].y, 0});
            if (it != s.end()) {
                if (connect(C[i.pid], C[it->second]))
                    ans[i.pid] = min(ans[i.pid], it->second);
            }
            if (it != s.begin()) {
                if (connect(C[i.pid], C[(--it)->second]))
                    ans[i.pid] = min(ans[i.pid], it->second);
            }
        }

    v.clear();
    for (int i = l; i <= mid; i++) if (self[i]) {
        v.push_back(event{C[i].y - C[i].R, 1, i}); // ins
        v.push_back(event{C[i].y + C[i].R, 3, i}); // del
    }
    for (int i = mid + 1; i <= r; i++) {
        v.push_back(event{C[i].y - C[i].R, 2, i});
        v.push_back(event{C[i].y + C[i].R, 2, i}); // qry
    }
    sort(v.begin(), v.end(), [](event& a, event& b) {
        return a.time != b.time ? a.time < b.time : a.cmd < b.cmd;
    });
    for (auto i : v)
        if (i.cmd == 1) {
            s.insert({C[i.pid].x, i.pid});
        } else if (i.cmd == 3) {
            s.erase({C[i.pid].x, i.pid});
        } else {
            it = s.lower_bound({C[i.pid].x, 0});
            if (it != s.end()) {
                if (connect(C[i.pid], C[it->second]))
                    ans[i.pid] = min(ans[i.pid], it->second);
            }
            if (it != s.begin()) {
                if (connect(C[i.pid], C[(--it)->second]))
                    ans[i.pid] = min(ans[i.pid], it->second);
            }
        }

    cdqSolve(mid + 1, r);
}

signed main() {
    ios::sync_with_stdio(false);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> C[i].x >> C[i].y >> C[i].R;
        C[i].idx = i;
    }

    sort(C + 1, C + 1 + n, [](circle& a, circle& b) {
        return a.R != b.R ? a.R > b.R : a.idx < b.idx;
    });
    memset(ans, 0x3f, sizeof(ans));
    cdqSolve(1, n);

    for (int i = 1; i <= n; i++)
        fin[C[i].idx] = C[ans[i]].idx;
    for (int i = 1; i <= n; i++)
        cout << fin[i] << ' ';
    return cout << endl, 0;
}

Code for Solution 3

C++
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;
const double eps = 1e-3;
const int N = 3e5 + 5;
const int K = 2;

int n;
struct circle {
    double p[K]; int r;
};
pair<circle, int> C[N];
int ans[N];

struct area {
    double max[K], min[K];
};

inline area trans(circle a) {
    return area{{a.p[0] + a.r, a.p[1] + a.r}, {a.p[0] - a.r, a.p[1] - a.r}};
}

struct node {
    int lc, rc;
    area range;
    circle val;
    int index, vaild;
} t[N];
int total = 0;

inline void pushup(int x) {
    t[x].range = trans(t[x].val);
    t[x].vaild = t[t[x].lc].vaild + t[t[x].rc].vaild + 1;
    for (int i = 0; i < K; i++) {
        if (t[x].lc) {
            t[x].range.max[i] = max(t[x].range.max[i], t[t[x].lc].range.max[i]);
            t[x].range.min[i] = min(t[x].range.min[i], t[t[x].lc].range.min[i]);
        }
        if (t[x].rc) {
            t[x].range.max[i] = max(t[x].range.max[i], t[t[x].rc].range.max[i]);
            t[x].range.min[i] = min(t[x].range.min[i], t[t[x].rc].range.min[i]);
        }
    }
}

int build(int l, int r, int d) {
    if (l > r) return 0;
    int mid = (l + r) >> 1, x = ++total;
    nth_element(C + l, C + mid, C + r + 1, [&](pair<circle, int> a, pair<circle, int> b) {
        return a.first.p[d] < b.first.p[d];
    });
    t[x].val = C[mid].first;
    t[x].index = C[mid].second;
    t[x].lc = build(l, mid - 1, d ^ 1);
    t[x].rc = build(mid + 1, r, d ^ 1);
    return pushup(x), x;
}

inline bool outside(circle& a, area& b) {
    double x = a.p[0], y = a.p[1]; int r = a.r;
    if (b.min[0] - (x + r) >= eps) return true;
    if (b.min[1] - (y + r) >= eps) return true;
    if ((x - r) - b.max[0] >= eps) return true;
    if ((y - r) - b.max[1] >= eps) return true;
    return false;
}
inline double sqr(double x) {
    return x * x;
}
inline bool connect(circle& a, circle& b) {
    return sqr(a.r + b.r) - (sqr(a.p[0] - b.p[0]) + sqr(a.p[1] - b.p[1])) >= -eps;
}
void select(int x, pair<circle, int>& c) {
    if (!x || outside(c.first, t[x].range)) return;
    if (!ans[t[x].index] && connect(c.first, t[x].val))
        ans[t[x].index] = c.second;
    if (t[t[x].lc].vaild) select(t[x].lc, c);
    if (t[t[x].rc].vaild) select(t[x].rc, c);
    t[x].vaild = t[t[x].lc].vaild + t[t[x].rc].vaild + (!ans[t[x].index] ? 1 : 0);
}

inline void rotate(double theta) {
    double SIN = sin(theta), COS = cos(theta);
    for (int i = 1; i <= n; i++) {
        double x = C[i].first.p[0];
        double y = C[i].first.p[1];
        C[i].first.p[1] = x * SIN + y * COS;
        C[i].first.p[0] = x * COS - y * SIN;
    }
}

signed main() {
    ios::sync_with_stdio(false);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> C[i].first.p[0] >> C[i].first.p[1] >> C[i].first.r;
        C[i].second = i;
    }
    rotate(2.3);

    int root = build(1, n, 0);

    sort(C + 1, C + 1 + n, [](pair<circle, int>& a, pair<circle, int>& b) {
        return a.first.r != b.first.r ? a.first.r > b.first.r : a.second < b.second;
    });
    for (int i = 1; i <= n; i++)
        if (!ans[C[i].second]) select(root, C[i]);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';
    return cout << endl, 0;
}