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

#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. WPF实现Twitter按钮效果(转)

    最近上网看到这个CSS3实现的Twitter按钮,感觉很漂亮,于是想用WPF来实现下. 实现这个效果,参考了CSS3 原文地址:http://www.html5tricks.com/css3-twit ...

  2. webpack 简单使用

    备注:  使用yarn 结合npm 模块进行简单项目开发 1. 安装 yarn init yarn add webpack --dev yarn global add live-server 2. 添 ...

  3. 通过openresty && tengine && nginx 动态添加资源到 html 页面

    1. 目标 不用修改后端代码,但是还需要进行js .css 等资源文件的加载 主要的场景是进行统计分析 2.技术实现 通过服务器扩展进行动态添加 iis 可以添加模块,nginx 可以使用  sub_ ...

  4. 【转】简述configure、pkg-config、pkg_config_path三者的关系

    原文网址:http://www.mike.org.cn/articles/description-configure-pkg-config-pkg_config_path-of-the-relatio ...

  5. wordpress分享到微信无缩略图的问题

    牛逼的老板想把他牛逼的艺术品分享给牛逼的画家和藏家简直苦逼了我这个程序狗,他想要的结果是这样的qq和微信上依次为     但是牛逼的微信越来越“抠”了现在不是微信开发者用不了他的分享接口不给你显示缩略 ...

  6. sed命令n,N,d,D,p,P,h,H,g,G,x解析

    1.sed执行模板=sed '模式{命令1;命令2}'即逐行读入模式空间,执行命令,最后输出打印出来2.为方便下面,先说下p和P,p打印当前模式空间内容,追加到默认输出之后,P打印当前模式空间开端至\ ...

  7. linux(7)

    第十七单元 Samba服务 [本节内容]1. 掌握samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件.2. 掌握samba服务的启动.停止.重启service ...

  8. bzoj4336: BJOI2015 骑士的旅行

    Description 在一片古老的土地上,有一个繁荣的文明. 这片大地几乎被森林覆盖,有N座城坐落其中.巧合的是,这N座城由恰好N-1条双 向道路连接起来,使得任意两座城都是连通的.也就是说,这些城 ...

  9. 列表的使用2,深COPY和浅COPY,循环列表,步长切片

    name2=names.copy() 下面我看几种奇怪的现象: 首先把源列表copy以后,把源列表第2个数值修改.那么没毛病. 如果源列表里,还包含了一个子列表:这也没毛病 如果我们这个时候修改子列表 ...

  10. 手势GestureDetector.OnGestureListener事件的调起

    @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionE ...