【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 ...
随机推荐
- RedHat7搭建Nginx+Apache+PHP
Nginx做为前端服务器(本机IP:192.168.136.104),将访问PHP页面的动态请求转发给Apache服务器(只监听本地回环地址172.0.0.1:80) 安装Apache# yum -y ...
- 卸载RedHat7自带的yum,安装并使用网易163源
由于redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,安装CentOS yum工具,再配置其他源,以下为详细过程: 删除redhat原有的yu ...
- apache的一些基本配置
Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) 基本配置: ServerRoot "/mnt/softw ...
- day-7
/* 倒数7天了 某一天 某一刻 某次呼吸 我们终将再分离 而我的 自传里 曾经有你 没有遗憾的诗句 诗句里 充满感激 (小仙女博客抄来的233) 是啊 就快要结束了 曲终人散 上午被错误数据卡了一小 ...
- PS之火焰铁锈字
效果图 素材一:将下图在PS中打开,选择菜单:编辑>定义图案,命名后关闭图案 素材二 1.新建如下画布 2.将素材二拖入新建好的画布(使用移动工具) 3.先将文字图层复制4次(ctrl+J)并且 ...
- IE的@cc_on条件编译
1: alert("浏览器版本为:"+sSuffix) 用来判断浏览器的版本很好用 var b = /*@cc_on!@*/false; 其中/*@cc_on ..... @*/之 ...
- Js 上传文件 页面不刷新
html控件代码: <form id="form1"> <p><input type="file" name="mfil ...
- 在mipsel-linux平台上的编译应用SQLite-3.5.9
sqlite 第一个Alpha版本诞生于2000年5月,是实现了SQL 92标准的一个大子集的嵌入式数据库,其以在一个库中组合了数据库引擎和接口,能将所有数据存储于单个文件中.官方测试表明sqlite ...
- Android Studio中JNI -- 2 -- 编写c文件
继上一篇,我们在native接口中编写了2个方法 生成的相应.h文件 这时,需要我们自己去完善.c文件 /* DO NOT EDIT THIS FILE - it is machine generat ...
- mysql update不能直接使用select的结果
在sql server中,我们可是使用以下update语句对表进行更新:update a set a.xx= (select yy from b) ;但是在mysql中,不能直接使用set selec ...