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]捉迷藏的更多相关文章

  1. [SDOI2010]捉迷藏 K-Dtree

    [SDOI2010]捉迷藏 链接 luogu 思路 k-dtree模板题 代码 #include <bits/stdc++.h> #define ls (t[u].ch[0]) #defi ...

  2. [SDOI2010]捉迷藏

    嘟嘟嘟 k-d tree板儿题. 建完树后对每一个点求一遍最小和最大曼哈顿距离,是曼哈顿,不是欧几里得. #include<cstdio> #include<iostream> ...

  3. P2479 [SDOI2010]捉迷藏

    传送门 KDtree是个吼东西啊-- 枚举每一个点,然后求出离他距离最远和最近的点的距离,更新答案 然而为什么感觉KDtree只是因为剪枝才能跑得动呢-- //minamoto #include< ...

  4. 模板—K-D-tree(P2479 [SDOI2010]捉迷藏)

    #include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #def ...

  5. 【题解】[SDOI2010]捉迷藏

    题目链接:https://www.luogu.com.cn/problem/P2479 题目大意:求平面\(n\)个点中,到其它\(n-1\)个点的曼哈顿距离最大和最小距离之差最小的点,求出这个这个距 ...

  6. [学习笔记]K-D Tree

    以前其实学过的但是不会拍扁重构--所以这几天学了一下 \(K-D\ Tree\) 的正确打开姿势. \(K\) 维 \(K-D\ Tree\) 的单次操作最坏时间复杂度为 \(O(k\times n^ ...

  7. KD-Tree总结

    KD-Tree总结 问题引入 平面上有\(n\)个点,\(q\)组询问,每一次查询距离\((x,y)\)最近的点对,强制在线. 问题解决 暴力 显然我们可以直接枚举点然后算距离取\(min\),这样子 ...

  8. 2021.07.09 K-D树

    2021.07.09 K-D树 前置知识 1.二叉搜索树 2.总是很长的替罪羊树 K-D树 建树 K-D树具有二叉搜索树的形态,对于每一个分类标准,小于标准的节点在父节点左边,大于标准的节点在父节点右 ...

  9. [BZOJ1941][Sdoi2010]Hide and Seek

    [BZOJ1941][Sdoi2010]Hide and Seek 试题描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他 ...

随机推荐

  1. systemd 配置文件

    [Unit]区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系.它的主要字段如下. Description:简短描述 Documentation:文档地址 ...

  2. Python基础:字符串的常见操作

    # 切片 # 切片 获取对象中一部分数据 [起始位置:结束位置(不包含):步长] qpstr = "山东张学友" result = qpstr[1: 3: 1] # 东张 prin ...

  3. 安装kali linux 2017.1 【二、安装VMware-tools 以及相关问题处理】

    一.基本步骤: 1.VMware Workstation菜单栏中,选择“虚拟机”,”安装VMware Tools...“. 2.挂载VMware Tools安装程序到/mnt/cdrom/. mkdi ...

  4. 平衡二叉树之RB树

    RB树(红黑树)并不追求“完全平衡”——它只要求部分地达到平衡要求,降低了对旋转的要求,从而提高了性能.由于它的设计,任何不平衡都会在三次旋转之内解决.典型的用途是实现关联数组(如C++中的map和s ...

  5. Python使用日常

    #Python中文件夹和文件的判断import os My_Path = "/home/lpworkstudy/Gooddir/" #现在我们判断这个文件夹是否存在 #如果不存在, ...

  6. CNN和GAN 比较nice的介绍

    CNN是个什么鬼:https://www.zhihu.com/question/52668301 就是这样GAN : https://zhuanlan.zhihu.com/p/27199954

  7. OSG图形设备接口GraphicsContext

    1.图形设备与相机 在Camera类的成员函数中,setGraphicContext()函数的工作是设置相机对应的图形设备对象,换句话说,下面要介绍的GraphicsContext类就是图形设备对象的 ...

  8. Java SimpleDateFormat用法

      ? Java中怎么才能把日期转换成想要的格式呢,或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类可以实现,以下是Si ...

  9. vs下C# WinForm 解决方案里面生成的文件都是什么作用?干什么的?

    Properties文件夹 定义你程序集的属性 项目属性文件夹 一般只有一个 AssemblyInfo.cs 类文件,用于保存程序集的信息,如名称,版本等,这些信息一般与项目属性面板中的数据对应,不需 ...

  10. 学习python4

    文件系统实现文件的增删改查 UnicodeDecodeError: 'gbk' codec can't decode byte 0x9a in position 8: illegal multibyt ...