bzoj2716 [Violet 3]天使玩偶(同bzoj2648)

bzoj2716 [Violet 3]天使玩偶(同bzoj2648)

#include 
#include 
#include 
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 500010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,D,ans,rt=0;
struct point{
    int d[2];
    point(int x=0,int y=0){d[0]=x;d[1]=y;}
    int& operator[](int x){return d[x];}
    friend bool operator<(point a,point b){return a[D]friend int dis(point a,point b){return abs(a[0]-b[0])+abs(a[1]-b[1]);}
}P[N];
struct node{
    point x;int mx[2],mn[2],lc,rc;
}tree[N<<1];
inline void update(int p){
    int l=tree[p].lc,r=tree[p].rc;
    for(int i=0;i<2;++i){
        if(l){
            tree[p].mn[i]=min(tree[p].mn[i],tree[l].mn[i]);
            tree[p].mx[i]=max(tree[p].mx[i],tree[l].mx[i]);
        }if(r){
            tree[p].mn[i]=min(tree[p].mn[i],tree[r].mn[i]);
            tree[p].mx[i]=max(tree[p].mx[i],tree[r].mx[i]);
        }
    }
}
inline void build(int &p,int l,int r,int op){
    int mid=l+r>>1;p=mid;D=op;
    nth_element(P+l,P+mid,P+r+1);tree[p].x=P[mid];
    for(int i=0;i<2;++i) tree[p].mx[i]=tree[p].mn[i]=tree[p].x[i];
    if(l1,op^1);
    if(r>mid) build(tree[p].rc,mid+1,r,op^1);
    update(p);
}
inline void ins(int &p,point x,int op){
    if(!p){
        p=++n;tree[p].x=x;
        for(int i=0;i<2;++i) tree[p].mn[i]=tree[p].mx[i]=tree[p].x[i];return;
    }if(x[op]<=tree[p].x[op]) ins(tree[p].lc,x,op^1);
    else ins(tree[p].rc,x,op^1);update(p);
}
inline int calcmn(int p,point x){
    if(!p) return inf;int res=0;
    for(int i=0;i<2;++i){
        if(x[i]if(x[i]>tree[p].mx[i]) res+=x[i]-tree[p].mx[i];
    }return res;
}
inline void query(int p,point x){
    ans=min(ans,dis(tree[p].x,x));
    int dl=calcmn(tree[p].lc,x),dr=calcmn(tree[p].rc,x);
    if(dlif(dlif(drelse{
        if(drif(dlint main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=1;i<=n;++i)
        for(int k=0;k<2;++k) P[i][k]=read();
    build(rt,1,n,0);
    while(m--){
        int op=read(),x=read(),y=read();
        if(op==1) ins(rt,point(x,y),0);
        else{
            ans=inf;query(rt,point(x,y));
            printf("%d\n",ans);
        }
    }return 0;
}

你可能感兴趣的:(bzoj,K-D-tree)