bzoj1941 [Sdoi2010]Hide and Seek

Description
小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏—捉迷藏。 但是,他们觉得,玩普通的捉迷藏没什么意思,还是不够寂寞,于是,他们决定玩寂寞无比的螃蟹版捉迷藏,顾名思义,就是说他们在玩游戏的时候只能沿水平或垂直方向走。一番寂寞的剪刀石头布后,他们决定iPig去捉giPi。由于他们都很熟悉PKU的地形了,所以giPi只会躲在PKU内n个隐秘地点,显然iPig也只会在那n个地点内找giPi。游戏一开始,他们选定一个地点,iPig保持不动,然后giPi用30秒的时间逃离现场(显然,giPi不会呆在原地)。然后iPig会随机地去找giPi,直到找到为止。由于iPig很懒,所以他到总是走最短的路径,而且,他选择起始点不是随便选的,他想找一个地点,使得该地点到最远的地点和最近的地点的距离差最小。iPig现在想知道这个距离差最小是多少。 由于iPig现在手上没有电脑,所以不能编程解决这个如此简单的问题,所以他马上打了个电话,要求你帮他解决这个问题。iPig告诉了你PKU的n个隐秘地点的坐标,请你编程求出iPig的问题

Input
第一行输入一个整数N 第2~N+1行,每行两个整数X,Y,表示第i个地点的坐标
Output
一个整数,为距离差的最小值。

Sample Input
4
0 0
1 0
0 1
1 1

Sample Output
1

分析:
kdtree

tip

一开始疯狂TLE,后来发现是一个int函数的返回值写错了。。。orz
改了以后又WA
经过我严密的排查之后,发现是在ask函数中
一开始看RZZdalao的代码时
ta是这样写的:

if (d0<ans) ans=d0;

但是因为这道题是询问平面内的点与平面内其他点的最近和最远距离差

暗含的要求就是两点不能重合

这就要求我们的判断变成这样:

if (d0) ans=min(ans,d0);
这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

const int N=1000003;
const int INF=0x33333333;
struct node{
    int l,r,d[2],mn[2],mx[2];
};
node t[N];
int root,cmpd,x,y,n,ansx,ansy,ans;

int cmp(const node &a,const node &b)
{
    return ((a.d[cmpd]<b.d[cmpd])||((a.d[cmpd]==b.d[cmpd])&&(a.d[!cmpd]<b.d[!cmpd])));
}

void update(int bh)
{
    int lc=t[bh].l;
    int rc=t[bh].r;
    if (lc)
    {
        t[bh].mn[0]=min(t[bh].mn[0],t[lc].mn[0]);
        t[bh].mn[1]=min(t[bh].mn[1],t[lc].mn[1]);
        t[bh].mx[0]=max(t[bh].mx[0],t[lc].mx[0]);
        t[bh].mx[1]=max(t[bh].mx[1],t[lc].mx[1]);
    }
    if (rc)
    {
        t[bh].mn[0]=min(t[bh].mn[0],t[rc].mn[0]);
        t[bh].mn[1]=min(t[bh].mn[1],t[rc].mn[1]);
        t[bh].mx[0]=max(t[bh].mx[0],t[rc].mx[0]);
        t[bh].mx[1]=max(t[bh].mx[1],t[rc].mx[1]);
    }
}

int build(int l,int r,int D)
{
    cmpd=D;
    int mid=(l+r)>>1;
    nth_element(t+l+1,t+mid+1,t+r+1,cmp);
    t[mid].mn[0]=t[mid].mx[0]=t[mid].d[0];
    t[mid].mn[1]=t[mid].mx[1]=t[mid].d[1];
    if (l!=mid) t[mid].l=build(l,mid-1,!D);
    if (r!=mid) t[mid].r=build(mid+1,r,!D);
    update(mid);
    return mid;  //
}

int dis1(int p,int x,int y)
{
    int d=0;
    if (x<t[p].mn[0]) d+=(t[p].mn[0]-x);
    if (x>t[p].mx[0]) d+=(x-t[p].mx[0]);
    if (y<t[p].mn[1]) d+=(t[p].mn[1]-y);
    if (y>t[p].mx[1]) d+=(y-t[p].mx[1]);
    return d;
}

int dis2(int p,int x,int y)
{
    int d=0;
    d=max(abs(t[p].mx[0]-x),abs(t[p].mn[0]-x));
    d+=(max(abs(t[p].mx[1]-y),abs(t[p].mn[1]-y)));
    return d;
}

void askx(int now)   //min
{
    int d0,dl,dr;
    d0=abs(t[now].d[0]-x)+abs(t[now].d[1]-y);
    if (d0) ansx=min(ansx,d0);
    if (t[now].l) dl=dis1(t[now].l,x,y);
    else dl=INF;
    if (t[now].r) dr=dis1(t[now].r,x,y);
    else dr=INF;
    if (dl<dr)
    {
        if (dl<ansx) askx(t[now].l);
        if (dr<ansx) askx(t[now].r); 
    }
    else
    {
        if (dr<ansx) askx(t[now].r);
        if (dl<ansx) askx(t[now].l);
    }
    return;
}

void asky(int now)   //max
{
    int d0,dl,dr;
    d0=abs(t[now].d[0]-x)+abs(t[now].d[1]-y);
    if (d0) ansy=max(ansy,d0); 
    if (t[now].l) dl=dis2(t[now].l,x,y);
    else dl=0;
    if (t[now].r) dr=dis2(t[now].r,x,y);
    else dr=0;
    if (dl>dr)
    {
        if (dl>ansy) asky(t[now].l);
        if (dr>ansy) asky(t[now].r); 
    }
    else
    {
        if (dr>ansy) asky(t[now].r);
        if (dl>ansy) asky(t[now].l);
    }
    return;
}

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d%d",&t[i].d[0],&t[i].d[1]);
    root=build(1,n,0);
    ans=INF;               ///
    for (int i=1;i<=n;i++)
    {
        x=t[i].d[0];
        y=t[i].d[1];
        ansx=INF; ansy=0;
        askx(root);  //
        asky(root);  //
        ans=min(ans,ansy-ansx);
    }
    printf("%d",ans);
    return 0;   
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673358.html