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.神经 ...
随机推荐
- Windows API函数大全(完整)
Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一 ...
- Linux-RedHat7.2 安装nginx托管.net core2.0
1.安装依赖包 yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel 2.下载安装包 wge ...
- 企业支付宝账号开发接口教程--JAVA-UTF-8(实际操作完善中...SpringMVC+JSP)
关于即时到账的开发.审核通过.简单测试如下. 希望看的可以收藏或者赞一下哦. 1:拥有自己的支付宝企业账号.去产品商店选择适合自己的方案.并签约合同. 2:选择合适的商家收款产品并去签约.填写相应的信 ...
- 全新Ubentu系统没有make,gcc命令解决办法
一定要记得先update sudo apt-get update 然后输入下述命令即可 sudo apt-get install make sudo apt-get install gcc
- python_OS 模块
os模块 用于提供系统级别的操作 os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") # 改变当前脚本工作目 ...
- ES6(字符串)
ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...
- xtu read problem training 2 B - In 7-bit
In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3 ...
- PTA 01-复杂度1 最大子列和问题 (20分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/709 5-1 最大子列和问题 (20分) 给定KK个整数组成的序列{ N_1N1 ...
- python多线程--线程同步
如果多个线程共同对某个数据修改,则可能出现不可预料的结果,为了保证数据的正确性,需要对多个线程进行同步. 使用Thread对象的Lock和Rlock可以实现简单的线程同步,这两个对象都有acquire ...
- 【multimap的应用】D. Array Division
http://codeforces.com/contest/808/problem/D #include<iostream> #include<cstdio> #include ...