HDU 2966 In case of failure(k-dTree)

原题链接

Problem Description

To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the customer to calmly head to the nearest Machine (that should hopefully
work fine).

In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.

Input

The input contains several test cases. The very first line contains the number of cases t (t <= 15) that follow. Each test cases begin with the number of Cash Machines n (2 <= n <= 10^5). Each of the next n lines contain the coordinates of one Cash Machine x,y (0 <= x,y <=10^9) separated by a space. No two
points in one test case will coincide.

Output

For each test case output n lines. i-th of them should contain the squared distance between the i-th ATM from the input and its nearest neighbour.

Sample Input

2
10
17 41
0 34
24 19
8 28
14 12
45 5
27 31
41 11
42 45
36 27
15
0 0
1 2
2 3
3 2
4 0
8 4
7 4
6 3
6 1
8 0
11 0
12 2
13 1
14 2
15 0

Sample Output

200
100
149
100
149
52
97
52
360
97
5
2
2
2
5
1
1
2
4
5
5
2
2
2
5

题目大意

在一个平面上给出n个点,求平面上离它最近的点的距离的平方。

解题思路

k-dTree模板题

AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=100010;
const int DIM=2;
#define lson l,m-1,dep+1
#define rson m+1,r,dep+1
#define ll long long
const ll INF=~0ULL>>1;
int cur;
int K=2;
struct point {
    ll x[DIM];
    bool operator<(const point &oth) const {
        return x[cur]<oth.x[cur];
    }
} vec[MAXN],origin[MAXN],pt,ans[10];
inline ll sqr(ll x) {
    return x*x;
}
ll dist(const point &a,const point &b) {
    ll ret=0;
    for(int i=0; i<K; ++i)
        ret+=sqr(a.x[i]-b.x[i]);
    return ret;
}
void build(ll l,ll r,int dep=0) {
    if(l>=r)
        return;
    ll m=l+r>>1;
    cur=dep%K;
    nth_element(vec+l,vec+m,vec+r+1);
    build(lson);
    build(rson);
}
priority_queue<pair<ll,point> > pq;
void query(const point &x,int k,int l,int r,int dep=0) {
    if(l>r)
        return;
    int m=l+r>>1,cur=dep%K;
    pair<ll,point> tmp(dist(x,vec[m]),vec[m]);
    if(pq.size()<k)
        pq.push(tmp);
    else if(pq.top().first>tmp.first) {
        pq.pop();
        pq.push(tmp);
    }
    if(x.x[cur]<vec[m].x[cur]) {
        query(x,k,lson);
        if(pq.top().first>sqr(x.x[cur]-vec[m].x[cur]))
            query(x,k,rson);
    } else {
        query(x,k,rson);
        if(pq.top().first>sqr(x.x[cur]-vec[m].x[cur]))
            query(x,k,lson);
    }
}
int main() {
    int n,t,m;
    m=2;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; ++i) {
            for(int j=0; j<K; ++j)
                scanf("%I64d",&origin[i].x[j]);
            vec[i]=origin[i];
        }
        build(1,n);

        for(int i=1;i<=n;++i) 
        {
            query(origin[i],m,1,n);
            printf("%I64d\n",dist(origin[i],pq.top().second));
            pq.pop();
            pq.pop();
        }

    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值