# 2016 ICPC青岛站---k题 Finding Hotels（K-D树）

http://acm.hdu.edu.cn/showproblem.php?pid=5992

Problem Description
There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations. The distances are measured in Euclidean metric.

Input
The first line is the number of test cases. For each test case, the first line contains two integers N (N ≤ 200000) and M (M ≤ 20000). Each of the following N lines describes a hotel with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the hotel, c is its price. It is guaranteed that each of the N hotels has distinct x, distinct y, and distinct c. Then each of the following M lines describes the query of a guest with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the guest, c is the maximum acceptable price of the guest.

Output
For each guests query, output the hotel that the price is acceptable and is nearest to the guests location. If there are multiple hotels with acceptable prices and minimum distances, output the first one.

Sample Input
2
3 3
1 1 1
3 2 3
2 3 2
2 2 1
2 2 2
2 2 3
5 5
1 4 4
2 1 2
4 5 3
5 2 1
3 3 5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5

Sample Output
1 1 1
2 3 2
3 2 3

5 2 1
2 1 2
2 1 2
1 4 4
3 3 5

Source

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
#define Sqrt2(x) (x)*(x)
typedef long long LL;
int N,M,idx;

struct Node
{
int f[3];
int id;
bool operator<(const Node& s)const
{
return f[idx]<s.f[idx];
}
}data[200005],tr[4*200005];
int flag[4*200005];
pair<LL,Node> ans;

void build(int l,int r,int i,int deep)
{
if(l>r) return;
flag[i]=1;
flag[i<<1]=0; flag[i<<1|1]=0;
idx=deep%2;
int mid=(l+r)>>1;
nth_element(data+l,data+mid,data+r+1);
tr[i]=data[mid];
build(l,mid-1,i<<1,deep+1);
build(mid+1,r,i<<1|1,deep+1);
}

void query(Node p,int i,int deep)
{
if(!flag[i]) return ;
pair<LL,Node> c;
c.second=tr[i];
c.first=(LL)(Sqrt2((LL)p.f[0]-tr[i].f[0])+Sqrt2((LL)p.f[1]-tr[i].f[1]));
bool fg=0;
int idm=deep%2;
int x=i<<1;
int y=i<<1|1;
if(p.f[idm]>=tr[i].f[idm]) swap(x,y);
if(flag[x]) query(p,x,deep+1);
if(ans.first==-1){
if(c.second.f[2]<=p.f[2])
ans.first=c.first,ans.second=c.second;
fg=1;
}
else {
if(c.second.f[2]<=p.f[2]&&(c.first<ans.first||(c.first==ans.first&&c.second.id<ans.second.id)))
ans.first=c.first,ans.second=c.second;
if((LL)(Sqrt2(tr[i].f[idm]-p.f[idm]))<ans.first)
fg=1;
}
if(fg&&flag[y]) query(p,y,deep+1);
}

int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
{
for(int j=0;j<3;j++)
scanf("%d",&data[i].f[j]);
data[i].id=i;
}
build(1,N,1,0);
while(M--)
{
Node p;
for(int i=0;i<3;i++)
scanf("%d",&p.f[i]);
ans.first=-1;
query(p,1,0);
printf("%d %d %d\n",ans.second.f[0],ans.second.f[1],ans.second.f[2]);
}
}
return 0;
}
View Code

posted @ 2016-11-20 16:09 茶飘香~ 阅读(...) 评论(...) 编辑 收藏