跟这题一样。
#include<bits/stdc++.h>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void Read(int& x){
char c=nc(),b=1;
for(;c<'0'||c>'9';c=nc())if(c=='-')b=-1;
for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());x*=b;
}
typedef long long ll;
const int N=100010;
int Rt;
struct Node{
int l,r,v[2],id;
int mn[2],mx[2];
}a[N];
struct node{
int x;ll y;
node(int x=0,ll y=0):x(x),y(y){}
bool operator < (node a)const{
return y>a.y||(y==a.y&&x<a.x);
}
};
priority_queue<node>Q;
bool cur;
int x;
bool Cmp(Node a,Node b){
return a.v[cur]<b.v[cur];
}
namespace KDT{
inline void Up(int x){
for(int i=0;i<2;i++){
if(a[x].l){
a[x].mx[i]=max(a[x].mx[i],a[a[x].l].mx[i]);
a[x].mn[i]=min(a[x].mn[i],a[a[x].l].mn[i]);
}
if(a[x].r){
a[x].mx[i]=max(a[x].mx[i],a[a[x].r].mx[i]);
a[x].mn[i]=min(a[x].mn[i],a[a[x].r].mn[i]);
}
}
}
int Build(int l,int r,bool d){
int Mid=l+r>>1;
cur=d;
nth_element(a+l,a+Mid,a+r+1,Cmp);
if(l<Mid)a[Mid].l=Build(l,Mid-1,d^1);
if(r>Mid)a[Mid].r=Build(Mid+1,r,d^1);
Up(Mid);
return Mid;
}
inline ll Sqr(int x){
return 1ll*x*x;
}
inline ll Dis(int x,int y){
ll Ans=0;
for(int i=0;i<2;i++)Ans+=Sqr(a[x].v[i]-a[y].v[i]);
return Ans;
}
inline ll Get(int x){
ll Ans=0;
for(int i=0;i<2;i++)Ans+=max(Sqr(a[x].mn[i]-a[0].v[i]),Sqr(a[x].mx[i]-a[0].v[i]));
return Ans;
}
void Query(int x){
ll t=Dis(x,0);
if(t>Q.top().y||(t==Q.top().y&&a[x].id<Q.top().x))Q.pop(),Q.push(node(a[x].id,t));
ll dl=-2,dr=-2;
if(a[x].l)dl=Get(a[x].l);
if(a[x].r)dr=Get(a[x].r);
if(dl>dr){
if(dl>=Q.top().y)Query(a[x].l);
if(dr>=Q.top().y)Query(a[x].r);
}else{
if(dr>=Q.top().y)Query(a[x].r);
if(dl>=Q.top().y)Query(a[x].l);
}
}
}
int k,n,m;
int main(){
Read(n);
for(int i=1;i<=n;i++){
for(int j=0;j<2;j++)Read(a[i].v[j]),a[i].mx[j]=a[i].mn[j]=a[i].v[j];
a[i].id=i;
}
Rt=KDT::Build(1,n,0);
Read(m);
while(m--){
Read(a[0].v[0]);Read(a[0].v[1]);Read(x);
while(!Q.empty())Q.pop();
while(x--)Q.push(node(n+1,-1));
KDT::Query(Rt);
printf("%d\n",Q.top().x);
}
return 0;
}