BZOJ 1941 kd-tree
模板题 题意说的可能有点不清楚 一开始的点必须在给定的n个点里面
所以枚举点 然后ask最大和最小值
估价函数中 最大值的写法和最小值不同 全部取max
而最小值在估价时 如果在某个点管辖的空间里 就视为距离=0
估价函数如果写错 可能会导致wa or t
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<string>
using namespace std;
#define L long long
const int INF = 999999999 ;
int n , root , cmp_d ;
struct node{
int d[2] , Max[2] , Min[2];
int l , r ;
}a[500050] ;
bool cmp(node a,node b){
return ((a.d[cmp_d] < b.d[cmp_d]) || (a.d[cmp_d] == b.d[cmp_d] && a.d[!cmp_d] < b.d[!cmp_d])) ;
}
void up(int p , int k ){
a[p].Max[0] = max(a[p].Max[0] , a[k].Max[0]);
a[p].Max[1] = max(a[p].Max[1] , a[k].Max[1]);
a[p].Min[0] = min(a[p].Min[0] , a[k].Min[0]);
a[p].Min[1] = min(a[p].Min[1] , a[k].Min[1]);
}
int build(int l, int r , int D ) {
int mid = (l+r) >> 1 ;
cmp_d = D;
nth_element(a+1+l,a+1+mid,a+1+r,cmp) ;
a[mid].Max[0] = a[mid].Min[0] = a[mid].d[0];
a[mid].Max[1] = a[mid].Min[1] = a[mid].d[1];
if(l != mid)
a[mid].l = build(l,mid-1,D^1);
else a[mid].l = 0 ;
if(r != mid)
a[mid].r = build(mid+1,r,D^1);
else a[mid].r = 0 ;
if(a[mid].l)up(mid,a[mid].l);
if(a[mid].r)up(mid,a[mid].r);
return mid ;
}
int ans1 , ans2 ;
int x, y ;
int getMaxdis(int p) {
int res = 0 ;
res += max(max(x - a[p].Max[0] , a[p].Max[0] - x) , max(x - a[p].Min[0] , a[p].Min[0] - x)) ;
res += max(max(y - a[p].Max[1] , a[p].Max[1] - y) , max(y - a[p].Min[1] , a[p].Min[1] - y)) ;
return res ;
}
void query_Max(int p) {
int d0 ;
int dl,dr ;
d0 = abs(x - a[p].d[0]) + abs(y - a[p].d[1]) ;
if(d0 != 0) {
ans2 = max(ans2 , d0) ;
}
if(a[p].l)
dl = getMaxdis(a[p].l) ;
else dl = -INF ;
if(a[p].r)
dr = getMaxdis(a[p].r) ;
else dr = -INF ;
if(dl > dr) {
if(dl > ans2)query_Max(a[p].l) ;
if(dr > ans2)query_Max(a[p].r) ;
}
else {
if(dr > ans2)query_Max(a[p].r) ;
if(dl > ans2)query_Max(a[p].l) ;
}
}
int getMindis(int p) {
int res = 0 ;
if(x > a[p].Max[0]) res += x - a[p].Max[0] ;
if(x < a[p].Min[0]) res += a[p].Min[0] - x ;
if(y > a[p].Max[1]) res += y - a[p].Max[1] ;
if(y < a[p].Min[1]) res += a[p].Min[1] - y ;
return res ;
}
void query_Min(int p) {
int d0 , dl , dr ;
d0 = abs(a[p].d[0] - x) + abs(a[p].d[1] - y) ;
if(d0 != 0) {
ans1 = min(d0 , ans1) ;
}
if(a[p].l)
dl = getMindis(a[p].l) ;
else dl = INF ;
if(a[p].r)
dr = getMindis(a[p].r) ;
else dr = INF ;
if(dl < dr) {
if(dl < ans1)query_Min(a[p].l) ;
if(dr < ans1)query_Min(a[p].r) ;
}
else {
if(dr < ans1)query_Min(a[p].r) ;
if(dl < ans1)query_Min(a[p].l) ;
}
}
int main(){
int n ;
scanf("%d",&n);
for(int i = 1; i <= n ; i ++ ){
scanf("%d%d",&a[i].d[0],&a[i].d[1]) ;
}
root = build(1,n,0) ;
int ans = INF ;
for(int i = 1; i <= n ; i ++ ){
ans1 = INF , ans2 = -INF ;
x = a[i].d[0] , y = a[i].d[1] ;
query_Max(root) ;
query_Min(root) ;
ans = min(ans , ans2 - ans1) ;
}
printf("%d\n",ans) ;
}
BZOJ 1941 kd-tree的更多相关文章
- BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)
Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 1712 Solved: 932[Submit][Status][Discuss] Descripti ...
- BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
- BZOJ 4520: [Cqoi2016]K远点对(k-d tree)
Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1162 Solved: 618[Submit][Status][Discuss] Descripti ...
- BZOJ 2648: SJY摆棋子(K-D Tree)
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 6051 Solved: 2113[Submit][Status][Discuss] Descript ...
- K-D Tree题目泛做(CXJ第二轮)
题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...
- k-d tree模板练习
1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...
- luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree
k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...
- AOJ DSL_2_C Range Search (kD Tree)
Range Search (kD Tree) The range search problem consists of a set of attributed records S to determi ...
- k-d tree 学习笔记
以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...
随机推荐
- 使用jenkins进行Android的持续集成
关于持续集成的定义和意义可以参考它的 百度百科 主要意义有以下几点: 减少风险 减少重复过程 任何时间.任何地点生成可部署的软件 增强项目的可见性 建立团队对开发产品的信心 持续集成的实施 持续集成的 ...
- JavaWeb关于session生命周期的几种设置方法
一般session的生命周期都是建立在用户登录系统后对用户信息进行一个记录,session类似于你有一张银行卡,而卡里的钱就是属于session存储的信息,卡掉了就不能取出里面的钱. 以前sessio ...
- 爬虫实战【8】Selenium解析淘宝宝贝-获取多个页面
作为全民购物网站的淘宝是在学习爬虫过程中不可避免要打交道的一个网站,而是淘宝上的数据真的很多,只要我们指定关键字,将会出现成千上万条数据. 今天我们来讲一下如何从淘宝上获取某一类宝贝的信息,比如今天我 ...
- Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Linux下OpenOffice的安装与启动
公司项目需求中增加了文档预览功能,所以采用了OpenOffice提供的将office文件转换为pdf的工具.那么我们的程序运行在服务器端,服务器系统版本多是Linux,因此有必要记录下Linux下Op ...
- 阻塞(sleep等等)区别 中断(interrupt)+ 中断的意义
不客气地说,至少有一半人认为,线程的"中断"就是让线程停止.如果你也这么认为,那你对多线程编程还没有入门. 在java中,线程的中断(interrupt)只是改变了线程的中断状态, ...
- MYSQL存储引擎介绍--应用场景
MySQL存储引擎通常有哪3种?各自分别有什么特点?应用场景是哪些? MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表.若要修改默 ...
- 九度OJ 1360:乐透之猜数游戏 (递归)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:955 解决:261 题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些 ...
- Quality of service
w https://en.wikipedia.org/wiki/Quality_of_service Quality of service (QoS) is the overall performan ...
- Vue父组件调用子组件的方法
vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如: 父组件: <template> <div @click ...