4072.   3D Birds-Shooting Game


Time Limit: 3.0 Seconds   Memory Limit: 65536K Total Runs: 167   Accepted Runs: 37

There are N birds in a 3D space, let x, y and z denote their coordinates in each dimension. You, the excellent shooter, this time sit in a helicopter and want to shoot these birds with your gun. Through the window of the helicopter you can only see a rectangle area and you choose to shoot the highest bird in view (the helicopter is above all the birds and the rectangle area is parallel to the ground). Now given the rectangle area of your view, can you figure out which bird to shoot?

Input

First line will be a positive integer T (1≤T≤10) indicating the test case number.. Following there are T test cases.. Each test case begins with a positive integer N (1≤N≤10000), the number of birds.. Then following N lines with three positive integers x, y and z (1≤x,y,z≤100000), which are the coordinates of each bird (you can assume no two birds have the same height z).. Next will be a positive integer Q (1≤Q≤10000) representing the number of query.. Following Q lines with four positive integers which are the lower-left and upper-right points' coordinates of the rectangle view area.. (please note that after each query you will shoot down the bird you choose and you can't shoot it any more in the later).

Output

For each query output the coordinate of the bird you choose to shoot or output 'Where are the birds?' (without the quotes) if there are no birds in your view.

Sample Input

2
3
1 1 1
2 2 2
3 3 3
2
1 1 2 2
1 1 3 3
2
1 1 1
3 3 3
2
2 2 3 3
2 2 3 3

Sample Output

2 2 2
3 3 3
3 3 3
Where are the birds?

Source: TJU Team Selection 2014 Round1

题意:三维空间中有N个点,给出其坐标;接下来Q个询问,每次询问给出一个垂直于z轴的矩形,矩形边界与坐标轴平行,求出此矩形范围内z坐标最大的点,输出其坐标,如果没有满足条件的点,输出"Where are the birds?"。每个点只能被输出一次。(所有点的z坐标值都不相同)

分析:典型的k-d树,在三维空间中查询满足条件的解。建树的时候,依次按照x,y,z来划分空间(也可以优化)。查询的时候,如果遇到以z坐标划分空间的情况,则先查询上部空间,查询下层空间的时候,先比较当前找到的答案与划分点的z坐标值,如果当前答案比该值还大,则没有再必要查询下层空间。

代码如下:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 10005 int idx;
struct point{
int x[];
bool operator < (const point &a) const{
return x[idx] < a.x[idx];
}
}; point p[N];
point tr[N<<];
int cnt[N<<];
bool flag[N<<]; int x1, x2, y1, y2;
void build(int l, int r, int u, int dep)
{
if(l > r) return;
cnt[u] = r-l+;
if(l == r)
{
tr[u] = p[l];
return;
}
idx = dep%;
int mid = l+r>>;
nth_element(p+l, p+mid, p+r+);
tr[u] = p[mid];
build(l, mid-, u<<, dep+);
build(mid+, r, u<<|, dep+);
} int ans, id;
bool check(point u)
{
return u.x[] >= x1 && u.x[] <= x2 && u.x[] >= y1 && u.x[] <= y2;
}
void query(int u, int dep)
{
if(cnt[u] == ) return;
if(!flag[u] && check(tr[u]))
if(tr[u].x[] > ans) ans = tr[u].x[], id = u;
int tid = dep%;
if(tid == )
{
if(x2 < tr[u].x[]) query(u<<, dep+);
else if(x1 > tr[u].x[]) query(u<<|, dep+);
else {
query(u<<, dep+);
query(u<<|, dep+);
}
}
else if(tid == )
{
if(y2 < tr[u].x[]) query(u<<, dep+);
else if(y1 > tr[u].x[]) query(u<<|, dep+);
else {
query(u<<, dep+);
query(u<<|, dep+);
}
}
else if(tid == )
{
query(u<<|, dep+);
if(ans < tr[u].x[])//这儿因为写成if(ans == -1)错了很多次
query(u<<, dep+);
}
} int main()
{
int T, n, q;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d %d %d", &p[i].x[], &p[i].x[], &p[i].x[]);
memset(flag, , sizeof(flag));
memset(cnt, , sizeof(cnt));
build(, n-, , ); scanf("%d", &q);
while(q--)
{
ans = -;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
query(, );
if(ans == -)
puts("Where are the birds?");
else{
printf("%d %d %d\n", tr[id].x[], tr[id].x[], tr[id].x[]);
flag[id] = ;
}
}
}
return ;
}

最新文章

  1. git文件迁移到新架构
  2. 你必须知道的Javascript 系列
  3. MySQL的下载与安装 和 navicat for mysql 安装使用
  4. activity 和 生命周期 :流程
  5. CentOS 7 中firewall-cmd命令
  6. Ruby on rail 开发准备
  7. UpdatePanel中执行js
  8. DBNull.Value 字段的用法
  9. bzoj3626
  10. Apache配置完虚拟主机后,使用Chrome访问localhost还是默认目录htdocs
  11. [py]资源搜集
  12. 解决JNI native 线程不能正常退出的问题
  13. 解决微云登陆出现wns login error的问题
  14. 20155210 实验一 逆向与Bof基础
  15. Python3学习之路~2.2 简单的购物车程序
  16. 20154312 曾林 Exp5_MSF基础应用
  17. Unity3d 手机屏幕自动适配
  18. SVN文件排除
  19. 学习 altera官网 之 timequest
  20. Live555 中的客户端openRTSP 保存H264文件

热门文章

  1. laravel的使用
  2. PHP 三元运算符?:的小坑
  3. openstack介绍及共享组件——消息队列rabbitmq
  4. Python算法每日一题--002--求众数
  5. sql 为什么要用where 1=1?
  6. 转义BABEL的POLYFILL和RUNTIME的区别
  7. each of which 用法
  8. python学习第十二天列表的循环,排序,统计操作方法
  9. II play with GG
  10. 关于分布式唯一ID,snowflake的一些思考及改进(完美解决时钟回拨问题)