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.神经 ...
随机推荐
- Navicat Premium 12试用期的破解方法
参考:https://blog.csdn.net/Jason_Julie/article/details/82864187 已测可用
- sql中group by
某图书馆收藏有书籍具有不同的出版年份,管理员需要做一下统计工作: (1)每一年书籍的数目,如: 2000年有10本书, 2001年有5本书... (2)每一种书籍的数目,如: 西游记有10本, 三国演 ...
- POJ-1011(sticks,深搜)
Description George took sticks of the same length and cut them randomly until all parts became at mo ...
- centos7.x设置静态IP
本教程以centOs7.4为例: 1.点击虚拟机的[编辑]选项,选择[虚拟网络编辑器] 2.选择[VMnet8],然后点击[NAT设置] 3.记录[子网掩码]和[网关IP],后面会用到 4.进入终端, ...
- C#对象初始化的探讨
最近在弄MQ的性能监测数据埋点,无疑中用到一个Nstatsd的客户端,看到里面写过里面一种嵌套类的写法.代码如下: 客户端Client是一个密封的类,并且构造函数私有访问.然后又用一个嵌套类Curre ...
- Django中的Cookie、Session、Token
Cookie : 指望着为了辨别用户身份.进行会话跟踪而存储在用户本地的数据(通常经过加密),是由服务端生成,发送给客户端浏览器,浏览器会将Cookie以key/value保存,下一请求同一网站是就发 ...
- 比较spring cloud和dubbo,各自的优缺点是什么
dubbo由于是二进制的传输,占用带宽会更少springCloud是http协议传输,带宽会比较多,同时使用http协议一般会使用JSON报文,消耗会更大 dubbo的开发难度较大,原因是dubbo的 ...
- 关联及web_reg_save_param
一.什么是关联 关联(correlation):脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服务器所响应的内容中查 找,得到相应的值,已变量的形式替换录制时的静 ...
- python3 时间复杂度
时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...
- Go函数学习
package main import ( "fmt" "reflect" "runtime" "math" ) //函 ...