bzoj2716 [Violet 3]天使玩偶

http://www.elijahqi.win/2018/01/11/bzoj2648-sjy%e6%91%86%e6%a3%8b%e5%ad%90/

#include
#include
#include
#define inf 0x3f3f3f3f
#define N 1100000 
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if(T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
    return x*f;
}
struct node{
    int left,right,x,y,minx,miny,maxx,maxy;
}tree[N];int D;
struct node1{
    int x,y;
    friend bool operator<(node1 a,node1 b){
        if (!D) return a.xx;else return a.yy;
    }
}point[N],P;
int n,m,root,num,ans;
inline void update(int x){
    int l=tree[x].left,r=tree[x].right;
    tree[x].minx=min(tree[x].minx,min(tree[l].minx,tree[r].minx));
    tree[x].miny=min(tree[x].miny,min(tree[l].miny,tree[r].miny));
    tree[x].maxx=max(tree[x].maxx,max(tree[l].maxx,tree[r].maxx));
    tree[x].maxy=max(tree[x].maxy,max(tree[l].maxy,tree[r].maxy));
}
inline void build(int &x,int l,int r,int dim){
    int mid=l+r>>1;D=dim;nth_element(point+l,point+mid,point+r+1);//printf("%d %d %d\n",l,mid,r);
    //for (int i=l;i<=r;++i) printf("%d %d\n",point[i].x,point[i].y);
    x=++num;tree[x].x=point[mid].x;tree[x].y=point[mid].y;
    tree[x].minx=tree[x].maxx=point[mid].x;tree[x].miny=tree[x].maxy=point[mid].y;
    if (lx].left,l,mid-1,dim^1);
    if (r>mid) build(tree[x].right,mid+1,r,dim^1);update(x);
}
inline void init(int &x){
    x=++num;tree[x].x=P.x;tree[x].y=P.y;
    tree[x].minx=tree[x].maxx=P.x;tree[x].miny=tree[x].maxy=P.y;
}
inline void insert1(int x,int dim){
    if (!dim){
        if (P.xx].x){
            if(tree[x].left) insert1(tree[x].left,dim^1);else init(tree[x].left);
        }else{
            if (tree[x].right) insert1(tree[x].right,dim^1);else init(tree[x].right);
        }
    }else{
        if (P.yx].y){
            if(tree[x].left) insert1(tree[x].left,dim^1);else init(tree[x].left);
        }else{
            if (tree[x].right) insert1(tree[x].right,dim^1);else init(tree[x].right);
        }
    }update(x);
}
inline int calc(node a,node1 b){
    return abs(a.x-b.x)+abs(a.y-b.y);
}
/*inline int calc1(int x){
    return (abs(P.x-tree[x].minx)+abs(tree[x].maxx-P.x)-(tree[x].maxx-tree[x].minx)+
    abs(P.y-tree[x].miny)+abs(tree[x].maxy-P.y)-(tree[x].maxy-tree[x].miny))/2;
}*/
inline int calc1(int x){
    int tmp=0;
    tmp+=max(0,P.x-tree[x].maxx);tmp+=max(0,P.y-tree[x].maxy);
    tmp+=max(0,tree[x].minx-P.x);tmp+=max(0,tree[x].miny-P.y);return tmp;
}
inline void query(int x,int dim){
    int disl=inf,disr=inf;ans=min(calc(tree[x],P),ans);
    if (tree[x].left) disl=calc1(tree[x].left);if (tree[x].right) disr=calc1(tree[x].right);
    if (dislif (dislx].left,dim^1);
        if (disrx].right,dim^1);
    }else{
        if (disrx].right,dim^1);
        if (dislx].left,dim^1);
    }
}
void print(int x,int dim){
    if (tree[x].left) print(tree[x].left,dim^1);
    printf("%d %d %d %d %d %d\n",tree[x].x,tree[x].y,tree[x].minx,tree[x].maxx,tree[x].miny,tree[x].maxy);
    if (tree[x].right) print(tree[x].right,dim^1);
}
int main(){
    freopen("bzoj2716.in","r",stdin);
    n=read();m=read();tree[0].minx=tree[0].miny=inf;tree[0].maxx=tree[0].maxy=0;
    for (int i=1;i<=n;++i) point[i].x=read(),point[i].y=read(); 
    build(root,1,n,0);
    for (int i=1;i<=m;++i){
        int op=read();P.x=read(),P.y=read();
        if (op==1) insert1(root,0);else ans=inf,query(root,0),printf("%d\n",ans);//print(root,0);printf("fdsf\n");
    }
    return 0;
}

你可能感兴趣的:(kd-tree)