[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> # ...
随机推荐
- WPF实现Twitter按钮效果(转)
最近上网看到这个CSS3实现的Twitter按钮,感觉很漂亮,于是想用WPF来实现下. 实现这个效果,参考了CSS3 原文地址:http://www.html5tricks.com/css3-twit ...
- jest js 测试框架-简单方便人性化
1. 安装 yarn global add jest-cli or npm install -g jest-cli 备注:可以安装为依赖不用全局安装 2. 项目代码 a. 项目初始化 yarn ini ...
- 再探VIM配置
再探VIM配置 最初找到这个发行版spf13-vim,在ubuntu上用的还比较方便,有很多插件:最近在mac上用,总是不兼容vim,用brew安装了最新的vim,还是跟系统不兼容,总是有问题,于是就 ...
- javascript 中的 arguments,callee.caller,apply,call 区别
记录一下: 1.arguments是一个对象, 是函数的一个特性,只有在函数内才具有这个特性,在函数外部不用使用. 举例: function test(){ alert(typeof argume ...
- Asp.net中的web.config配置文件(转)
最近开始学习.NET的开发,首先碰到的就是web.config的配置问题,把网上大虾的资料转发记录一下,以备不时之需. 原贴路径如下:http://blog.csdn.net/hbqhdlc/arti ...
- CRC 自动判断大端 小端
/* aos_crc64.c -- compute CRC-64 * Copyright (C) 2013 Mark Adler * Version 1.4 16 Dec 2013 Mark Adle ...
- java代码-----计算器,界面+功能+boolean
总结:还是那个不懂代码放在哪里好?不知道怎么定义一些关键性变量.比如boolean 型的. package com.sads; import java.awt.BorderLayout; import ...
- sklearn: TfidfVectorizer 中文处理及一些使用参数
TfidfVectorizer可以把原始文本转化为tf-idf的特征矩阵,从而为后续的文本相似度计算,主题模型,文本搜索排序等一系列应用奠定基础.基本应用如: #coding=utf-8 from s ...
- Centos6-7安装Python3.5以及SSL的编译安装,识别https
Python3中无法导入ssl模块的解决办法 如果你发现在python3脚本运行过程中发现涉及到ssl模块都无法运行的情况下.那么需要进行如下步骤 第一步: yum install openssl o ...
- 分析iOS Crash文件:符号化iOS Crash文件的3种方法
转自:http://www.cocoachina.com/industry/20140514/8418.html 转自wufawei的博客 当你的应用提交到App Store或者各个渠道之后,请问你多 ...