题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1941

第二道KDtree!

枚举每个点,求出距离它的最远和最近距离。O( n * logn )。

曼哈顿最小估价:max( 0 , s[ cr ].x[ 0 ] - a[ k ].p[ 0 ] ) + max( 0 , a[ k ].p[ 0 ] - s[ cr ].y[ 0 ] ) + max( 0 , s[ cr ].x[ 1 ] - a[ k ].p[ 1 ] ) + max( 0 , a[ k ].p[ 1 ] - s[ cr ].y[ 1 ] ) 。

曼哈顿最大估价:max( abs( s[ cr ].x[ 0 ] - a[ k ].p[ 0 ] ) , abs( a[ k ].p[ 0 ] - s[ cr ].y[ 0 ] ) ) + max( abs( s[ cr ].x[ 1 ] - a[ k ].p[ 1 ] ) , abs( a[ k ].p[ 1 ] - s[ cr ].y[ 1 ] ) ) 。

  (其中0、1表示横坐标、纵坐标,x、y表示最小、最大)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5+,INF=0x3f3f3f3f;
int n,rt,ans=INF,fx,tot,mx,mn;
struct Dt{
int x[],y[],p[],ly;//x:min y:max p:ps
}a[N];
bool cmp(Dt u,Dt v){return u.p[fx]<v.p[fx];}
struct KD{
int c[N][],q;Dt s[N];
void add(int cr,int k)
{ for(int i=;i<=;i++)
s[cr].x[i]=s[cr].y[i]=s[cr].p[i]=a[k].p[i], s[cr].ly=k; }
void pshp(int cr)
{
int ls=c[cr][],rs=c[cr][];
for(int i=;i<=;i++)
{
if(ls) s[cr].x[i]=min(s[cr].x[i],s[ls].x[i]),
s[cr].y[i]=max(s[cr].y[i],s[ls].y[i]);
if(rs) s[cr].x[i]=min(s[cr].x[i],s[rs].x[i]),
s[cr].y[i]=max(s[cr].y[i],s[rs].y[i]);
}
}
void build(int &cr,int l,int r,bool now)
{
int mid=l+r>>; fx=now; nth_element(a+l,a+mid,a+r+,cmp);
cr=++tot; add(cr,mid);
if(l<mid) build(c[cr][],l,mid-,!now);
if(mid<r) build(c[cr][],mid+,r,!now);
pshp(cr);
}
int distx(int cr,int k)
{
int ret=;
for(int i=;i<=;i++)
ret+=max(abs(s[cr].x[i]-a[k].p[i]),abs(a[k].p[i]-s[cr].y[i]));
return ret;
}
int distn(int cr,int k)
{
int ret=;
for(int i=;i<=;i++)
ret+=max(,s[cr].x[i]-a[k].p[i])+max(,a[k].p[i]-s[cr].y[i]);
return ret;
}
int dis(Dt u,Dt v){return abs(u.p[]-v.p[])+abs(u.p[]-v.p[]);}
void queryx(int cr,int l,int r)
{
mx=max(mx,dis(s[cr],a[q]));
int ls=c[cr][], rs=c[cr][], mid=l+r>>;
int dl=(ls?distx(ls,q):-INF), dr=(rs?distx(rs,q):-INF);
if(dl>dr)
{ if(dl>mx) queryx(ls,l,mid-); if(dr>mx) queryx(rs,mid+,r);}
else
{ if(dr>mx) queryx(rs,mid+,r); if(dl>mx) queryx(ls,l,mid-);}
}
void queryn(int cr,int l,int r)
{
if(s[cr].ly!=q) mn=min(mn,dis(s[cr],a[q]));
int ls=c[cr][], rs=c[cr][], mid=l+r>>;
int dl=(ls?distn(ls,q):INF), dr=(rs?distn(rs,q):INF);
if(dl<dr)
{ if(dl<mn) queryn(ls,l,mid-); if(dr<mn) queryn(rs,mid+,r);}
else
{ if(dr<mn) queryn(rs,mid+,r); if(dl<mn) queryn(ls,l,mid-);}
}
}kd;
int rdn()
{
int ret=; char ch=getchar();
while(ch>''||ch<'')ch=getchar();
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
return ret;
}
int main()
{
n=rdn();
for(int i=;i<=n;i++)
a[i].p[]=rdn(),a[i].p[]=rdn();
kd.build(rt,,n,);
for(int i=,t1,t2;i<=n;i++)
{
kd.q=i; mx=-INF; mn=INF;
kd.queryx(rt,,n); kd.queryn(rt,,n);
ans=min(ans,mx-mn);
// printf("ans=%d i=%d mx=%d mn=%d\n",ans,i,mx,mn);
}
printf("%d\n",ans);
return ;
}

bzoj 1941 [Sdoi2010]Hide and Seek——KDtree的更多相关文章

  1. BZOJ 1941: [Sdoi2010]Hide and Seek KDtree + 估价函数

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000 using namespace std; ...

  2. bzoj:1941: [Sdoi2010]Hide and Seek

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 531  Solved: 295[Submi ...

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

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

  4. 【BZOJ1941】[Sdoi2010]Hide and Seek KDtree

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

  5. 【54.08%】【BZOJ 1941】Hide and Seek

    Time Limit: 16 Sec  Memory Limit: 162 MB Submit: 919  Solved: 497 [Submit][Status][Discuss] Descript ...

  6. 【bzoj1941】[Sdoi2010]Hide and Seek KD-tree

    题目描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏---捉迷藏. ...

  7. 【BZOJ-1941】Hide and Seek KD-Tree

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 830  Solved: 455[Submi ...

  8. [BZOJ1941][Sdoi2010]Hide and Seek

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

  9. 【bzoj1941】[Sdoi2010]Hide and Seek(kd-tree)

    bzoj 题意: 给出\(n\)个点,对于每个点,\(d_i\)等于距离其最远的点的距离减去距离最近的点的距离.这里的距离为曼哈顿距离. 求\(min\{d_i\}\). 思路: 考虑直接对每个点暴力 ...

随机推荐

  1. JAVA NIO 简介 (netty源码死磕1.1)

    [基础篇]netty 源码死磕1.1:  JAVA NIO简介 1. JAVA NIO简介 Java 中 New I/O类库 是由 Java 1.4 引进的异步 IO.由于之前老的I/O类库是阻塞I/ ...

  2. mysql数据库访问授权

    1.进入MySQL服务器 d:\mysql\bin\> mysql -h localhost -u root; 2.赋予任何主机访问数据的权限 mysql> GRANT ALL PRIVI ...

  3. Java基础 - 输出

    输出在各个开发语言中起着至关重要的作用,方便编写代码时进行调试,在java中使用 System.out.println 进行输出 public class Main { public static v ...

  4. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  5. 3.写一个hello world页面

    经过1和2的学习,现在已经可以正常启动Django了,这一节说怎么写一个hello world页面,所有的环境基础就是1和2中搭建的 1.在app模块中添加页面 具体为 hello_django\he ...

  6. js原生函数一些封装

    这是一些js原生封装的函数,主要是为了兼容IE浏览器,如下 获取css样式 function getStyle(ele, prop) { if(window.getComputedStyle) { r ...

  7. hd acm1008

    Problem Description The highest building in our city has only one elevator. A request list is made u ...

  8. 剑指offer——和为s的两个数字VS和为s的连续正数序列

    两种方法都类似于快排的变形. #include <iostream> #include <string> using namespace std; bool FindNumbe ...

  9. 《机器学习实战》学习笔记第九章 —— 决策树之CART算法

    相关博文: <机器学习实战>学习笔记第三章 —— 决策树 主要内容: 一.CART算法简介 二.分类树 三.回归树 四.构建回归树 五.回归树的剪枝 六.模型树 七.树回归与标准回归的比较 ...

  10. nginx location 语法

    location 语法location 有”定位”的意思, 根据Uri来进行不同的定位.在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.比如, 碰到.p ...