传送门

分析

首先给大家推荐一个非常好的KDTree笔记 here

此题就是用优先队列维护距离最远的k个,最后输出队首元素即可

估价函数就是max和min两点到

询问点的最远距离

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define int long long
const int inf = 1e18;
struct kd {
int d[],mx[],mn[],le,ri,id;
};
kd t[],now;
int n,m,root,wh;
inline bool operator < (kd a,kd b){
return a.d[wh]<b.d[wh];
}
struct node {
int d,id;
};
inline bool operator < (node a,node b){
return a.d>b.d||((a.d==b.d)&&(a.id<b.id));
}
inline void up(int rt){
for(int i=;i<;i++){
t[rt].mn[i]=min(t[rt].mn[i],min(t[t[rt].le].mn[i],t[t[rt].ri].mn[i]));
t[rt].mx[i]=max(t[rt].mx[i],max(t[t[rt].le].mx[i],t[t[rt].ri].mx[i]));
}
}
inline void build(int &x,int le,int ri,int wwh){
wh=wwh;
int mid=(le+ri)>>;
x=mid;
nth_element(t+le,t+x,t+ri+);
for(int i=;i<;i++)
t[x].mx[i]=t[x].mn[i]=t[x].d[i];
if(le<x)build(t[x].le,le,mid-,wwh^);
if(ri>x)build(t[x].ri,mid+,ri,wwh^);
up(x);
}
priority_queue<node>q;
inline int getd(kd a,kd b){
return (a.d[]-b.d[])*(a.d[]-b.d[])+(a.d[]-b.d[])*(a.d[]-b.d[]);
}
inline int calc(int x){
if(!x)return -inf;
int res=;
for(int i=;i<;i++)
res+=max((t[x].mx[i]-now.d[i])*(t[x].mx[i]-now.d[i]),(t[x].mn[i]-now.d[i])*(t[x].mn[i]-now.d[i]));
return res;
}
inline void qurey(int x){
if(!x)return;
int dl=calc(t[x].le),dr=calc(t[x].ri),d=getd(t[x],now);
if(d>q.top().d||(d==q.top().d&&t[x].id<q.top().id)){
q.pop();
q.push((node){d,t[x].id});
}
if(dl>dr){
if(dl>=q.top().d)qurey(t[x].le);
if(dr>=q.top().d)qurey(t[x].ri);
}else {
if(dr>=q.top().d)qurey(t[x].ri);
if(dl>=q.top().d)qurey(t[x].le);
}
}
signed main(){
int i,j,k;
t[].mn[]=t[].mn[]=inf;
t[].mx[]=t[].mx[]=-inf;
scanf("%lld",&n);
for(i=;i<=n;i++){
scanf("%lld%lld",&t[i].d[],&t[i].d[]);
t[i].id=i;
}
build(root,,n,);
scanf("%lld",&m);
for(i=;i<=m;i++){
while(!q.empty())q.pop();
scanf("%lld%lld%lld",&now.d[],&now.d[],&k);
for(j=;j<=k;j++)q.push((node){-,-});
qurey(root);
printf("%lld\n",q.top().id);
}
return ;
}
05-16 03:06