p2093 [国家集训队]JZPFAR
分析
首先给大家推荐一个非常好的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 ;
}
p2093 [国家集训队]JZPFAR的更多相关文章
- P2093 [国家集训队]JZPFAR(KDTree)
传送门 类似于p4357 不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符 //minamoto #include<bits/stdc++.h> #define R regi ...
- luogu P2093 [国家集训队]JZPFAR
传送门 要维护平面上点的信息,所以可以用KD-tree来维护,然后维护一个大小为\(k\)的堆,每次从根开始遍历,遇到一个点就看能不能作为前\(k\)远的点,也就是看能不能把堆中最近的点给替换掉.如果 ...
- [国家集训队]JZPFAR
嘟嘟嘟 k-d tree模板之二:查询第k大距离.(所以是怎么上黑的) 因为k-d tree的查询就是暴力嘛,所以我就想到了一个很暴力的做法:每一次查询用一个长度为k的优先队列维护.按距离递增,编号递 ...
- luogu2093 [国家集训队]JZPFAR
题面不符?-- #include <algorithm> #include <iostream> #include <cstdio> using namespace ...
- [国家集训队2012]JZPFAR
[国家集训队2012]JZPFAR 题目 平面上有n个点.现在有m次询问,每次给定一个点(px, py)和一个整数k,输出n个点中离(px, py)的距离第k大的点的标号.如果有两个(或多个)点距离( ...
- BZOJ 2039: [2009国家集训队]employ人员雇佣
2039: [2009国家集训队]employ人员雇佣 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1369 Solved: 667[Submit ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7676 Solved: 3509[Subm ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- Oracle导出导入表空间创建
//备份数据库前的sqlplus命令创建数据库dmp存入目录 sqlplus /nolog conn /as sysdba SQL> create or replace directory ex ...
- cassandra的一些概念
分区器Partitioners 在集群内,根据设置的副本数,决定数据如何分发,允许跨机房 具体看 http://teddymaef.github.io/learncassandra/cn/replic ...
- shell中date命令对month进行加减操作的bug
shell脚本中如何取上个月的月份呢?很容易能想到下面的命令: date +%Y%m -d '-1 month' 或者 date +%Y%m -d 'last month' 在大部分情况下这个命令 ...
- (转)Inno Setup入门(十)——操作注册表
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250871 有些程序需要随系统启动,或者需要建立某些文件关联等问题 ...
- poj 3977 Subset(折半枚举+二进制枚举+二分)
Subset Time Limit: 30000MS Memory Limit: 65536K Total Submissions: 5721 Accepted: 1083 Descripti ...
- [Java.Web]从零开始布署 Tomcat
1. 下载 JRE 1.7 2. 下载 Tomcat 7.0.77,我使用的是红圈的压缩包版本,也可以使用绿圈的安装包版本[更省心] 3. 加入环境变量 JRE_HOME .CATALINA_HOME ...
- @Retention 注解的作用
注解@Retention可以用来修饰注解,是注解的注解,称为元注解.Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚 ...
- node操作mongoDB数据库的最基本例子
连接数据库 var mongo=require("mongodb"); var host="localhost"; var port=mongo.Connect ...
- SQL DATE_SUB 函数用法
SQL DATE_SUB 是一个mySql函数.不象SQL DATE_ADD 函数 增加时间值,SQL DATE_SUB 将从一个日期/时间值中减去一个时间值(时间间隔). SQL DATE_SUB ...
- Java int Integer
http://www.cnblogs.com/haimingwey/archive/2012/04/16/2451813.html http://developer.51cto.com/art/200 ...