【HDOJ】2966 In case of failure
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的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】4373 Mysterious For
1. 题目描述有两种不同类型的循环,并给出一个由1.2组成的序列,表示嵌套的循环类型.问这样组着的循环一共需要多少次循环?并将结果模364875103. 2.基本思路显然,每当遇到一个类型1的序列,即 ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...
- 【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+..+ ...
- 【HDOJ】3727 Jewel
静态区间第K大值.主席树和划分树都可解. /* 3727 */ #include <iostream> #include <sstream> #include <stri ...
- 【HDOJ】3473 Minimum Sum
划分树解.主席树解MLE. /* 3473 */ #include <iostream> #include <sstream> #include <string> ...
- 【HDOJ】4341 Gold miner
分组01背包.在一条直线上的点归为一组. /* 4341 */ #include <iostream> #include <sstream> #include <stri ...
随机推荐
- portal开发"下拉框"“日期框”查询要怎么配置
下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...
- Spring读书笔记-----Spring的Bean之设置Bean值
[java] view plaincopyprint? Java实例的属性值可以有很多种数据类型.基本类型值.字符串类型.java实例甚至其他的Bean实例.java集合.数组等.所以Spring允许 ...
- .NET 统一用户管理 -- 单点登录
单点登录 前言 本篇做为.Net 统一用户管理的系列文章的开山之作,主要说一个单点登录是怎么实现的,以及为啥要统一用户管理. 单点登录(Single Sign On),简称为 SSO,是目前比较流行的 ...
- .Net程序员学习Linux(三)
基础命令 ll 文件名 命令可以查看文件的大小 file 文件名 可以看到文件后缀,大小 压缩与解压工具 这些压缩工具按照我理解应该是很少单独拿出来用,应该是需要配合其他命令或者工具来使用 gzi ...
- 【转载】介绍“Razor”— ASP.NET的一个新视图引擎
最近在做一个项目,用的MVC razor 视图,因为之前没用这个视图做过,于是查阅文档资料,共享一下. https://msdn.microsoft.com/zh-cn/ff849693 内容主要是讲 ...
- boost::bind实践
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...
- 总结javascript继承的两种方式的N中写法
最近翻看博客园,总结了一下javascript的继承方式:prototype和copy继承方式. 一.prototype方式 当一个函数被创建时,Function构造函数产生的函数会隐式的被赋予一个p ...
- java 整体字体样式设置
两种方式: 1.UIManager.put("Button.font", new Font("MS UI Gothic", Font.PLAIN, 24)) ...
- 关于script,first,second,third=argv运行出错的问题
from sys import argv input(argv) #python自带的IDLE直接执行不能提供命令行参数 script,first,second,third=argv print(&q ...
- POJ 2513 Colored Sticks 字典树、并查集、欧拉通路
Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...