Time Limit: 3.0 Seconds Memory Limit:65536K
Total Runs: 110 Accepted Runs:24
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
题目大意:三维空间内给出二维的的方块,查询区域内第三维最大的并将其删去
题目分析:k-d树直接做,模板题
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MAX 20007
using namespace std;
struct Point
{
int x,y,h,flag;
}p[MAX];
int dv[MAX];
struct Tree
{
int lx , ly , rx , ry , maxn , l ,r;
bool flag;
}tree[MAX*30];
bool cmpx ( const Point& p1 , const Point &p2 )
{
return p1.x < p2.x;
}
bool cmpy ( const Point &p1 , const Point &p2 )
{
return p1.y < p2.y;
}
void push_up ( int u )
{
if ( p[tree[u<<1].maxn].h > p[tree[u<<1|1].maxn].h )
tree[u].maxn = tree[u<<1].maxn;
else
tree[u].maxn = tree[u<<1|1].maxn;
}
void build ( int u , int l , int r )
{
tree[u].l = l;
tree[u].r = r;
tree[u].lx = min_element ( p+l , p+r+1 , cmpx )->x;
tree[u].rx = max_element ( p+l , p+r+1 , cmpx )->x;
tree[u].ly = min_element ( p+l , p+r+1 , cmpy )->y;
tree[u].ry = max_element ( p+l , p+r+1 , cmpy )->y;
tree[u].flag = tree[u].rx - tree[u].lx >= tree[u].ry - tree[u].ly;
if ( l == r )
{
tree[u].maxn = l;
return;
}
int mid = l + r >> 1;
nth_element ( p+l , p+mid , p+r+1 , tree[u].flag?cmpx:cmpy );
build ( u<<1 , l , mid );
build ( u<<1|1 , mid+1 , r );
push_up ( u );
}
bool check ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 )
{
if ( x2 < x3 ) return false;
if ( x1 > x4 ) return false;
if ( y1 > y4 ) return false;
if ( y2 < y3 ) return false;
return true;
}
int query ( int u , int x1 , int y1 , int x2 , int y2 )
{
int lx = tree[u].lx , ly = tree[u].ly , rx = tree[u].rx , ry = tree[u].ry;
if ( x1 <= lx && y1 <= ly && x2 >= rx && y2 >= ry )
return tree[u].maxn;
int x3 = tree[u<<1].lx , y3 = tree[u<<1].ly , x4 = tree[u<<1].rx , y4 = tree[u<<1].ry;
int x5 = tree[u<<1|1].lx , y5 = tree[u<<1|1].ly , x6 = tree[u<<1|1].rx , y6 = tree[u<<1|1].ry;
int tid1 = 0, tid2 = 0;
if ( check ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) )
tid1 = query ( u<<1 , x1 , y1 , x2 , y2 );
if ( check ( x1 , y1 , x2 , y2 , x5 , y5 , x6 , y6 ) )
tid2 = query ( u<<1|1 , x1 , y1 , x2 , y2 );
if ( p[tid1].h > p[tid2].h )
return tid1;
else return tid2;
}
void update ( int u , int tid )
{
int l = tree[u].l , r = tree[u].r;
if ( l == r )
{
p[tid].h = -1;
return;
}
int mid = l + r >> 1;
if ( tid > mid ) update ( u<<1|1 , tid );
else update ( u<<1 , tid );
push_up ( u );
}
int t,n,m,x1,y1,x2,y2;
int main ( )
{
p[0].h = -1;
scanf ( "%d" , &t );
while ( t-- )
{
scanf ( "%d" , &n );
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%d%d%d" , &p[i].x , &p[i].y , &p[i].h );
build ( 1 , 1 , n );
scanf ( "%d" , &m );
for ( int i = 0 ; i < m ; i++ )
{
scanf ( "%d%d%d%d" , &x1 , &y1 , &x2 , &y2 );
if ( x1 > x2 ) swap ( x1 , x2 );
if ( y1 > y2 ) swap ( y1 , y2 );
int id = query ( 1 , x1 , y1 , x2 , y2 );
if ( p[id].h > -1 )
{
printf ( "%d %d %d\n" , p[id].x , p[id].y , p[id].h );
update ( 1 , id );
}
else puts ( "Where are the birds?" );
}
}
}