hdu 2966 In case of failure(KD-tree)

题目链接:hdu 2966 In case of failure

题意:

给你n个点,让你输出每个点到最近点的欧式距离。

题解:

KD-树裸题,板子抄的鸟神的。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=(a);i<=(b);++i)
 3 using namespace std;
 4 using ll=long long;
 5
 6 namespace KD_Tree{
 7     const int N=1e5+7,DI=2;
 8     struct Node{
 9         int p[DI],f,l,r,mx[DI],mi[DI];
10         int operator[](const int&idx)const{return p[idx];}
11         void in(int idx,int *v){f=idx;F(i,0,DI-1)p[i]=v[i];}
12         void up(Node&a){
13             F(i,0,DI-1){
14                 mi[i]=min(mi[i],a.mi[i]);
15                 mx[i]=max(mx[i],a.mx[i]);
16             }
17         }
18     }T[N];
19     int idx[N],cmpd,root;
20
21     bool cmp(const Node&a,const Node&b){return a[cmpd]<b[cmpd];}
22     void up(int x){
23         if(T[x].l)T[x].up(T[T[x].l]);
24         if(T[x].r)T[x].up(T[T[x].r]);
25     }
26     int build(int l,int r,int d=0,int f=0)
27     {
28         int mid=l+r>>1;
29         cmpd=d%DI,nth_element(T+l,T+mid,T+r+1,cmp);
30         idx[T[mid].f]=mid,T[mid].f=f;
31         F(i,0,DI-1)T[mid].mi[i]=T[mid].mx[i]=T[f][i];
32         T[mid].l=l!=mid?build(l,mid-1,d+1,mid):0;
33         T[mid].r=r!=mid?build(mid+1,r,d+1,mid):0;
34         return up(mid),mid;
35     }
36     ll dist(ll x,ll y=0){return x*x+y*y;}
37     ll query(ll x,ll y,ll&ans,int rt=root,int d=0)
38     {
39         ll tmp=dist(x-T[rt][0],y-T[rt][1]);
40         if(tmp)ans=min(ans,tmp);
41         if(T[rt].l&&T[rt].r)
42         {
43             bool is=!d?(x<=T[rt][0]):(y<=T[rt][1]);
44             ll dis=!d?dist(x-T[rt][0]):dist(y-T[rt][1]);
45             query(x,y,ans,is?T[rt].l:T[rt].r,!d);
46             if(dis<ans)query(x,y,ans,is?T[rt].r:T[rt].l,!d);
47         }
48         else if(T[rt].l)query(x,y,ans,T[rt].l,!d);
49         else if(T[rt].r)query(x,y,ans,T[rt].r,!d);
50     }
51 };
52 using namespace KD_Tree;
53 int t,n;
54
55 int main(){
56     scanf("%d",&t);
57     while(t--)
58     {
59         scanf("%d",&n);
60         F(i,1,n)
61         {
62             int x[2];
63             scanf("%d%d",x,x+1);
64             T[i].in(i,x);
65         }
66         root=build(1,n);
67         F(i,1,n)
68         {
69             ll ans=1ll<<61;
70             query(T[idx[i]][0],T[idx[i]][1],ans);
71             printf("%lld\n",ans);
72         }
73     }
74     return 0;
75 }

时间: 2024-08-08 01:08:09

hdu 2966 In case of failure(KD-tree)的相关文章

[HDU 2966]In case of failure

Description To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the cust

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

HDU ACM 2966 In case of failure -&gt;K_D树模版题

分析:k_d树的模版题,参考了别人的写的:划分的时候采用坐标跨度作为分割依据的效率略比采用树的深度作为划分依据的高:nth_element函数比sort函数的效率高:全部采用getchar和putchar效率也能提高一些. #include<iostream> #include<algorithm> using namespace std; struct POINT { int x,y; }; struct K_D_Node { POINT mid; //分割中点 int spli

KD tree

Kd-树 其实是K-dimension tree的缩写,是对数据点在k维空间中划分的一种数据结构.其实,Kd-树是一种平衡二叉树. 举一示例: 假设有六个二维数据点 = {(2,3),(5,4),(9,6),(4,7),(8,1),(7,2)},数据点位于二维空间中.为了能有效的找到最近邻,Kd-树采用分而治之的思想,即将整个空间划分为几个小部分.六个二维数据点生成的Kd-树的图为: 对于拥有n个已知点的kD-Tree,其复杂度如下: 构建:O(log2n) 插入:O(log n) 删除:O(l

HDU 1325 POJ 1308 Is It A Tree? (并查集)

这道题就是裸并查集,关键在于对不是树几种的判断 1. 空树是树 2. 森林不是树 3. 无环 或者从入度来看:1,无环:2,除了根,所有的入度为1,根入度为0:3,这个结构只有一个根,不然是森林了. 这道题本来暑假做的POJ 1308 但是HDU没有过.在于空树没有考虑. 用并查集判断有多少个森林注意编号是随机的,不是次序.... /* input: 0 0 1 1 0 0 1 2 1 2 0 0 1 2 2 3 4 5 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

HDU 2966 Aragorn&#39;s Story 树链剖分第一题 基础题

Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges c

bzoj 3053 HDU 4347 : The Closest M Points kd树

bzoj 3053 HDU 4347 : The Closest M Points  kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差较大的维度分开(建树常数大)或者每一位轮换分割(询问常数大),后者更快也更好些,以后就果断写第二种了. #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using

[转载]kd tree

[本文转自]http://www.cnblogs.com/eyeszjwang/articles/2429382.html k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索). 应用背景 SIFT算法中做特征点匹配的时候就会利用到k-d树.而特征点匹配实际上就是一个通过距离函数在高维矢量之间进行相似性检索的问题.针对如何快速而准确地找到查询点的近邻,现在提出了很多高维空间索引结构和近似查询的算法,k-d树

k-d tree算法详解

k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索). 1.应用背景 SIFT算法中做特征点匹配的时候就会利用到k-d树.而特征点匹配实际上就是一个通过距离函数在高维矢量之间进行相似性检索的问题.针对如何快速而准确地找到查询点的近邻,现在提出了很多高维空间索引结构和近似查询的算法,k-d树就是其中一种. 索引结构中相似性查询有两种基本的方式:一种是范围查询(range searches),另一种是K近邻查询