KD树,这东西其实在ML经常被使用,不过30s的时限还是第一次见。

 /* 2966 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std; typedef struct {
int x, y;
} Point_t; typedef struct {
int axis;
Point_t p;
int l, r;
} node_t; const int maxn = 1e5+;
Point_t P[maxn], P2[maxn];
node_t nd[maxn];
int L;
__int64 ans; __int64 Eucl(Point_t a, Point_t b) {
return 1LL*(a.x-b.x)*(a.x-b.x) + 1LL*(a.y-b.y)*(a.y-b.y);
} bool compx(const Point_t &a, const Point_t &b) {
return a.x < b.x;
} bool compy(const Point_t &a, const Point_t &b) {
return a.y < b.y;
} void newNode() {
++L;
nd[L].l = nd[L].r = ;
} int create_KD(int l, int r, int d) {
if (l > r)
return ;
newNode();
int rt = L;
nd[rt].axis = d&;
if (l == r) {
nd[rt].p = P[l];
return rt;
}
if (d & )
sort(P+l, P+r+, compy);
else
sort(P+l, P+r+, compx);
int mid = (l+r)>>;
nd[rt].p = P[mid];
nd[rt].l = create_KD(l, mid-, d+);
nd[rt].r = create_KD(mid+, r, d+);
return rt;
} void cal(int rt, Point_t p) {
if (rt == )
return ;
int i, j, k; __int64 d = Eucl(nd[rt].p, p);
if (d && d<ans)
ans = d; if (nd[rt].axis) {
j = nd[rt].p.y;
k = p.y;
} else {
j = nd[rt].p.x;
k = p.x;
} if (k <= j)
cal(nd[rt].l, p);
else
cal(nd[rt].r, p); if (1LL*(j-k)*(j-k) < ans) {
if (k <= j)
cal(nd[rt].r, p);
else
cal(nd[rt].l, p);
}
} int main() {
int t, n;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i=; i<n; ++i) {
scanf("%d %d", &P[i].x, &P[i].y);
P2[i] = P[i];
}
L = ;
int rt = create_KD(, n-, );
for (i=; i<n; ++i) {
ans = LLONG_MAX;
cal(rt, P2[i]);
printf("%I64d\n", ans);
}
} #ifndef ONLINE_JUDGE
printf("time = %d\n", (int)(clock()));
#endif return ;
}

【HDOJ】2966 In case of failure的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  3. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  4. 【HDOJ】4373 Mysterious For

    1. 题目描述有两种不同类型的循环,并给出一个由1.2组成的序列,表示嵌套的循环类型.问这样组着的循环一共需要多少次循环?并将结果模364875103. 2.基本思路显然,每当遇到一个类型1的序列,即 ...

  5. 【HDOJ】4358 Boring counting

    基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...

  6. 【HDOJ】4351 Digital root

    digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...

  7. 【HDOJ】3727 Jewel

    静态区间第K大值.主席树和划分树都可解. /* 3727 */ #include <iostream> #include <sstream> #include <stri ...

  8. 【HDOJ】3473 Minimum Sum

    划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> ...

  9. 【HDOJ】4341 Gold miner

    分组01背包.在一条直线上的点归为一组. /* 4341 */ #include <iostream> #include <sstream> #include <stri ...

随机推荐

  1. 使用jquery.validate.js实现boostrap3的校验和验证

    使用jquery.validate.js实现boostrap3的校验和验证 boostrap3验证框架 jquery.validate.js校验表单 >>>>>>& ...

  2. gitcafe 使用hexo搭建博客

    --缘由:因为看大家都用github等搭建博客,作为半个程序员的自己,也按捺不住了,终于有空来尝试一把了,选择了和github 相同功能的gitcafe网站,因为在国内比较快,这是大家的看法,下面写一 ...

  3. PHP的数据库 之 关闭问题

    首先,PHP由于有垃圾回收机制,所以数据库即使你不手动关闭,也有自动去关闭的机制, 这里就和操作文本流不同,文本流需要手动去关闭,不然会发生内存浪费现象 并且,PHP在同时连接多个DB的时候,连接到一 ...

  4. C#语法糖之第五篇: 泛型委托- Action<T>

    因为工作的原因(其实还是个人的惰性)昨天没有给大家分享文章,然后这几天也有很多园友也提出了他们报告的意见及指导,再次感谢这些兄弟们的照顾我 和支持,这个分类的文章我当时想的是把我的学习经验和工作中用到 ...

  5. 3. NHibernate基础知识 - 你必须知道的一些事情

    首先介绍一下框架结构(这个有个概念就可以): 然后我们会介绍一个很重要的概念(一定要好看)!! 这节对 NHibernate 架构做一个介绍,首先要了解一下该框架在应用程序中的位置: 先来一个简单的图 ...

  6. 根据文件夹的GUid找到该文件夹

    Guid guid = Guid.Parse(folderGuID); SPFolder folder = list.Folders[guid].Folder;

  7. Java学习----集合函数

    1.List----有序的collection(序列) 与数组的不同:数据类型可以相同可以不同,数组的长度是预先定义好的,集合的长度也是预先定义好的,但是长度会随着元素的增加而增加 ArrayList ...

  8. Struts2输入校验

    1.编写校验规则文件 (<ActionName>-validation.xml),文件放在Action类文件相同的路径下校验失败返回input的result.       <vali ...

  9. javascript闭包传参和事件的循环绑定

    今天看到一个javascript的题目,按常理循环绑定事件,但是得到的结果却不是想要的. <a href="#">text</a><br>< ...

  10. qrcode.js插件将你的内容转换成二维码格式

    ---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...