KD树,看学长的代码,半抄半写的。。。
数据可能有错,题目说了 1≤x,y,z≤100000。。
但是可能有0,我把打掉的鸟设为0就WA,设为-1就AC了。。。
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 100010
#define eps 1e-6
#define mod 10007
#define INF 99999999
#define lowbit(x) (x&(-x))
//#define lson o<<1, L, mid
//#define rson o<<1 | 1, mid+1, R
typedef long long LL;
using namespace std;
struct node
{
node *lson, *rson;
int lx, rx, ly, ry;
int x, y, loc;
int maxv;
}*head, no[maxn], *top;
struct point
{
int x, y, z;
}po[maxn];
int split[maxn];
int vx[maxn], vy[maxn];
int LX, LY, RX, RY, ans;
int ux, uy, uz;
int n, m;
int cmpx(point a, point b)
{
return a.x < b.x ||(a.x == b.x && a.y < b.y);
}
int cmpy(point a, point b)
{
return a.y < b.y ||(a.y == b.y && a.x < b.x);
}
void pushup(node *p)
{
if(p->lson) {
p->lx = min(p->lx, p->lson->lx);
p->rx = max(p->rx, p->lson->rx);
p->ly = min(p->ly, p->lson->ly);
p->ry = max(p->ry, p->lson->ry);
p->maxv = max(p->maxv, p->lson->maxv);
}
if(p->rson) {
p->lx = min(p->lx, p->rson->lx);
p->rx = max(p->rx, p->rson->rx);
p->ly = min(p->ly, p->rson->ly);
p->ry = max(p->ry, p->rson->ry);
p->maxv = max(p->maxv, p->rson->maxv);
}
}
node* build(int L, int R, int spt)
{
if(L>R) return NULL;
int mid = (R + L) >>1;
split[mid] = spt;
node *p = top++;
if(spt) nth_element(po+L, po+mid, po+R+1, cmpx);
else nth_element(po+L, po+mid, po+R+1, cmpy);
p->x = po[mid].x, p->y = po[mid].y;
p->maxv = p->loc = po[mid].z;
p->lx = p->rx = po[mid].x;
p->ly = p->ry = po[mid].y;
p->lson = build(L, mid-1, !spt);
p->rson = build(mid+1, R, !spt);
pushup(p);
return p;
}
void query(node *p)
{
if(!p) return;
if(p->maxv < ans) return;
if(p->lx >= LX && p->rx <= RX && p->ly >= LY && p->ry <= RY){
if(ans < p->maxv) ans = p->maxv;
return;
}
if(p->x >=LX && p->x <=RX && p->y >= LY && p->y <=RY)
if(ans < p->loc) ans = p->loc;
if(p->lson)
if(p->lson->lx <= RX && p->lson->rx >= LX && p->lson->ly <= RY && p->lson->ry >= LY)
query(p->lson);
if(p->rson)
if(p->rson->lx <= RX && p->rson->rx >= LX && p->rson->ly <= RY && p->rson->ry >= LY)
query(p->rson);
}
void updata(node *p)
{
p->maxv = p->loc;
if(p->x == ux && p->y == uy && p->maxv == uz){
p->maxv = p->loc = -1;
pushup(p);
return;
}
if(p->lson)
if(p->lson->lx <= ux && p->lson->rx >= ux && p->lson->ly <= uy && p->lson->ry >= uy)
updata(p->lson);
if(p->rson)
if(p->rson->lx <= ux && p->rson->rx >= ux && p->rson->ly <= uy && p->rson->ry >= uy)
updata(p->rson);
pushup(p);
}
void init(void)
{
top = no;
memset(split, 0, sizeof split);
}
void read(void)
{
int i;
scanf("%d", &n);
for(i = 1; i <= n; i++) {
scanf("%d%d%d", &po[i].x, &po[i].y, &po[i].z);
vx[po[i].z] = po[i].x, vy[po[i].z] = po[i].y;
}
}
void work(void)
{
scanf("%d", &m);
while(m--){
scanf("%d%d%d%d", &LX, &LY, &RX, &RY);
ans=-1;
query(head);
if(ans==-1){
printf("Where are the birds?\n");
continue;
}
printf("%d %d %d\n", vx[ans], vy[ans], ans);
ux=vx[ans], uy=vy[ans], uz=ans;
updata(head);
}
}
int main(void)
{
int _;
while(scanf("%d", &_)!=EOF) {
while(_--){
init();
read();
head=build(1, n, 0);
work();
}
}
return 0;
}