[hdu4347]The Closest M Points(平衡树式kdtree)
解题关键:模板题(结合起来了)
#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)的更多相关文章
- [hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...
- 【BZOJ】3053: The Closest M Points(kdtree)
http://www.lydsy.com/JudgeOnline/problem.php?id=3053 本来是1a的QAQ.... 没看到有多组数据啊.....斯巴达!!!!!!!!!!!!!!!! ...
- HDU 4347 - The Closest M Points - [KDTree模板题]
本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...
- hdu4347The Closest M Points kdtree
kdtree讲解: https://blog.csdn.net/qing101hua/article/details/53228668 https://blog.csdn.net/acdreamers ...
- HDU 4347 The Closest M Points (kdTree)
赤果果的kdTree. 学习传送门:http://www.cnblogs.com/v-July-v/archive/2012/11/20/3125419.html 其实就是二叉树的变形 #includ ...
- bzoj 3053: The Closest M Points【KD-tree】
多维KDtree板子 左右儿子的估价用mn~mx当区间,假设区间里的数都存在:k维轮着做割点 #include<iostream> #include<cstdio> #incl ...
- hud 4347 The Closest M Points(KD-Tree)
传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...
- 【kd-tree】bzoj3053 The Closest M Points
同p2626.由于K比较小,所以不必用堆. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- 【hdu4347】The Closest M Points 【KD树模板】
题意 一个k维空间,给出n个点的坐标,给出t个询问,每个询问给出一个点的坐标和一个m.对于每个询问找出跟这个点最接近的m个点 分析 kd树的模板题. #include <cstdio> # ...
随机推荐
- UVA12716 GCD XOR 数论数学构造
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010682557/article/details/36204645 题目给你一个N,让你求 两个数 ...
- 在debian上安装最新版erlang
参考这里https://www.erlang-solutions.com/downloads/download-erlang-otp 源码安装的无视 sudo gvim /etc/apt/source ...
- js实现定时调用的函数setInterval()
setInterval是一个实现定时调用的函数,可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval方法会不停地调用函数,直到 clearInterval被调用或窗口被关闭 定义 ...
- 初学FPGA一些建议
数字电路: 这是大学里的基本课程 ,涵盖了一般数字电路的组合电路.时序电路.寄存器传输.储存器以及可编程逻辑电路(FPGA 就是其中一种),还有比较好的添加了计算机的指令集结构.处理器设计等计算机方面 ...
- java代码------------条件运算符 ?:
总结: package com.sads; //?: //这个运算符是条件运算符 //条件式?值:值 public class Sd { public static void main(String[ ...
- java里面的public static void main(String[] args)
package com.java_1; public class Hello { public static void main(String[] args){ System.out.println( ...
- Ladder面积
package com.hanqi; import javax.swing.plaf.synth.SynthSeparatorUI; //梯形 public class Ladder { double ...
- limesurvey的问卷类型
- 使用GET方式提交的表单遇到的问题
经常使用表单,一直使用的都是POST方式,POST将数据封装到请求体中,相对于GET安全一点:而POST处理中文编码问题也比GET简单(GET需要将URL编码,后台接受到后还需要解码).今天我想要使用 ...
- jaxp使用笔记
XML文件的解析技术有DOM和SAX方式,在Android中还有pull解析方式,这里不再讨论 DOM解析的方式和js中的DOM操作是一致的,DOM解析一次将文档加载入内存建立树型模型,但是如果XML ...