kd树 hdu2966 In case of failure
传送门: pid=2966" target="_blank">点击打开链接
题意:给n个点,求对于每一个点到近期点的欧几里德距离的平方。
思路:看鸟神博客学kd树劲啊点击打开链接
kd树事实上是暴力树。,真的。
。
它把一个平面划分成了非常多个小平面。每次划分的根据是平面中按x排序的点的中位数或者是按y排序的点的中位数。
建树的复杂度是稳定O(nlogn),可是查询就是大暴力了
把平面分成非常多个小平面后,相当于在平面上搜索剪枝。
kd树是一颗二叉树,假如我要查找离(a,b)近期的点,先依照x和y的划分,从kd树根节点出发走到(a,b)所在的平面,然后这个平面中有一个点。通过这个点。就能大致确定近期点的半径范围了。再看这个圈是否和其它的平面有相交部分,假设有,相同的也訪问其它平面部分(说白了就是暴搜。是不是非常暴力。。
求第k近临时还没学会,仅仅学会了求近期。。
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII; const int MX = 1e5 + 5; struct Point {
int xy[2], l, r, id;
void read(int i) {
id = i;
scanf("%d%d", &xy[0], &xy[1]);
}
} P[MX];
int cmpw; LL ans;
int idx[MX]; bool cmp(const Point &a, const Point &b) {
return a.xy[cmpw] < b.xy[cmpw];
}
int build(int l, int r, int w) {
int m = (l + r) >> 1; cmpw = w;
nth_element(P + l, P + m, P + 1 + r, cmp);
idx[P[m].id] = m;
P[m].l = l != m ? build(l, m - 1, !w) : 0;
P[m].r = r != m ? build(m + 1, r, !w) : 0;
return m;
}
LL dist(LL x, LL y = 0) {
return x * x + y * y;
}
void query(int rt, int w, LL x, LL y) {
LL temp = dist(x - P[rt].xy[0], y - P[rt].xy[1]);
if(temp) ans = min(ans, temp);
if(P[rt].l && P[rt].r) {
bool sign = !w ? (x <= P[rt].xy[0]) : (y <= P[rt].xy[1]);
LL d = !w ? dist(x - P[rt].xy[0]) : dist(y - P[rt].xy[1]);
query(sign ? P[rt].l : P[rt].r, !w, x, y);
if(d < ans) query(sign ? P[rt].r : P[rt].l, !w, x, y);
} else if(P[rt].l) query(P[rt].l, !w, x, y);
else if(P[rt].r) query(P[rt].r, !w, x, y);
} int main() {
int T, n; //FIN;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++) P[i].read(i);
int rt = build(1, n, 0);
for(int i = 1; i <= n; i++) {
ans = 1e18;
query(rt, 0, P[idx[i]].xy[0], P[idx[i]].xy[1]);
printf("%I64d\n", ans);
}
}
return 0;
}
kd树 hdu2966 In case of failure的更多相关文章
- HDU2966 In case of failure(浅谈k-d tree)
嘟嘟嘟 题意:给定\(n\)个二维平面上的点\((x_i, y_i)\),求离每一个点最近的点得距离的平方.(\(n \leqslant 1e5\)) 这就是k-d tree入门题了. k-d tre ...
- hdu 2966 In case of failure k-d树
题目链接 给n个点, 求出每个点到离它最近的点的距离. 直接建k-d树然后查询就可以 感觉十分神奇... 明白了算法原理但是感觉代码还不是很懂... #include <bits/stdc++ ...
- 从K近邻算法谈到KD树、SIFT+BBF算法
转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...
- K-D树
一般用来解决各种二维平面的点对统计,也许一般非正解? 没时间慢慢写了,打完这个赛季后补细节 建树板子: #include <cstdio> #include <locale> ...
- 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...
- k近邻法的C++实现:kd树
1.k近邻算法的思想 给定一个训练集,对于新的输入实例,在训练集中找到与该实例最近的k个实例,这k个实例中的多数属于某个类,就把该输入实例分为这个类. 因为要找到最近的k个实例,所以计算输入实例与训练 ...
- bzoj 3053 HDU 4347 : The Closest M Points kd树
bzoj 3053 HDU 4347 : The Closest M Points kd树 题目大意:求k维空间内某点的前k近的点. 就是一般的kd树,根据实测发现,kd树的两种建树方式,即按照方差 ...
- HDU 5809 Ants(KD树+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5809 [题目大意] 给出一些蚂蚁和他们的巢穴,一开始他们会在自己的巢穴(以二维坐标形式给出),之后 ...
- <转>从K近邻算法、距离度量谈到KD树、SIFT+BBF算法
转自 http://blog.csdn.net/likika2012/article/details/39619687 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章待写:1.KD树:2.神经 ...
随机推荐
- lodash中文说明文档
lodash中文说明文档 https://www.css88.com/doc/lodash/
- dockerfile note
dockerfile note reference summary defination docker can build images automatically by reading the in ...
- linux下vi修改文件用法
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件,并将光标置于最后 ...
- 基于HLS(HTTP Live Streaming)的视频直播分析与实现
转自:http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html HLS(HTTP Live Streaming)的分析: HTT ...
- ZOJ - 1655 Transport Goods(单源最长路+迪杰斯特拉算法)
题目: 有N-1个城市给首都(第N个城市)支援物资,有M条路,走每条路要耗费一定百分比(相对于这条路的起点的物资)的物资.问给定N-1个城市将要提供的物资,和每条路的消耗百分比.求能送到首都的最多的物 ...
- hdu3404 Switch lights
题目描述 题解: 首先,由$SG$定理得SG(x,y)=mex(SG(x',y)^SG(x,y')^SG(x',y'))(x'<x,y'<y) 这里的$SG(x,y)$叫$Nim$积. $ ...
- memcached内存分配
Memcached默认情况下采用了名为Slab Allocator的机制分配.管理内存,最大单个存储对象大小为1M. page:分配给slab的最小内存空间,默认为1M,可以在启动时通过-l参数修改 ...
- netcat命令的使用
Linux netcat 命令实例: 1,端口扫描 端口扫描经常被系统管理员和黑客用来发现在一些机器上开放的端口,帮助他们识别系统中的漏洞. $nc -z -v -n 172.31.100.7 21- ...
- 2016年工作中遇到的问题41-50:Dubbo注册中心奇葩问题,wifi热点坑了
41.获得JSON中的变量.//显示json串中的某个变量,name是变量名function json(json,name){ var jsonObj = eval(json); return jso ...
- [HDU4348]To the moon(主席树)
传送门 对于这个题,显然要打lazy标记了,但是lazy标记pushdown的时候肯定会增加一大堆节点,然后就MLE了.(题解这么说的,我其实不会pushdown) 所以,就换另一种方式,把标记直接打 ...