解题关键:模板题(结合起来了)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#define N 50050
#define inf (1<<30)
#define sq(x) (x)*(x)
using namespace std;
int n,m,dim,rt,k;
struct node{
int p[],minn[],maxx[];
bool operator<(const node &u)const{
return p[dim]<u.p[dim];
}
}a[N];
typedef pair<double,node>PDN;
priority_queue<PDN>que;
struct kd_tree{
int c[N][];
node s[N],q;
void update(int o){//管辖范围
int l=c[o][],r=c[o][];
for(int i=;i<k;i++){
if(l){ s[o].minn[i]=min(s[o].minn[i],s[l].minn[i]); s[o].maxx[i]=max(s[o].maxx[i],s[l].maxx[i]); }
if(r){ s[o].minn[i]=min(s[o].minn[i],s[r].minn[i]); s[o].maxx[i]=max(s[o].maxx[i],s[r].maxx[i]); }
}
}
void add(int o,node t){ for(int i=;i<k;i++)s[o].minn[i]=s[o].maxx[i]=s[o].p[i]=t.p[i]; c[o][]=c[o][]=;}
int dist(node t,int o){
int tmp=;
for(int i=;i<k;i++){
if(s[o].minn[i]>t.p[i])tmp+=sq(s[o].minn[i]-t.p[i]);
if(t.p[i]>s[o].maxx[i])tmp+=sq(t.p[i]-s[o].maxx[i]);
}
return tmp;
}
void build(int &o,int l,int r,int now){
o=(l+r)>>;now%=k;dim=now;
nth_element(a+l,a+o,a+r+);
add(o,a[o]);
if(l!=o) build(c[o][],l,o-,now+);
if(o!=r) build(c[o][],o+,r,now+);
update(o);
}
void ins(int o,int now){
now%=k;
if(q.p[now]<s[o].p[now]){
if(c[o][]) ins(c[o][],now+);
else c[o][]=++n,add(n,q);
}
else{
if(c[o][]) ins(c[o][],now+);
else c[o][]=++n,add(n,q);
}
update(o);
}
void qry(int o){//曼哈顿距离,且只求最短,dis是最短距离
PDN tmp=PDN(,s[o]);
for(int i=;i<k;i++) tmp.first+=sq(s[o].p[i]-q.p[i]);
if(que.size()<m) que.push(tmp);
else{
if(que.top().first>tmp.first){
que.pop();
que.push(tmp);
}
}
int dl=c[o][]?dist(q,c[o][]):inf,dr=c[o][]?dist(q,c[o][]):inf;
if(dl<dr){
if((dl<que.top().first||que.size()<m)&&dl!=inf) qry(c[o][]);
if((dr<que.top().first||que.size()<m)&&dr!=inf) qry(c[o][]);
}else{
if((dr<que.top().first||que.size()<m)&&dr!=inf) qry(c[o][]);
if((dl<que.top().first||que.size()<m)&&dl!=inf) qry(c[o][]);
}
}
}kd; int main(){
while(scanf("%d%d",&n,&k)!=EOF){
for(int i=;i<=n;i++){
for(int j=;j<k;j++)
scanf("%d",&a[i].p[j]);
}
kd.build(rt,,n,);
int t;
scanf("%d",&t);
while(t--){
while(!que.empty()) que.pop();
for(int j=;j<k;j++) scanf("%d",&kd.q.p[j]);
scanf("%d",&m);
kd.qry(rt);
node pp[];
int nn=;
printf("the closest %d points are:\n",m);
while(!que.empty()) pp[++nn]=que.top().second,que.pop();
for(int i=m;i>;i--){
for(int j=;j<k;j++){
printf("%d%c",pp[i].p[j],j==k-?'\n':' ');
}
}
}
}
return ;
}

[hdu4347]The Closest M Points(平衡树式kdtree)的更多相关文章

  1. [hdu4347]The Closest M Points(线段树形式kd-tree)

    解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...

  2. 【BZOJ】3053: The Closest M Points(kdtree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...

  3. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  4. hdu4347The Closest M Points kdtree

    kdtree讲解: https://blog.csdn.net/qing101hua/article/details/53228668 https://blog.csdn.net/acdreamers ...

  5. HDU 4347 The Closest M Points (kdTree)

    赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...

  6. bzoj 3053: The Closest M Points【KD-tree】

    多维KDtree板子 左右儿子的估价用mn~mx当区间,假设区间里的数都存在:k维轮着做割点 #include<iostream> #include<cstdio> #incl ...

  7. hud 4347 The Closest M Points(KD-Tree)

    传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...

  8. 【kd-tree】bzoj3053 The Closest M Points

    同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. 【hdu4347】The Closest M Points 【KD树模板】

    题意 一个k维空间,给出n个点的坐标,给出t个询问,每个询问给出一个点的坐标和一个m.对于每个询问找出跟这个点最接近的m个点 分析 kd树的模板题. #include <cstdio> # ...

随机推荐

  1. js各种效果

    1.JavaScript 仿LightBox内容显示效果 2.固定高度的div,竖直方向出现滚动条,水平方向固定 http://www.jb51.net/css/109928.html <!do ...

  2. eclipse adt调试出错,不能产出apk问题

    The connection to adb is down, and a severe error has occured http://blog.csdn.net/h7870181/article/ ...

  3. oracle之 调整 I/O 相关的等待

    I/O相关竞争等待简介 当Oracle数据库出现I/O相关的竞争等待的时候,一般来说都会引起Oracle数据库的性能低下,发现数据库存在I/O相关的竞争等待一般可以通过以下的三种方法来查看Oracle ...

  4. mysql存储引擎之innodb学习

    innodb引擎特点1.支持事务:支持4个事务隔离级别,支持多版本读. 2.行级锁定(更新时一般是锁定当前行):通过索引实现,全表扫描仍然会是表锁,注意间隙 锁的影响 3.读写阻塞与事务隔离级别有关 ...

  5. laravel的auth用户认证的例子

    参考http://www.cnblogs.com/yjf512/p/4042356.html 需要注意的是,生成的测试数据,password部分必须用laravel自带的hash一下 Hash::ma ...

  6. 2dx 3.0环境配置(mac)

    安装ant brew install ant ant默认的目录在 /usr/local/bin mvim ~/.profile,添加 export ANT_ROOT=/usr/local/bin . ...

  7. [rejected] master -> master (fetch first)(non-fast forward)

    git push 时候遇到的错误: hint: Updates were rejected because the tip of your current branch is behind hint: ...

  8. 谈谈对zynq的浅显理解

    zynq并不能说是一个嵌入arm核的FPGA.从它的启动过程就可以发现,绝对是arm主导的,所以称它为以高性能FPGA为外设的双核arm或许更为合适.以下是优势: 第一个:开发环境的大集成.从hls到 ...

  9. GOF23设计模式之中介者模式(mediator)

    一.中介者模式概述 如果一个系统中对象之间的联系呈现网状结构,对象之间存在大量多对多的关系,导致关系及其复杂,这时可以引入一个中介者对象,使得各个对象只跟中介者对象打交道,从而将复杂的网络结构化为星型 ...

  10. Excel不同工作簿之间提取信息

    Sub 不同工作簿间提取信息() '用于单个字段信息的提取: Dim w As Workbook, wb1 As Workbook, wb2 As Workbook, wb3 As Workbook ...