[hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键: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)的更多相关文章
- Codeforces 1140F Extending Set of Points 线段树 + 按秩合并并查集 (看题解)
Extending Set of Points 我们能发现, 如果把x轴y轴看成点, 那么答案就是在各个连通块里面的x轴的个数乘以y轴的个数之和. 然后就变成了一个并查集的问题, 但是这个题目里面有撤 ...
- CodeForces 19D Points (线段树+set)
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- Codeforces Beta Round #19D(Points)线段树
D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- [Codeforces19D]Points 线段树
大致题意: 给出n个询问,每次询问有三种: 1.往平面上加一个点 2.删除平面上的一个点 3.给出一个点p,查询平面上某点q,使得q.x>p.x且q.y>p.y,输出x轴坐标最小的q,若有 ...
- [hdu4347]The Closest M Points(平衡树式kdtree)
解题关键:模板题(结合起来了) #include<iostream> #include<cstdio> #include<cstring> #include< ...
- Codeforces 1140F Extending Set of Points (线段树分治+并查集)
这题有以下几个步骤 1.离线处理出每个点的作用范围 2.根据线段树得出作用范围 3.根据分治把每个范围内的点记录和处理 #include<bits/stdc++.h> using name ...
- 【HDU6621】K-th Closest Distance【线段树】
题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...
随机推荐
- open和close函数
1.open函数的使用 调用open函数可以打开或创建一个文件 #include <sys/stat.h> #include <fcntl.h> #include <sy ...
- 数据库的备份与恢复(oracle 11g) (转)
一. 内容与步骤 (注意这里许多步骤需要同学们查资料,理解并消化后才能完成) 1.数据库创建 (1) 安装Oralce11g: (2) 创建至少两个以上用户: (3) 每个用户 ...
- sql的一些事件处理
select getdate() select Convert(varchar(10),getdate(),120) yyyy-mm-ddselect Convert(varchar(20),getd ...
- caffemodel的读取与修改
直接撸代码~ import caffe import numpy as np caffe.set_mode_cpu() net = caffe.Net('myprototxt.prototxt', ' ...
- FPGA与图像处理
用FPGA做图像处理最关键的一点优势就是:FPGA能进行实时流水线运算,能达到最高的实时性.因此在一些对实时性要求非常高的应用领域,做图像处理基本就只能用FPGA.例如在一些分选设备中图像处理基本上用 ...
- struts2学习(6)自定义拦截器-登录验证拦截器
需求:对登录进行验证,用户名cy 密码123456才能登录进去: 登录进去后,将用户存在session中: 其他链接要来访问(除了登录链接),首先验证是否登录,对这个进行拦截: com.cy.mod ...
- Java-Runoob-面向对象:Java 继承-u1
ylbtech-Java-Runoob-面向对象:Java 继承 1.返回顶部 1. Java 继承 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类 ...
- java传递是引用的拷贝,既不是引用本身,更不是对象
java传递是引用的拷贝,既不是引用本身,更不是对象 2008-09-16 04:27:56| 分类: Java SE|举报|字号 订阅 下载LOFTER客户端 1. 简单类型是按值 ...
- java基础循环
一. while循环 示例1:.循环打印1到10之间的值 public class Test1 { public static void main(String[] args) { int i=1;/ ...
- 3_python之路之商城购物车
python之路之商城购物车 1.程序说明:Readme.txt 1.程序文件:storeapp_new.py userinfo.py 2.程序文件说明:storeapp_new.py-主程序 use ...