HDU 5992 && 2016青岛ICPC: K. Finding Hotels(KD树)

 

Finding Hotels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1755    Accepted Submission(s): 546

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

 

题意:

直角坐标系上有n个宾馆,每个宾馆的价格都不同,有m个人想住宾馆,已知他们的位置和最大可以接受的价格,对于每个人求出最近的能住的宾馆在哪儿

 

思路:

将价格也作为一维,上个三维KD树模板搞定(青岛现场赛时暴力可以过,当时我们就是暴力过得

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
#define deep 3
LL ans;
int loc, id;
typedef struct Point
{
	int id;
	LL x[3];
	bool operator < (const Point &b) const
	{
		if(x[loc]<b.x[loc])
			return 1;
		return 0;
	}
}Point;
Point p[200005], temp[200005], Aim;
void Create(int l, int r, int now)
{
	int m;
	if(l>=r)
		return;
	m = (l+r)/2;
	loc = now%deep;
	nth_element(p+l, p+m, p+r+1);
	Create(l, m-1, now+1);
	Create(m+1, r, now+1);
}
void Jud(Point now)
{
	LL bet;
	if(now.x[2]>Aim.x[2])
		return;
	bet = (now.x[1]-Aim.x[1])*(now.x[1]-Aim.x[1])+(now.x[0]-Aim.x[0])*(now.x[0]-Aim.x[0]);
	if(bet<ans || bet==ans && now.id<id)
		ans = bet, id = now.id;
}
void Find(int l, int r, int now)
{
	int m;
	LL len;
	loc = now%deep;
	m = (l+r)/2;
	if(l>=r)
	{
		if(l==r)
			Jud(p[r]);
		return;
	}
	Jud(p[m]);
	if(loc!=2)
	{
		len = p[m].x[loc]-Aim.x[loc];
		if(len>=0)
		{
			Find(l, m-1, now+1);
			if(len*len<ans)
				Find(m+1, r, now+1);
		}
		else
		{
			Find(m+1, r, now+1);
			if(len*len<ans)
				Find(l, m-1, now+1);
		}
	}
	else
	{
		len = p[m].x[loc]-Aim.x[loc];
		if(len>=0)
			Find(l, m-1, now+1);
		else
		{
			Find(m+1, r, now+1);
			Find(l, m-1, now+1);
		}
	}
}
int main(void)
{
	int T, n, Q, i, j;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d%d", &n, &Q);
		for(i=1;i<=n;i++)
		{
			for(j=0;j<=deep-1;j++)
				scanf("%lld", &p[i].x[j]);
			p[i].id = i;
			temp[i] = p[i];
		}
		Create(1, n, 0);
		while(Q--)
		{
			ans = 1e18, id = -1;
			for(j=0;j<=deep-1;j++)
				scanf("%lld", &Aim.x[j]);
			Find(1, n, 0);
			printf("%lld", temp[id].x[0]);
			for(j=1;j<=deep-1;j++)
				printf(" %lld", temp[id].x[j]);
			puts("");
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值