BZOJ 1941 [Sdoi2010]Hide and Seek

题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=1941

题意:略。

思路:本来以为有什么高论。。。结果就是个KDTree的板子题。。。亏我还想了那么久。。。建好树后,对每个点都查询一下它的最近和最远的点,要先把这个点标记掉不要在查询的时候查询到它,然后更新答案就可以了。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
int D,n,root,X,Y,ID,ans[2];
struct A{int d[2];}a[maxn];
struct T{int d[2],s[2],x[2],y[2];}t[maxn];
bool cmp(const A&a,const A&b){return a.d[D]<b.d[D];}
#define ls t[root].s[0]
#define rs t[root].s[1]
#define oo 1000000000
void umin(int&a,int b){if(a>b)a=b;}
void umax(int&a,int b){if(a<b)a=b;}
void maintain(int f,int x){
    umin(t[f].x[0],t[x].x[0]),umax(t[f].x[1],t[x].x[1]);
    umin(t[f].y[0],t[x].y[0]),umax(t[f].y[1],t[x].y[1]);
}
int getdis(int p,int tp){
    if (tp==0)
        return max(t[p].x[0]-X,0)+max(X-t[p].x[1],0)+max(t[p].y[0]-Y,0)+max(Y-t[p].y[1],0);
    else
        return max(abs(X-t[p].x[1]),abs(t[p].x[0]-X))+max(abs(Y-t[p].y[1]),abs(t[p].y[0]-Y));
}
void query(int root,int tp){
    int tmp=abs(t[root].d[0]-X)+abs(t[root].d[1]-Y),d[2];
    if (ls) d[0]=getdis(ls,tp);else d[0]=tp?-oo:oo;
    if (rs) d[1]=getdis(rs,tp);else d[1]=tp?-oo:oo;
    if (tp==0){
        if (ID!=root) umin(ans[0],tmp),tmp=d[0]>=d[1];
        if (d[tmp]<ans[0]) query(t[root].s[tmp],tp);tmp^=1;
        if (d[tmp]<ans[0]) query(t[root].s[tmp],tp);
    }
    else{
        if (ID!=root) umax(ans[1],tmp),tmp=d[1]>=d[0];
        if (d[tmp]>ans[1]) query(t[root].s[tmp],tp);tmp^=1;
        if (d[tmp]>ans[1]) query(t[root].s[tmp],tp);
    }
}
int build(int l,int r,int d){
    D=d;int root=l+((r-l)>>1);
    nth_element(a+l,a+root,a+r+1,cmp);
    t[root].d[0]=t[root].x[0]=t[root].x[1]=a[root].d[0];
    t[root].d[1]=t[root].y[0]=t[root].y[1]=a[root].d[1];
    if (l<root) ls=build(l,root-1,d^1),maintain(root,ls);
    if (root<r) rs=build(root+1,r,d^1),maintain(root,rs);
    return root;
}
int main(){
    read(n);
    for (int i=1;i<=n;i++) read(a[i].d[0]),read(a[i].d[1]);
    root=build(1,n,0);
    int ret=0;
    for (int i=1;i<=n;i++){
        X=t[i].d[0],Y=t[i].d[1],ID=i;
        ans[0]=oo,ans[1]=-oo;
        query(root,1),query(root,0);
        if (i==1) ret=ans[1]-ans[0];
        else ret=min(ret,ans[1]-ans[0]);
    }
    printf("%d\n",ret);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注