解题关键:kdtree模板题,距离某点最近的m个点。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
#define sq(x) (x)*(x)
using namespace std;
typedef long long ll;
const int N=; int idx,k,n,m,q;
struct node{
int x[];
bool operator<(const node &u)const{
return x[idx]<u.x[idx];
}
}P[N];
//线段树形式kd-tree
typedef pair<double,node>PDN;
priority_queue<PDN>que;
struct KD_Tree{
int sz[N<<];
node p[N<<];
void build(int rt,int l,int r,int dep){
if(l>r) return;
int mid=(l+r)>>;
idx=dep%k;sz[rt]=r-l;
sz[rt<<]=sz[rt<<|]=-;
nth_element(P+l,P+mid,P+r+);
p[rt]=P[mid];
build(rt<<,l,mid-,dep+);
build(rt<<|,mid+,r,dep+);
} void query(int i,int m,int dep,node a){//寻找m个。
if(sz[i]==-) return;
PDN tmp=PDN(,p[i]);//新建的tmp,first更新为0了
for(int j=;j<k;j++)
tmp.first+=sq(tmp.second.x[j]-a.x[j]);//距离目标点的距离
int lc=i<<,rc=i<<|,dim=dep%k,flag=;
if(a.x[dim]>=p[i].x[dim]) swap(lc,rc);
if(~sz[lc]) query(lc,m,dep+,a);//~sz,sz!=-1
if(que.size()<m) que.push(tmp),flag=;
else{
if(tmp.first<que.top().first) que.pop(),que.push(tmp);
if(sq(a.x[dim]-p[i].x[dim])<que.top().first) flag=;//求到面的距离,空间相交
}
if(~sz[rc]&&flag) query(rc,m,dep+,a);
}
}KDT; int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=;i<n;i++)
for(int j=;j<k;j++)
scanf("%d",&P[i].x[j]);
KDT.build(,,n-,);
scanf("%d",&q);
while(q--){
node now;
for(int i=;i<k;i++)
scanf("%d",&now.x[i]);
scanf("%d",&m);
KDT.query(,m,,now);
node pp[];
int t=;
while(!que.empty()){
pp[++t]=que.top().second;
que.pop();
}
printf("the closest %d points are:\n",m);
for(int i=m;i>;i--) for(int j=;j<k;j++) printf("%d%c",pp[i].x[j],j==k-?'\n':' ');
}
}
return ;
}

[hdu4347]The Closest M Points(线段树形式kd-tree)的更多相关文章

  1. Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)

    Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...

  2. CodeForces 19D Points (线段树+set)

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  3. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

  4. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  5. CF 19D - Points 线段树套平衡树

    题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...

  6. [Codeforces19D]Points 线段树

    大致题意: 给出n个询问,每次询问有三种: 1.往平面上加一个点 2.删除平面上的一个点 3.给出一个点p,查询平面上某点q,使得q.x>p.x且q.y>p.y,输出x轴坐标最小的q,若有 ...

  7. [hdu4347]The Closest M Points(平衡树式kdtree)

    解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...

  8. Codeforces 1140F Extending Set of Points (线段树分治+并查集)

    这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...

  9. 【HDU6621】K-th Closest Distance【线段树】

    题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...

随机推荐

  1. oracle之 sqlplus prelim 参数介绍 ( 处理hang )

    从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤 ...

  2. jdk1.8新特性应用之Iterable

    我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback ...

  3. JDBC 流程

    转载地址:https://blog.csdn.net/suwu150/article/details/52744952 JDBC编程的六个步骤:    准备工作中导入ojdbc文件,然后右键选中添加路 ...

  4. 64位windows系统安装javaee6.0不成功解决方案

    64位windows系统安装javaee6.0不成功解决方案 (2013-01-19 14:59:51) 转载▼ 标签: 杂谈   could not find the required versio ...

  5. erlang的格式化字符串

    往pgsql里面写数据的时候,不能双引号,开始纠结的不行,用拼字符串的形式,后来发现可以格式化字符串,泪奔 data_format.erl -module(data_format). -export( ...

  6. Clustershell集群管理

    在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去 ...

  7. eyoucms 前台 getshell 复现

    漏洞地址:http://www.sch01ar.com/index.php/api/Uploadify/preview 这样子说明存在漏洞 对 <?php phpinfo(); 进行 base6 ...

  8. Vue语法

    第一个Vue示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  9. 3.docker学习之docker与虚拟化

    虚拟化技术是一个总称,是一系列实现虚拟技术的统称.从广义上来说,虚拟化技术包括了虚拟机技术和容器技术, 所谓虚拟化技术最大的特点就是将一个真实的机器进行虚拟地分割,然后分割出来的部分可以独立使用   ...

  10. python+selenium+Firefox+pycharm版本匹配

    window(2018-05-29)最新 python:3.6.1    地址https://www.python.org/downloads/release/python-361/ selenium ...