传送门

类似于p4357

不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define inf 0x3f3f3f3f
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5;
struct node{int v[2],mn[2],mx[2],l,r,id,idmn;}tr[N],S;
int n,m,K,rt,k;
inline bool operator <(const node &a,const node &b){return a.v[K]<b.v[K];}
struct qqq{
int id;ll dis;
qqq(){}
qqq(int id,ll dis):id(id),dis(dis){}
inline bool operator <(const qqq &b)const{return dis==b.dis?id<b.id:dis>b.dis;}
};priority_queue<qqq>q;
void upd(int p){
int l=tr[p].l,r=tr[p].r;tr[p].idmn=tr[p].id;
if(l)cmin(tr[p].idmn,tr[l].idmn);if(r)cmin(tr[p].idmn,tr[r].idmn);
fp(i,0,1){
tr[p].mn[i]=tr[p].mx[i]=tr[p].v[i];
if(l)cmin(tr[p].mn[i],tr[l].mn[i]),cmax(tr[p].mx[i],tr[l].mx[i]);
if(r)cmin(tr[p].mn[i],tr[r].mn[i]),cmax(tr[p].mx[i],tr[r].mx[i]);
}
}
int build(int l,int r,int k){
K=k;int mid=(l+r)>>1;nth_element(tr+l,tr+mid,tr+r+1);
if(l<mid)tr[mid].l=build(l,mid-1,k^1);
if(mid<r)tr[mid].r=build(mid+1,r,k^1);
upd(mid);return mid;
}
inline ll sqr(ll x){return x*x;}
inline ll ck(int p){
return max(sqr(tr[p].mn[0]-S.v[0]),sqr(tr[p].mx[0]-S.v[0]))+
max(sqr(tr[p].mn[1]-S.v[1]),sqr(tr[p].mx[1]-S.v[1]));
}
inline ll dis(int p){return sqr(tr[p].v[0]-S.v[0])+sqr(tr[p].v[1]-S.v[1]);}
void query(int p){
ll dl=-inf,dr=-inf,dd=dis(p);int id=tr[p].id;
if(dd>q.top().dis||(dd==q.top().dis&&id<q.top().id))q.pop(),q.push(qqq(id,dd));
if(tr[p].l)dl=ck(tr[p].l);if(tr[p].r)dr=ck(tr[p].r);
if(dl>dr){
if(dl>q.top().dis||(dl==q.top().dis&&tr[tr[p].l].idmn<q.top().id))query(tr[p].l);
if(dr>q.top().dis||(dr==q.top().dis&&tr[tr[p].r].idmn<q.top().id))query(tr[p].r);
}else{
if(dr>q.top().dis||(dr==q.top().dis&&tr[tr[p].r].idmn<q.top().id))query(tr[p].r);
if(dl>q.top().dis||(dl==q.top().dis&&tr[tr[p].l].idmn<q.top().id))query(tr[p].l);
}
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();fp(i,1,n)tr[i].v[0]=read(),tr[i].v[1]=read(),tr[i].id=i;
rt=build(1,n,0),m=read();
while(m--){
while(!q.empty())q.pop();S.v[0]=read(),S.v[1]=read(),k=read();
fp(i,1,k)q.push(qqq(0,-inf));
query(rt);ll d=q.top().dis;print(q.top().id);
}return Ot(),0;
}

P2093 [国家集训队]JZPFAR(KDTree)的更多相关文章

  1. p2093 [国家集训队]JZPFAR

    传送门 分析 首先给大家推荐一个非常好的KDTree笔记 here 此题就是y9ong优先队列维护距离最远的k个,最后输出队首元素即可 估价函数就是max和min两点到 询问点的最远距离 代码 #in ...

  2. luogu P2093 [国家集训队]JZPFAR

    传送门 要维护平面上点的信息,所以可以用KD-tree来维护,然后维护一个大小为\(k\)的堆,每次从根开始遍历,遇到一个点就看能不能作为前\(k\)远的点,也就是看能不能把堆中最近的点给替换掉.如果 ...

  3. luogu2093 [国家集训队]JZPFAR

    题面不符?-- #include <algorithm> #include <iostream> #include <cstdio> using namespace ...

  4. [国家集训队]JZPFAR

    嘟嘟嘟 k-d tree模板之二:查询第k大距离.(所以是怎么上黑的) 因为k-d tree的查询就是暴力嘛,所以我就想到了一个很暴力的做法:每一次查询用一个长度为k的优先队列维护.按距离递增,编号递 ...

  5. [国家集训队2012]JZPFAR

    [国家集训队2012]JZPFAR 题目 平面上有n个点.现在有m次询问,每次给定一个点(px, py)和一个整数k,输出n个点中离(px, py)的距离第k大的点的标号.如果有两个(或多个)点距离( ...

  6. BZOJ 2039: [2009国家集训队]employ人员雇佣

    2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1369  Solved: 667[Submit ...

  7. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  8. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7676  Solved: 3509[Subm ...

  9. [转] ACM中国国家集训队论文集目录(1999-2009)

    国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...

随机推荐

  1. c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down

    c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down http://blog.csdn.net/delphizhou/article/details/30 ...

  2. 从零开始写STL—模板元编程之tuple

    tuple Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generali ...

  3. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  4. 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...

  5. [Bzoj4521][Cqoi2016]手机号码(数位dp)

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 870  Solved: 505[Submit][Status ...

  6. 洛谷 P3807 【模板】卢卡斯定理

    P3807 [模板]卢卡斯定理 题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105) 求 C_{n+m}^{m}\ mod\ pCn+mm ...

  7. Visual Studio VS如何卸载Visual assistant

    1 点击工具-扩展管理器   2 选中Visual Assist X,点击卸载即可.                            

  8. 【Facebook的UI开发框架React入门之八】Image的使用简单介绍(iOS平台)-goodmao

    --------------------------------------------------------------------------------------------------- ...

  9. HDU 1824 Let&#39;s go home (2-SAT判定)

    Let's go home Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. 3 TypeScript 语法特性

    一.类型注解(Type annotations) TypeScript 通过类型注解提供静态类型以在编译时启动类型检查,简单来说,就是指定数据类型,它会在代码运行的时候,对传入的数据进行数据类型匹配检 ...