模板题 题意说的可能有点不清楚 一开始的点必须在给定的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的更多相关文章

  1. BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)

    Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 1712  Solved: 932[Submit][Status][Discuss] Descripti ...

  2. BZOJ 3489: A simple rmq problem(K-D Tree)

    Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 2579  Solved: 888[Submit][Status][Discuss] Descripti ...

  3. BZOJ 3053: The Closest M Points(K-D Tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1235  Solved: 418[Submit][Status][Discuss] Descripti ...

  4. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  5. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

  6. K-D Tree题目泛做(CXJ第二轮)

    题目1: BZOJ 2716 题目大意:给出N个二维平面上的点,M个操作,分为插入一个新点和询问到一个点最近点的Manhatan距离是多少. 算法讨论: K-D Tree 裸题,有插入操作. #inc ...

  7. k-d tree模板练习

    1. [BZOJ]1941: [Sdoi2010]Hide and Seek 题目大意:给出n个二维平面上的点,一个点的权值是它到其他点的最长距离减最短距离,距离为曼哈顿距离,求最小权值.(n< ...

  8. luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree

    k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...

  9. 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 ...

  10. k-d tree 学习笔记

    以下是一些奇怪的链接有兴趣的可以看看: https://blog.sengxian.com/algorithms/k-dimensional-tree http://zgjkt.blog.uoj.ac ...

随机推荐

  1. SSH配置免秘钥登录

    一.  SSH 配置免秘要登录 配置SSH 免秘要登录,虽然就那么几步,但总是会出现点小问题,今天就做下记录.SSH 免秘钥就是让两台机器相互信任,不需要输入密码就能相互登录.配置相互信任就是把各自的 ...

  2. Java 基础巩固:IO

    在学习IO的时候发现IO的类太多,如InputStream下面就用ReaderInputStream.InputStreamBuffer等等, 还用Reader.Writer.OutputStream ...

  3. <转> python的垃圾回收机制

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  4. HUST 1354 - Rubiks (DP)

    1354 - Rubiks 时间限制:1秒 内存限制:64兆 452 次提交 102 次通过 题目描述 Isun is a genius. Not only he is an expert in al ...

  5. 关于webpack打包js和css

    废话不多说,直接贴出代码,大家瞅瞅:其中要引用css的话是要用css-loader.用了之后再webpack.config.js里面配置相应的代码,并且在相应的js文件里面引用即可啦,不知道有哪位大神 ...

  6. jquery遍历json与数组方法总结

    来自:http://www.php100.com/html/program/jquery/2013/0905/5927.html 先我们来参考each() 方法,each()规定为每个匹配元素规定运行 ...

  7. nodejs开发解决方案

    1.2. 统一环境 开发环境 nvm nrm nodejs 0.10.38 node-inspector 部署环境 nvm nrm iojs 2.x pm2 nginx 异步流程控制:Promise是 ...

  8. Use Private Members from Base Class

    最近看了一段代码:在导出类中调用继承自基类的某个public函数,该函数中对基类中的private数据成员 进行了赋值并将结果打印.看到程序的运行结果后,顿时感觉自己之前(我之前的理解这里就不说明了) ...

  9. 20160419 while练习,复习

    10 一.while和if题目练习 . 二.知识拓展 1. C#中的委托是什么?事件是不是一种委托? 答 :    委托可以把一个方法作为参数代入另一个方法.委托可以理解为指向一个函数的引用.     ...

  10. LeetCode:为运算表达式设置优先级【241】

    LeetCode:为运算表达式设置优先级[241] 题目描述 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含  ...