题目大意:

平面上n个点,每次给出一个点,求这个点的k远点

题解:

什么叫做k远点呢。。。

1 2 3 4 5中5是第一远,4是第二远...

看来我语文学的不好

那么我们直接上k-D Tree求k邻近的方式求k远离即可

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
const ll maxn = 100010;
const ll dem = 2;
struct Node{
Node *ch[2];
ll pos[2];ll id;
ll minn[2],maxx[2];
void update(){
for(ll d=0;d<dem;++d){
minn[d] = min(pos[d],ch[0]->minn[d],ch[1]->minn[d]);
maxx[d] = max(pos[d],ch[0]->maxx[d],ch[1]->maxx[d]);
}
}
}*null;
Node akdjalskjalksdlkasdlkjlkjflsgdjasdf;
Node T[maxn];
inline void init(){
null = &akdjalskjalksdlkasdlkjlkjflsgdjasdf;
null->ch[0] = null->ch[1] = null;
for(ll d=0;d<dem;++d){
null->pos[d] = 0;
null->minn[d] = 0x3f3f3f3f;
null->maxx[d] = -0x3f3f3f3f;
}
}
ll now,split[maxn];
inline bool cmp(const Node &a,const Node &b){
return a.pos[split[now]] < b.pos[split[now]];
}
Node* build(ll l,ll r,ll s){
if(l > r) return null;
ll mid = (l+r) >> 1;
split[now = mid] = s % dem;
nth_element(T+l,T+mid,T+r+1,cmp);
Node *p = &T[mid];
p->ch[0] = build(l,mid-1,s+1);
p->ch[1] = build(mid+1,r,s+1);
p->update();return p;
}
struct Data{
ll dis;ll id;
bool operator < (const Data &a)const{
if(dis != a.dis) return dis > a.dis;
return id < a.id;
}
Data(){}
Data(ll a,ll b){dis=a;id=b;}
};
priority_queue<Data>q;
inline ll sqr(ll x){return x*x;}
Node op;ll k;
inline ll md(Node *p){
ll ret = .0;
ret = max(ret,sqr(p->minn[0] - op.pos[0]) + sqr(p->minn[1] - op.pos[1]));
ret = max(ret,sqr(p->minn[0] - op.pos[0]) + sqr(p->maxx[1] - op.pos[1]));
ret = max(ret,sqr(p->maxx[0] - op.pos[0]) + sqr(p->minn[1] - op.pos[1]));
ret = max(ret,sqr(p->maxx[0] - op.pos[0]) + sqr(p->maxx[1] - op.pos[1]));
return ret;
}
void query(Node *p){
if(p == null) return;
ll dis = 0;
for(ll d=0;d<dem;++d) dis += sqr(op.pos[d] - p->pos[d]);
if(q.size() < k){
q.push(Data(dis,p->id));
}else if(Data(dis,p->id) < q.top()){
q.pop();q.push(Data(dis,p->id));
}
if(md(p->ch[0]) < md(p->ch[1])) swap(p->ch[0],p->ch[1]); if(p->ch[0] != null && ((q.size() < k) || (md(p->ch[0]) >= q.top().dis))) query(p->ch[0]);
if(p->ch[1] != null && ((q.size() < k) || (md(p->ch[1]) >= q.top().dis))) query(p->ch[1]);
}
int main(){
init();
ll n;read(n);
for(ll i=1;i<=n;++i){
for(ll d=0;d<dem;++d){
read(T[i].pos[d]);
}
T[i].ch[0] = T[i].ch[1] = null;
T[i].id = i;T[i].update();
}
Node *root = build(1,n,1);
ll m;read(m);
while(m--){
for(ll d=0;d<dem;++d) read(op.pos[d]);
read(k);
while(!q.empty()) q.pop();
query(root);
printf("%lld\n",q.top().id);
}
getchar();getchar();
return 0;
}

bzoj 2626: JZPFAR k-D树的更多相关文章

  1. BZOJ 2626 JZPFAR(KD-tree)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2626 题意:平面上有n个点.现在有m次询问,每次给定一个点(px, py)和一个整数k, ...

  2. BZOJ 2626: JZPFAR KDtree + 堆

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000000000 #define mid ((l ...

  3. bzoj 2626: JZPFAR【KD-tree】

    和3053差不多,把pair first做成负数就可以用大根堆维护了 注意:要开long long:比较的时候因为编号也占权重所以要比较pair:编号不是mid!不是mid!是初始输入的那个编号!搞混 ...

  4. Bzoj 2752 高速公路 (期望,线段树)

    Bzoj 2752 高速公路 (期望,线段树) 题目链接 这道题显然求边,因为题目是一条链,所以直接采用把边编上号.看成序列即可 \(1\)与\(2\)号点的边连得是. 编号为\(1\)的点.查询的时 ...

  5. BZOJ 1227 虔诚的墓主人(离散化+树状数组)

    题目中矩形的尺寸太大,导致墓地的数目太多,如果我们统计每一个墓地的虔诚度,超时是一定的. 而常青树的数目<=1e5.这启发我们从树的方向去思考. 考虑一行没有树的情况,显然这一行的墓地的虔诚度之 ...

  6. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...

  7. [BZOJ 3110] [Zjoi2013] K大数查询 【树套树】

    题目链接: BZOJ - 3110 题目分析 这道题是一道树套树的典型题目,我们使用线段树套线段树,一层是区间线段树,一层是权值线段树.一般的思路是外层用区间线段树,内层用权值线段树,但是这样貌似会很 ...

  8. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  9. BZOJ 3110: [Zjoi2013]K大数查询 [树套树]

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6050  Solved: 2007[Submit][Sta ...

随机推荐

  1. Linux系统下授权MySQL账户访问指定数据库和数据库操作

    Linux系统下授权MySQL账户访问指定数据库 需求: 1.在MySQL中创建数据库mydata 2.新建MySQL账户admin密码123456 3.赋予账户admin对数据库mydata具有完全 ...

  2. ie6中利用jquery居中

    1.利用jquery居中代码 <script type="text/javascript"> $hwidth=parseInt($(window).width()); ...

  3. ck-reset css(2016/5/13)

    /**rest by 2016/05/04 */ * {box-sizing: border-box;} *:before,*:after {box-sizing: border-box;} body ...

  4. Jenkins--Run shell command in jenkins as root user?

    You need to modify the permission for jenkins user so that you can run the shell commands. You can i ...

  5. 目标检测之人头检测(HaarLike Adaboost)---高密度环境下行人检测和统计

    实验程序视频 下载 1 问题描述 高密度环境下的行人统计一直没有得到很好的解决,主要原因是对高密度人群中的行人检测和跟踪是一个很难的问题,如下图所示环境,存在的困难包括: 检测方面: 由于人群整体处于 ...

  6. wpf 获取datagrid 模板列中的控件

    目前采用的 方法  (网上提供的一款) public static DataGridRow GetRow(DataGrid datagrid, int columnIndex)        {    ...

  7. 用Newtonsoft将json串转为对象的方法(详解)

    首先,将json串转为一个JObject对象: JObject jo = (JObject)JsonConvert.DeserializeObject(CurrentSelectedItemReq) ...

  8. ECMall验证码问题

    一.确认问题: 输入下面这个地址:http://你的网址/index.php?app=captcha&876589486 打开后出现:mt_rand(): max(0) is smaller ...

  9. 公网yum 源地址

    1. centos5.*  公网yum 源地址 [root@web ~]# cd /etc/yum.repos.d/[root@web yum.repos.d]# wget -O /etc/yum.r ...

  10. cmake使用第三方库

    1 link_directories和target_link_libraries 1.1 link_directories 告诉linker去这些目录去找library. 1.2 target_lin ...