kdtree HDU5992
STL里面的nth_element()函数
用法:nth_element(first,nth,last)
int a[maxn];
nth_element(a,a+k,a+f);
作用:在a到a+f区间内,使第k位左边的都比它小,右边的都比它大。
kdtree代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
#define lowbit(x) ((x)&(-x))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=2e5+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
using namespace std;
int kdd;
struct node{
int id,g[];
bool operator<(const node &u)const{
return g[kdd]<u.g[kdd];
}
}kdt[maxn<<],data[maxn];
bool flag[maxn<<];
LL dis(node a,node b){
return LL(a.g[]-b.g[])*(a.g[]-b.g[])+LL(a.g[]-b.g[])*(a.g[]-b.g[]);
}
void build(int l,int r,int rt,int kd){
kd%=;
kdd=kd;
if(l>r) return ;
flag[rt]=;
int mid=(l+r)>>;
nth_element(data+l,data+mid,data+r+);
kdt[rt]=data[mid];
flag[rt<<]=flag[rt<<|]=;
if(l<=mid-){
build(l,mid,rt<<,kd+);
}
if(mid+<=r){
build(mid+,r,rt<<|,kd+);
}
}
pair<LL,node> ans;
void query(int rt,node p,int kd){
kd%=;
pair<LL,node> now={dis(p,kdt[rt]),kdt[rt]};
int x=rt<<;
int y=rt<<|;
if(p.g[kd]>=kdt[rt].g[kd]){
swap(x,y);
}
if(flag[x])
query(x,p,kd+);
bool ff=;
if(ans.fi==-){
ff=;
if(p.g[]>=kdt[rt].g[]){
ans=now;
}
}
else{
if(p.g[]>=kdt[rt].g[]&&(now.fi<ans.fi||now.fi==ans.fi&&now.se.id<ans.se.id)){
ans=now;
}
if(LL(kdt[rt].g[kd]-p.g[kd])*(kdt[rt].g[kd]-p.g[kd])<ans.fi){
ff=;
}
}
if(flag[y]&&ff){
query(y,p,kd+);
}
}
int main(){
int t;
scanf("%d",&t);
int n,m;
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<;j++){
scanf("%d",&data[i].g[j]);
}
data[i].id=i;
}
build(,n,,);
while(m--){
node p;
for(int i=;i<;++i){
scanf("%d",&p.g[i]);
}
ans.fi=-;
query(,p,);
printf("%d %d %d\n",ans.se.g[],ans.se.g[],ans.se.g[]);
}
}
}
kdtree HDU5992的更多相关文章
- hdu-5992 Finding Hotels(kd-tree)
题目链接: Finding Hotels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/ ...
- 【kd-tree】hdu5992 Finding Hotels
比较裸的kd-tree,但是比较考验剪枝. 貌似除了经典的矩形距离剪枝之外, 还必须加个剪枝是某个矩形内的最小价格如果大于价格限制的话,则剪枝. #include<cstdio> #inc ...
- Hdu-5992 2016ACM/ICPC亚洲区青岛站 K.Finding Hotels KDtree
题面 题意:二维平面上有很多点,每个点有个权值,现在给你一个点(很多组),权值v,让你找到权值小于等于v的点中离这个点最近的,相同的输出id小的 题解:很裸的KDtree,但是查询的时候有2个小限制, ...
- bzoj1941 hdu5992
看了青岛赛区的题简单学了一下kd,感觉这东西还是挺厉害的 一般kd树找最近点对最坏是O(n),但是随机情况下跑得还是很快的 kd树是一棵BST,但是每一层的关键字不同 一般写法是按照每一维轮流来,这一 ...
- bzoj 2648 KD-tree
稍微看了一下KD-tree的讲义,大概明白了它的原理,但是实现不出来... 所以无耻的抄了一下黄学长的... #include<iostream> #include<cstdio&g ...
- 【BZOJ-1941】Hide and Seek KD-Tree
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 830 Solved: 455[Submi ...
- 【BZOJ-4520】K远点对 KD-Tree + 堆
4520: [Cqoi2016]K远点对 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 490 Solved: 237[Submit][Status ...
- BZOJ 2648 SJY摆棋子 ——KD-Tree
[题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...
- BZOJ 2716 [Violet 3]天使玩偶 ——KD-Tree
[题目分析] KD-Tree的例题.同BZOJ2648. [代码] #include <cstdio> #include <cstring> #include <cstd ...
随机推荐
- C# .NET 实体类转Dictionary
-- System.Reflection.PropertyInfo[] cfgItemProperties = cfgItem.GetType().GetProperties(System.Refle ...
- 黄聪:Windows2012-IIS8安装SSL证书
开始菜单->右上角搜索按钮-> mmc 弹出窗口 关闭控制台会提示保存 打开iis新建站点,为了兼容不支持sni的浏览器,绑定默认https
- 转发 Delphi中线程类TThread 实现多线程编程
Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchr ...
- ES - Dynamic templates 动态模板
1.ES Mapping 在lucene中,索引中每个字段都需要指定很多属性,例如:是否分词.采用哪个分词器.是否存储等. 在ES中,其实索引中每个字段也需要指定这些属性,我们有时候并没有对这些属性进 ...
- 前端-JavaScript练习2
用户输入一个年份,判断这个年是否是闰年. 判断闰年条件: ① 非整百年数除以4,无余为闰,有余不闰: ② 整百年数除以400,无余为闰,有余不闰. 比如: 2000年,整百数年,就要用②公式,除以40 ...
- c#+cad2010+MQ接收消息
cad2015+版本可以使用TrayItem气泡显示消息 static TrayItem trayItem = new TrayItem(); public static void testtrayi ...
- vue3.0中如何使用ueditor
1.在官网下载一个ueditor版本[https://ueditor.baidu.com/website/download.html],解压后放到public文件下的static文件中重命名为Edit ...
- Linq to SQL -- Select、Distinct和Count、Sum、Min、Max、Avg
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
- git push时报错refusing to merge unrelated histories
1. 删除本地项目中的.git目录,然后向远程仓库提交代码的时候,重新配置后再次提交.会有冲突. 解决方式: git remote add origin [//your giturl] git pul ...
- PackagesNotFoundError: The following packages are not available from current channels
因为要用到lifelines 包,在cmd中使用conda install lifelines ,显示如下错误: PackagesNotFoundError: The following packag ...