BZOJ 3053 (kdtree)

题目链接:点击这里

k维坐标系下的最近点对问题。直接对于每一个询问都在kdtree中询问m次最近点,每次找到一个最近点对需要把它记录下来,当下次再找到它的时候距离直接设置成无穷大即可。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <stack>
#define Clear(x,y) memset (x,y,sizeof(x))
#define Close() ios::sync_with_stdio(0)
#define Open() freopen ("more.in", "r", stdin)
#define get_min(a,b) a = min (a, b)
#define get_max(a,b) a = max (a, b);
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<long long, int>
#define pb push_back
#define mod 1000000007
template <class T>
inline bool scan (T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9') ) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
using namespace std;
#define maxn 50005

struct node {
    long long d[5];
    int l, r;//节点的点的坐标 左右孩子
    long long Max[5], Min[5];//节点中点x的最值 y的最值
    int id;
}tree[maxn<<1], tmp;
int n, m, k;
int root, cmp_d;

bool cmp (const node &a, const node &b) {
    return a.d[cmp_d] < b.d[cmp_d] || (a.d[cmp_d] == b.d[cmp_d] &&
            a.d[cmp_d^1] < b.d[cmp_d^1]);
}

void push_up (int p, int pp) {
    for (int i = 0; i < k; i++) {
        get_min (tree[p].Min[i], tree[pp].Min[i]);
        get_max (tree[p].Max[i], tree[pp].Max[i]);
    }
}

int build_tree (int l, int r, int D) {
    int mid = (l+r)>>1;
    tree[mid].l = tree[mid].r = 0;
    cmp_d = D;
    nth_element (tree+l+1, tree+mid+1, tree+1+r, cmp);
    //按照cmp把第mid元素放在中间 比他小的放左边 比他大的放右边
    for (int i = 0; i < k; i++) {
        tree[mid].Max[i] = tree[mid].Min[i] = tree[mid].d[i];
    }
    if (l != mid) tree[mid].l = build_tree (l, mid-1, (D+1)%k);
    if (r != mid) tree[mid].r = build_tree (mid+1, r, (D+1)%k);
    if (tree[mid].l) push_up (mid, tree[mid].l);
    if (tree[mid].r) push_up (mid, tree[mid].r);
    return mid;
}

void insert (int now) {
    int D = 0, p = root;
    while (1) {
        push_up (p, now);//先更新p节点
        if (tree[now].d[D] >= tree[p].d[D]) {
            if (!tree[p].r) {
                tree[p].r = now;
                return ;
            }
            else p = tree[p].r;
        }
        else {
            if (!tree[p].l) {
                tree[p].l = now;
                return ;
            }
            else p = tree[p].l;
        }
        D = (D+1)%k;
    }
    return ;
}

#define INF 4e18
#define sqr(a) (a)*(a)
pli ans;
long long x[5];
bool vis[maxn];
long long dis (int p) {//点在p的管辖范围内的可能最小值
    long long ans = 0;
    for (int i = 0; i < k; i++) {
        if (x[i] < tree[p].Min[i]) ans += sqr (tree[p].Min[i]-x[i]);
        if (x[i] > tree[p].Max[i]) ans += sqr (x[i]-tree[p].Max[i]);
    }
    return ans;
}

long long distance (int p) {
    long long ans = 0;
    for (int i = 0; i < k; i++) {
        double tmp = tree[p].d[i]-x[i];
        ans += tmp*tmp;
    }
    return ans;
}

void query (int p) {
    long long dl = INF, dr = INF, d0 = INF;
    if (!vis[tree[p].id])
        d0 = distance (p);//初始答案 排除重合
    if (d0 < ans.fi) ans = make_pair (d0, tree[p].id);
    if (tree[p].l) dl = dis (tree[p].l);
    if (tree[p].r) dr = dis (tree[p].r);
    if (dl < dr) {
        if (dl < ans.fi) query (tree[p].l);
        if (dr < ans.fi) query (tree[p].r);
    }
    else {
        if (dr < ans.fi) query (tree[p].r);
        if (dl < ans.fi) query (tree[p].l);
    }
}

long long res[maxn];
struct Point {
    long long x[5], id;
    bool operator < (const Point a) const {
        for (int i = 0; i < k; i++) if (x[i] != a.x[i]) {
            return x[i] < a.x[i];
        }
        return id < a.id;
    }
}point[maxn], del[15];

int main () {
    //Open ();
    while (scanf ("%d%d", &n, &k) == 2) {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < k; j++) {
                scan (tree[i].d[j]);
                point[i].x[j] = tree[i].d[j];
            }
            tree[i].id = point[i].id = i;
        }
        root = build_tree (1, n, 0);
        scanf ("%d", &m);
        Clear (vis, 0);
        for (int i = 1; i <= m; i++) {
            for (int j = 0; j < k; j++) scan (x[j]);
            int cnt; scan (cnt);
            for (int j = 0; j < cnt; j++) {
                ans = make_pair (INF, 0);
                query (root);
                del[j] = point[ans.se];
                vis[ans.se] = 1;
            }
            printf ("the closest %d points are:\n", cnt);
            for (int j = 0; j < cnt; j++) {
                vis[del[j].id] = 0;
                for (int l = 0; l < k; l++) {
                    printf ("%d", del[j].x[l]);
                    if (l < k-1) printf (" ");
                }
                printf ("\n");
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值