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 ...
随机推荐
- [Spring Data MongoDB]学习笔记--MapReduce
mongodb的MapReduce主要包含两个方法:map和reduce. 举个例子,假设现在有下面3条记录 { "_id" : ObjectId("4e5ff893c0 ...
- 《从零开始学Swift》学习笔记(Day 31)——存储属性
原创文章,欢迎转载.转载请注明:关东升的博客 Swift中的属性分为存储属性和计算属性,存储属性就是Objective-C中的数据成员,计算属性不存储数据,但可以通过计算其他属性返回数据. 存储属性可 ...
- IIPP迷你项目(一)“Rock-paper-scissor-lizard-Spock”
0 前言——关于IIPP 本系列博客的内容均来自<An Introduction to Interactive Programming in Python (Part 1)>(在此我简称为 ...
- E - Rails (栈)
E - Rails Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description The ...
- Instapaper 使用经验和技巧
Instapaper 分类本质是文件夹整理,没有标签. 文件夹意味着一篇文章只能放在一个文件夹里,不像标签可以实现一篇文章多个标签的功能. 一.文件夹和Like功能 1.已有文件夹: Home:存放所 ...
- Spring整合Velocity模版引擎
1. 首先通过pom.xml自动加载velocity扩展包到工程: <dependency> <groupId>velocity</groupId> <art ...
- HTML随意记录
HTML特殊符号对照表: http://www.cnblogs.com/knowledgesea/archive/2013/07/24/3210703.html
- Nulls
Nullshttps://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements005.htm
- mysql 5.7.18版本 sql_mode 问题
only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了. only_full_group_by ,感觉,group by 将变成和 di ...
- Security Report: Stop using relative path to import CSS files
Detecting and exploiting path-relative stylesheet import (PRSSI) vulnerabilities Early last year G ...