题目: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. 2017-2018-1 20179209《Linux内核原理与分析》第九周作业

    理解进程调度时机 进程调度时机 中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule(): 内 ...

  2. go语言之并发编程一

    Go语言最大的优势就在于并发编程.Go语言的关键字go就是开启并发编程也就是goroutine的唯一途径.一条go语句以为着一个函数或方法的并发执行.Go语句是由go关键字和表达式组成.比如下面的这种 ...

  3. python数据分析之:数据聚合与分组运算

    在数据库中,我们可以对数据进行分类,聚合运算.例如groupby操作.在pandas中同样也有类似的功能.通过这些聚合,分组操作,我们可以很容易的对数据进行转换,清洗,运算.比如如下图,首先通过不同的 ...

  4. - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, ma

    $ composer install Loading composer repositories with package information Installing dependencies (i ...

  5. Broadcast Intent & Broadcast Receiver

    当Android系统发生某种状况,必须通知所有程序进行处理时,例如电量不足等,可利用Broadcast Intent对象的功能来进行信息广播. 运行机制包括两部:送出Intent对象的程序:监听广播信 ...

  6. GDI+在绘制验证码中的使用

    GDI+最简单的理解就是用来绘图的.其中包括点.直线.矩形.字符串等等. 先简单来个例子,说明如何在winform窗体中绘制一条直线,并且这条直线不随着窗体的移动而消失. using System; ...

  7. Sql Server2008——存储过程编程简单例子

    主要介绍: 存储过程的定义方法及其使用方法. 实例介绍: 1 创建学生表Student create database Stu use Stu go CREATE TABLE Student ( Sn ...

  8. Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion

    http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...

  9. 吴恩达机器学习笔记(三) —— Regularization正则化

    主要内容: 一.欠拟合和过拟合(over-fitting) 二.解决过拟合的两种方法 三.正则化线性回归 四.正则化logistic回归 五.正则化的原理 一.欠拟合和过拟合(over-fitting ...

  10. codeforces 460D:Little Victor and Set

    Description Little Victor adores the sets theory. Let us remind you that a set is a group of numbers ...