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

#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. c/c++中system函数在Linux和windows下区别

    windows 在windows下的system函数中命令可以不区别大小写! 功 能: 发出一个DOS命令 #include <stdlib.h> int system(char *com ...

  2. 【转】HP laserjet p2055dn的自动双面打印功能

    原文网址:http://zhidao.baidu.com/link?url=n_NW7Qfa_7HlrEhLucdvKO43jj3SpFXJhGAfQ-WqF979jm80eUv8s1atqtxE7w ...

  3. Zen Coding改名Emmet-功能更智能化

    早在2009年,谢尔盖Chikuyonok写了一篇文章,提出了一种新的编写HTML和CSS代码的方式.这一革命性的插件,被称为zen coding,多年来已帮助许多开发人员,现在已达到一个新的水平. ...

  4. 1045 access denied for user 'root'@'localhost' using password yes

    mysql -u root -p 方法一:  # /etc/init.d/mysql stop  # mysqld_safe --user=mysql --skip-grant-tables --sk ...

  5. JDBC--数据库链接及相关方法的封装

    使用的是MySQL数据库,首先导入驱动类,然后根据数据库URL和用户名密码获得数据的链接.由于使用的是MySQL数据库,它的URL一般为,jdbc:mysql://主机地址:端口号/库名. 下面是封装 ...

  6. 北京师范大学第十六届程序设计竞赛决赛 C萌萌哒身高差

    链接:https://www.nowcoder.com/acm/contest/117/C来源:牛客网 萌萌哒身高差 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...

  7. 使用DOSGi在OSGi环境下发布Web Services

    前言 Apache CXF是一个开源的服务框架项目,而Distributed OSGi子项目提供了基于OSGi远程服务规范的分布式组件实现.它使用Web Services,HTTP上的SOAP手段实现 ...

  8. Windows Server 2012十大实用快捷键组合

    在本文中,我们将一起体验快捷键如何在微软最新服务器操作系统中帮助用户提升工作效率. 微软推出的最新服务器操作系统比我印象中任何一款前代Windows Server产品都依赖于键盘操作——当然,这些产品 ...

  9. node的express中间件之directory

    direcotry中间件用于在浏览器中流出网站某个目录下的所有子目录及文件. app.use(express.directory(path,[options])); 查看网站根目录下的文件及目录 va ...

  10. node中一个基本的HTTP客户端向本地的HTTP服务器发送数据

    上一篇讲到了node可以轻松的向其他请求数据. 这一篇就来讲讲向本地服务器的数据交互. HTTP服务器代码,s.js var http=require("http"); var s ...