解题关键: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. WPF自定义控件之图形解锁控件 ScreenUnLock

    ScreenUnLock 与智能手机上的图案解锁功能一样.通过绘制图形达到解锁或记忆图形的目的. 本人突发奇想,把手机上的图形解锁功能移植到WPF中.也应用到了公司的项目中. 在创建ScreenUnL ...

  2. gradle 插件

    1. 系统内置插件的应用 a. 二进制 apply plugin :"pluginname" 比如: java b. 脚本插件 apply from : "version ...

  3. 怎么安装Docker CE 17( Centos 7)

    Docker CE for Centos 7 yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manage ...

  4. 执行npm install报错:npm ERR! code EINTEGRITY

    命令行执行npm install报错如下: D:\frontend\viewsdev>npm install npm ERR! code EINTEGRITY npm ERR! sha512-8 ...

  5. 性能优化之mysql索引优化

    sql及索引优化 如何通过慢查询日志发现有问题的sql? 查询次数多且每次查询占用时间长的sql通常为pt-query-digest分析的前几个查询 IO大的sql注意pt-query-digest分 ...

  6. xunsearch之php索引维护(四)

    1.添加文档 $xs = new XS('njw'); $index = $xs->index; $data = array( 'pid' => 234, // 此字段为主键,必须指定 ' ...

  7. CMapStringToPtr添加与释放

    // 创建MapStringToPtr CMapStringToPtr m_prjFiles; CStringList m_fileList; m_fileList.AddTail(_T(" ...

  8. struts2学习(12)struts2验证框架2.自定义验证

    一.例子需求: 对敏感词进行验证: 将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器: com.cy.validators.SensitiveWor ...

  9. hadoop Partiton中的字符串Hash函数改进

    最近的MapReduce端的Partition根据map生成的Key来进行哈希,导致哈希出来的Reduce端处理任务数量非常不均匀,有些Reduce端处理的数据量非常小(几分钟就执行完成,而最后的pa ...

  10. c# 关键字学习

    partial class Test { public string str; public void get() { } } partial class Test { public int i; p ...