bzoj1941

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj1941相关的知识,希望对你有一定的参考价值。

?????????nbsp   ??????   friend   res   \n   upd   lse   pac   etc   

K-D TREE?????????????????????

?????????????????????????????????????????????????????????????????????11292ms?????????1548ms(??????????????????????????????????????????)

?????????????????????????????????maxans/minans????????????/??????

 

???????????????????????????????????????????????????????????????????????????kd-tree????????????????????????????????????????????????

?????????mindis????????????????????????????????????????????????

#include<cstdio>
#include<cctype>
#include<cmath>
#include<algorithm>
#define maxn 500002
using namespace std;
int n,m,cmpd,rt,maxans,minans;
struct data{
    int x[2],mn[2],mx[2],l,r;
    friend bool operator<(data a,data b){return a.x[cmpd]<b.x[cmpd];}
    friend int dis(data a,data b){return abs(a.x[0]-b.x[0])+abs(a.x[1]-b.x[1]);}
    friend int maxdis(data a,data b){return max(abs(a.x[1]-b.mn[1]),abs(a.x[1]-b.mx[1]))+max(abs(a.x[0]-b.mn[0]),abs(a.x[0]-b.mx[0]));}
    friend int mindis(data a,data b){ int s=0;for(int i=0;i<=1;i++)s+=max(a.x[i]-b.mx[i],0),s+=max(b.mn[i]-a.x[i],0); return s; }//?????????mindis?????????????????????????????????????????? 
}tr[maxn];
void read(int &x){
    char ch=getchar();x=0;int f=1;
    while(!isdigit(ch)){if(ch==???-???)f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-???0???;ch=getchar();}
    x*=f;
}
void updata(int x){
    int ls=tr[x].l,rs=tr[x].r;
    if(ls>0){
        if(tr[ls].mn[0]<tr[x].mn[0])tr[x].mn[0]=tr[ls].mn[0];
        if(tr[ls].mn[1]<tr[x].mn[1])tr[x].mn[1]=tr[ls].mn[1];
        if(tr[ls].mx[0]>tr[x].mx[0])tr[x].mx[0]=tr[ls].mx[0];
        if(tr[ls].mx[1]>tr[x].mx[1])tr[x].mx[1]=tr[ls].mx[1];
    }
    if(rs>0){
        if(tr[rs].mn[0]<tr[x].mn[0])tr[x].mn[0]=tr[rs].mn[0];
        if(tr[rs].mn[1]<tr[x].mn[1])tr[x].mn[1]=tr[rs].mn[1];
        if(tr[rs].mx[0]>tr[x].mx[0])tr[x].mx[0]=tr[rs].mx[0];
        if(tr[rs].mx[1]>tr[x].mx[1])tr[x].mx[1]=tr[rs].mx[1];
    }
}
int buildtr(int l,int r,int flag){
    cmpd=flag;
    int mid=(l+r)>>1;
    nth_element(tr+l,tr+mid,tr+r+1);
    tr[mid].l=tr[mid].r=0;
    tr[mid].mn[0]=tr[mid].mx[0]=tr[mid].x[0];
    tr[mid].mn[1]=tr[mid].mx[1]=tr[mid].x[1];
    if(l<mid)tr[mid].l=buildtr(l,mid-1,!flag);
    if(r>mid)tr[mid].r=buildtr(mid+1,r,!flag);
    updata(mid);
    return mid;
}
void getmx(int whic,int x){
    if(!x)return;
    maxans=max(maxans,dis(tr[whic],tr[x]));
    int ls=tr[x].l,rs=tr[x].r,ldis=-1e9,rdis=-1e9;
/*    if(ls>0 && ldis>maxans)getmx(whic,ls);
    if(rs>0 && rdis>maxans)getmx(whic,rs);*/
    if (ls)ldis=maxdis(tr[whic],tr[ls]);if(rs)rdis=maxdis(tr[whic],tr[rs]);
    if (ldis>rdis){if(ldis>maxans) getmx(whic,ls);if(rdis>maxans) getmx(whic,rs);}
    else { if (rdis>maxans) getmx(whic,rs); if (ldis>maxans) getmx(whic,ls); }
}


void getmn(int whic,int x){
    if(!x)return;
    if(whic!=x)minans=min(minans,dis(tr[whic],tr[x]));
    int ldis=1e9,rdis=1e9,ls=tr[x].l,rs=tr[x].r;
    if (ls) ldis=mindis(tr[whic],tr[ls]); if (rs) rdis=mindis(tr[whic],tr[rs]);
    /*if(ls>0 && ldis<minans)getmn(whic,ls);
    if(rs>0 && rdis<minans)getmn(whic,rs);*/
    if (ldis<rdis){if(ldis<minans)getmn(whic,ls);if(rdis<minans)getmn(whic,rs); }
        else {if(rdis<minans)getmn(whic,rs);if(ldis<minans)getmn(whic,ls);}
}

int query(){
    int res=2e9;
    for(int i=1;i<=n;i++){
        maxans=-1e9;minans=1e9;
        getmx(i,rt);
        getmn(i,rt);
        res=min(res,maxans-minans);
    }
    return res;
}
int main(){
    read(n);
    for(int i=1;i<=n;i++)read(tr[i].x[0]),read(tr[i].x[1]);
    rt=buildtr(1,n,0);
    printf("%d\n",query());
}

 

以上是关于bzoj1941的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 1941 Sdoi2010 Hide ans Seek

[BZOJ1941][Sdoi2010]Hide and Seek

BZOJ-1941Hide and Seek KD-Tree

bzoj1941 Sdoi2010—Hide and Seek

BZOJ 1941 [Sdoi2010]Hide and Seek

bzoj1941 hdu5992