luoguP2479 [SDOI2010]捉迷藏
https://www.luogu.org/problemnew/show/P2479
据说可以用线段树做但是我不会,只能写一个 KD-Tree 了
对于每个点求出距离它最远的点和最近的点的距离,然后取 min 即可
因为这个东西是可以剪枝的,所以跑的挺快的
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') fu = -1; c = getchar();}
while(c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
const int N = 1e5 + 5;
int WD, siz, n, root;
struct po {
int a[2];
bool operator < (const po A) const {return a[WD] < A.a[WD];}
}t[N];
struct Node {
int mn[2], mx[2], lc, rc;
po tp;
}p[N];
void update(int u) {
int l = p[u].lc, r = p[u].rc;
for(register int i = 0; i <= 1; i++) {
p[u].mn[i] = p[u].mx[i] = p[u].tp.a[i];
if(l) p[u].mn[i] = min(p[u].mn[i], p[l].mn[i]), p[u].mx[i] = max(p[u].mx[i], p[l].mx[i]);
if(r) p[u].mn[i] = min(p[u].mn[i], p[r].mn[i]), p[u].mx[i] = max(p[u].mx[i], p[r].mx[i]);
}
}
int build(int l, int r, int wd) {
if(l > r) return 0;
int u = ++siz, mid = (l + r) >> 1;
WD = wd; nth_element(t + l, t + mid, t + r + 1);
p[u].tp = t[mid]; p[u].lc = build(l, mid - 1, wd ^ 1); p[u].rc = build(mid + 1, r, wd ^ 1);
update(u); return u;
}
// 最小距离
int calc1(int u, po tp) {
int ans = 0;
for(register int i = 0; i <= 1; i++) ans += max(0, p[u].mn[i] - tp.a[i]) + max(0, tp.a[i] - p[u].mx[i]);
return ans;
}
int calc2(int u, po tp) {
int ans = 0;
for(register int i = 0; i <= 1; i++) ans += max(abs(tp.a[i] - p[u].mn[i]), abs(tp.a[i] - p[u].mx[i]));
return ans;
}
int dis(po a, po b) {
int ans = 0;
for(register int i = 0; i <= 1; i++) ans += abs(a.a[i] - b.a[i]);
return ans;
}
const int INF = 0x7f7f7f7f;
int ans1, ans2;
void query1(int u, po tp) {
if(!u) return;
int now = dis(p[u].tp, tp);
if(ans1 > now && now) ans1 = now;
int l = INF, r = INF;
if(p[u].lc) l = calc1(p[u].lc, tp);
if(p[u].rc) r = calc1(p[u].rc, tp);
if(l < r) {
if(l < ans1) query1(p[u].lc, tp);
if(r < ans1) query1(p[u].rc, tp);
} else {
if(r < ans1) query1(p[u].rc, tp);
if(l < ans1) query1(p[u].lc, tp);
}
}
void query2(int u, po tp) {
if(!u) return;
int now = dis(p[u].tp, tp);
if(ans2 < now) ans2 = now;
int l = -1, r = -1;
if(p[u].lc) l = calc2(p[u].lc, tp);
if(p[u].rc) r = calc2(p[u].rc, tp);
if(l > r) {
if(l > ans2) query2(p[u].lc, tp);
if(r > ans2) query2(p[u].rc, tp);
} else {
if(r > ans2) query2(p[u].rc, tp);
if(l > ans2) query2(p[u].lc, tp);
}
}
int minn = INF;
int main() {
cin >> n;
for(register int i = 1; i <= n; i++) read(t[i].a[0]), read(t[i].a[1]);
root = build(1, n, 0);
for(register int i = 1; i <= n; i++) {
ans1 = INF, ans2 = -INF;
query1(root, t[i]);
query2(root, t[i]);
minn = min(minn, ans2 - ans1);
}
cout << minn << endl;
return 0;
}
luoguP2479 [SDOI2010]捉迷藏的更多相关文章
- [SDOI2010]捉迷藏 K-Dtree
[SDOI2010]捉迷藏 链接 luogu 思路 k-dtree模板题 代码 #include <bits/stdc++.h> #define ls (t[u].ch[0]) #defi ...
- [SDOI2010]捉迷藏
嘟嘟嘟 k-d tree板儿题. 建完树后对每一个点求一遍最小和最大曼哈顿距离,是曼哈顿,不是欧几里得. #include<cstdio> #include<iostream> ...
- P2479 [SDOI2010]捉迷藏
传送门 KDtree是个吼东西啊-- 枚举每一个点,然后求出离他距离最远和最近的点的距离,更新答案 然而为什么感觉KDtree只是因为剪枝才能跑得动呢-- //minamoto #include< ...
- 模板—K-D-tree(P2479 [SDOI2010]捉迷藏)
#include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #def ...
- 【题解】[SDOI2010]捉迷藏
题目链接:https://www.luogu.com.cn/problem/P2479 题目大意:求平面\(n\)个点中,到其它\(n-1\)个点的曼哈顿距离最大和最小距离之差最小的点,求出这个这个距 ...
- [学习笔记]K-D Tree
以前其实学过的但是不会拍扁重构--所以这几天学了一下 \(K-D\ Tree\) 的正确打开姿势. \(K\) 维 \(K-D\ Tree\) 的单次操作最坏时间复杂度为 \(O(k\times n^ ...
- KD-Tree总结
KD-Tree总结 问题引入 平面上有\(n\)个点,\(q\)组询问,每一次查询距离\((x,y)\)最近的点对,强制在线. 问题解决 暴力 显然我们可以直接枚举点然后算距离取\(min\),这样子 ...
- 2021.07.09 K-D树
2021.07.09 K-D树 前置知识 1.二叉搜索树 2.总是很长的替罪羊树 K-D树 建树 K-D树具有二叉搜索树的形态,对于每一个分类标准,小于标准的节点在父节点左边,大于标准的节点在父节点右 ...
- [BZOJ1941][Sdoi2010]Hide and Seek
[BZOJ1941][Sdoi2010]Hide and Seek 试题描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他 ...
随机推荐
- kubernetes 集群安全配置
版本:v1.10.0-alpha.3 openssl genrsa -out ca.key 2048 openssl req -x509 -new -nodes -key ca.key -subj & ...
- Mono在Full AOT模式下的限制
[Mono在Full AOT模式下的限制] 调试时遇到一个Mono运行时异常: ExecutionEngineException: Attempting to JIT compile method ' ...
- 英文单词cipher 和password的区别,用法有什么不同,
['saɪfə(r)] cipher 指一套密码系统,比如电影<风声>中破译的那个系统叫cipher:password 则指进入的指令,比如你的qq密码,电脑密码等叫password.总之 ...
- maven filter插件只替换了部分变量问题
maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...
- Partial Functions(偏函数)
如果你想定义一个函数,而让它只接受和处理其参数定义域范围内的子集,对于这个参数范围外的参数则抛出异常,这样的函数就是偏函数(顾名思异就是这个函数只处理传入来的部分参数). 偏函数是个特质其的类型为Pa ...
- 冲刺NOIP2015提高组复赛模拟试题(五) 3.破坏基地
3.破坏基地 描述 Description 在Z国和W国之间一直战火不断. 好不容易,W国的间谍把完整的Z国的军事基地的地图到手了. 于是W国决定再次出击,一举击破Z国的防线. W国认真研究了Z国的地 ...
- python中fork()函数生成子进程分析-乾颐堂
python的os module中有fork()函数用于生成子进程,生成的子进程是父进程的镜像,但是它们有各自的地址空间,子进程复制一份父进程内存给自己,两个进程之 间的执行是相互独立的,其执行顺序可 ...
- 【转】Comprehensive learning path – Data Science in Python
Journey from a Python noob to a Kaggler on Python So, you want to become a data scientist or may be ...
- Google-优秀移动站点设计10招
Google-优秀移动网站设计10招 1)添加一个醒目的搜索条:在移动终端上,人们希望能够快速找到自己需要的东西 2)把大表格拆分成小块:别搞一个长长的表格页面,上面包含各种输入框 3)允许用户匿名浏 ...
- Redhat9.0+Apache1.3.29+Mysql3.23.58+PHP4.3.4
Redhat9.0+Apache1.3.29+Mysql3.23.58+PHP4.3.4 TAG标签: 摘要:红帽创建于1993年,是目前世界上最资深的Linux和开放源代码提供商,同时也是最获认可的 ...