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

2016ACM/ICPC亚洲区青岛站-重现赛(感谢中国石油大学)

【题目链接】:http://acm.split.hdu.edu.cn/showproblem.php?pid=5992

【题解】



先炫耀一下;



kd-tree;

如果没学过kd-tree不建议你再往下看。

这里不能用那个四边形的估价函数;会超时。

有一个更好的估价函数是

judge=(op.d[fx]-t[rt].d[fx])^2;

op是输入的询问,d[2]表示坐标;(二维)

这里的fx是kd-tree中当前这个域的划分依据,fx==0表示是以x轴作为划分依据、fx==1表示是以y轴作为划分依据;

具体点

    int zuo = t[rt].l,you = t[rt].r;
if (op.d[fx]>t[rt].d[fx])//先搞fx维坐标离操作坐标近的;
swap(zuo,you);
query(zuo,1-fx);//优先搞"左"子树
bool should = false;//判断要不要搞右子树;
if (dis == INF)//如果距离为正无穷表示还没有更新过答案就要搞
should = true;
else
{
LL ju = sqr(op.d[fx]-t[rt].d[fx]);//否则看看这个划分依据fx的维当前这个fx坐标与操作坐标的距离的平方
//经过了swap "you"子树内全部都是比t[rt].d[fx]小或全都比它大的节点;
if (ju <= dis)//如果估价函数比当前更新到的答案小;那么就要搞右子树
should = true;//如果估价函数比当前更新到的答案还要大,那么就不要搞右子树了;
}//因为如果继续搞右子树,fx这一维的坐标在右子树里面是单调的;ju只会越来越大;
if (should)
query(you,1-fx);

除此之外;还可以在每个树的节点里面储存以这个节点为根的子树下面最低消费最小的min;如果min都大于op.n则可以不用进入子树了;

(交程序的时候选g++不然会莫名TLE);



【完整代码】

#include <cstdio>
#include <algorithm>
#define LL long long using namespace std; const int MAXN = 205000;
const LL INF = 1e18; struct point
{
int min, n, dot, l,r;
LL d[2];
}; int n, m, root, now;
LL dis; point t[MAXN];
point op,ans; void input_data()
{
scanf("%d%d", &n,&m);
for (int i = 1; i <= n; i++)
{
scanf("%I64d%I64d%d", &t[i].d[0], &t[i].d[1],&t[i].n);
t[i].dot = i;
}
} bool cmp_1(point a, point b)
{
return a.d[now] < b.d[now];
} void gengxin(int father, int son)
{
if (t[father].min > t[son].min)
t[father].min = t[son].min;
} void up_data(int rt)
{
t[rt].min = t[rt].n;
int l = t[rt].l, r = t[rt].r;
if (l) gengxin(rt, l);
if (r) gengxin(rt, r);
} int build(int begin, int end, int fa,int fx)
{
int m = (begin + end) >> 1;
now = fx;
nth_element(t + begin, t + m, t + end + 1, cmp_1);
if (begin < m)
t[m].l = build(begin, m - 1, m, 1 - fx);
else
t[m].l = 0;
if (m < end)
t[m].r = build(m + 1, end, m, 1 - fx);
else
t[m].r = 0;
up_data(m);
return m;
} LL sqr(LL x)
{
return x*x;
} void query(int rt,int fx)
{
if (!rt)
return;
int temp1 = t[rt].min;
if (temp1 > op.n)
return;
if (t[rt].n <= op.n)
{
LL dd = sqr(op.d[0]-t[rt].d[0])+sqr(op.d[1]-t[rt].d[1]);
if (dd < dis || (dd==dis && t[rt].dot < ans.dot))
{
dis = dd;
ans.d[0] = t[rt].d[0];
ans.d[1] = t[rt].d[1];
ans.n = t[rt].n;
ans.dot = t[rt].dot;
}
}
int zuo = t[rt].l,you = t[rt].r;
if (op.d[fx]>t[rt].d[fx])
swap(zuo,you);
query(zuo,1-fx);
bool should = false;
if (dis == INF)
should = true;
else
{
LL ju = sqr(op.d[fx]-t[rt].d[fx]);
if (ju <= dis)
should = true;
}
if (should)
query(you,1-fx);
} void get_ans()
{
root = build(1, n, 0, 0);
for (int i = 1; i <= m; i++)
{
scanf("%I64d%I64d%d",&op.d[0],&op.d[1],&op.n);
dis = INF;
query(root,0);
printf("%I64d %I64d %d\n",ans.d[0],ans.d[1],ans.n);
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--)
{
input_data();
get_ans();
}
return 0;
}

【22.95%】【hdu 5992】Finding Hotels的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  3. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  4. 七夕节 (HDU - 1215) 【简单数论】【找因数】

    七夕节 (HDU - 1215) [简单数论][找因数] 标签: 入门讲座题解 数论 题目描述 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们 ...

  5. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  7. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  8. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  9. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

随机推荐

  1. Dubbo springcloud

    简而言之,Dubbo确实类似于Spring Cloud的一个子集,Dubbo功能和文档完善,在国内有很多的成熟用户,然而鉴于Dubbo的社区现状(曾经长期停止维护,2017年7月31日团队又宣布重点维 ...

  2. 在windows上面安装并用jupyter运行pyspark

    1,下载hadoop  winutils 设置HADOOP_HOME 2.下载spark,设置SPARK_HOME,将%SPARK_HOME%/加入到PATH路径下 3.安装Anaconda 就是py ...

  3. Mysql学习总结(18)——Mysql主从架构的复制原理及配置详解

    一.复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...

  4. javascript进阶课程--第二章--对象

    javascript进阶课程--第二章--对象 学习要点 理解面向对象的概念 掌握对象的创建方法 掌握继承的概念和实现方法 基本概念 对象究竟是什么?什么叫面向对象编程? 对象是从我们现实生活中抽象出 ...

  5. int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....

    Android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指针问题,费劲心思才找到报空指针的原因: 代码 ...

  6. Codeforces 451 E. Devu and Flowers(组合数学,数论,容斥原理)

    传送门 解题思路: 假如只有 s 束花束并且不考虑 f ,那么根据隔板法的可重复的情况时,这里的答案就是 假如说只有一个 f 受到限制,其不合法时一定是取了超过 f 的花束 那么根据组合数,我们仍然可 ...

  7. C++ 复制到粘贴板

    网上好多教程讲如何复制到剪切板,但是有可能复制的是乱码,为了方便,将CString类型的复制到剪切板 CString source;if (OpenClipboard()){//防止非ASCII语言复 ...

  8. iOS关闭键盘简单实现(objc/swift)

    Objective-C 代码实例方式一 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; 假设一个view上有很多Text ...

  9. BFS模版程序

    本文转自q=bfs&u=cnyali&t=blog">http://so.csdn.net/so/search/s.do?q=bfs&u=cnyali& ...

  10. CentOS6 安装中文包和变更系统默认语言

    CentOS6 安装中文包和变更系统默认语言   用 yum 安装语言包的命令是 yum groupinstall <language>-support    ,其中 <langua ...