hdu 5992 Finding Hotels (kdTree)

题意

kdtree板子题

传送门

Code

#include 
#include 
#include 
#include 
#include 

using namespace std;

const long double eps = 1e-14;
const int maxn = 2e5+10;

int idx, k=2, n;

struct node {
    int x[2], c, id;
    void read(int _id) {
        scanf("%d%d%d", x, x+1, &c);
        id = _id;
    }
    bool operator < (const node &p) const { return x[idx] < p.x[idx]; }
}point[maxn];
typedef pair tp;
tp Q;

struct KdTree {
    node tr[maxn<<2];
    int son[maxn<<2];

#define ls (rt<<1)
#define rs (rt<<1|1)

    void build(int l, int r, int rt = 1, int dep = 0) {
        if(l > r) return;
        son[rt] = r-l;
        son[ls] = son[rs] = -1;
        idx = dep%k;
        int mid = l + r >> 1;
        nth_element(point+l, point+mid, point+r+1);
        tr[rt] = point[mid];
        build(l, mid-1, ls, dep+1);
        build(mid+1, r, rs, dep+1);
    }

    void query(const node &p, int rt = 1, int dep = 0) {
        if(son[rt] == -1) return;
        tp nd(0, tr[rt]);
        for (int i = 0; i < k; ++i)
            nd.first += (long double)(tr[rt].x[i]-p.x[i])*(tr[rt].x[i]-p.x[i]);
        int dim = dep % k, x = ls, y = rs, fg = 0;
        if(p.x[dim] >= tr[rt].x[dim]) swap(x, y);
        if(~son[x]) query(p, x, dep+1);
        if(Q.first == -1) {
            if(tr[rt].c <= p.c) Q = nd;
            fg = 1;
        } else {
            if(tr[rt].c <= p.c && (nd.first < Q.first || (fabs(nd.first-Q.first)

你可能感兴趣的:(hdu 5992 Finding Hotels (kdTree))