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被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他 ...
随机推荐
- Django框架开发web网站的网页优化—页面静态化
网站优化-页面静态化 1)概念 提前将页面所用到的数据从数据库查询出来,然后生成一个静态页面,之后用户来访问的时候,直接返回静态页面. 举例:首页静态化:获取首页用到的数据表中的数据,生成静态首页in ...
- Kafka Zookeeper 基本命令示例
Kafka 新建Topic bin/kafka-topics. --replication-factor --partitions --topic my-topic 查看已存在Topic列表 bin/ ...
- 使用RampTexture来控制diffuse shading
[RampTexture] RampTexture(渐变纹理),可以是1D/2D纹理. This allows you to accentuate the surface's colors to fa ...
- default of c#
[default of c#] 在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给 ...
- 平衡二叉树之RB树
RB树(红黑树)并不追求“完全平衡”——它只要求部分地达到平衡要求,降低了对旋转的要求,从而提高了性能.由于它的设计,任何不平衡都会在三次旋转之内解决.典型的用途是实现关联数组(如C++中的map和s ...
- 学习h5(开始)
webstorm 下载地址:http://www.sdifenzhou.com/6176.html webstorm注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QT ...
- 制作initramfs/initrd镜像
Linux kernel在自身初始化完成之后,需要能够找到并运行第一个用户程序(这个程序通常叫做"init"程序).用户程序存在于文件系统之中,因此,内核必须找到并挂载一个文件系统 ...
- photoshop最全快捷键列表
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C] 切片工 ...
- python CSV 文件的读写
1.CSV文件 import csv with open(r"E:\code\0_DataSet\tianchi_2015_mobile_recommand\fresh_comp_offli ...
- mybatis 传参是 list<string> 的注意事项
<!--付款 批量 修改账单状态--><update id="editbillpayALL" parameterType="java.util.List ...