bzoj2648

板子,和1941非常像

#include<cstdio>
#include<cctype>
#include<cmath>
#include<algorithm>
#define maxn 1000001
using namespace std;
int n,m,rt,cmpd,tot,res;
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 mindis(data b,data a){int s=0;for(int i=0;i<=1;i++)s+=max(b.x[i]-a.mx[i],0)+max(a.mn[i]-b.x[i],0);return s;}
}tr[maxn],T;

int read(){
    char ch=getchar();int x=0,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;
    return x;
}

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].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 insert(int &x,int flag){
    if(!x){tr[x=++n]=T;updata(x);return;}
    if(T.x[flag]<=tr[x].x[flag])insert(tr[x].l,!flag);else insert(tr[x].r,!flag);
    updata(x);
}

void query(int x){
    if(!x)return;
    res=min(res,dis(tr[x],T));
    int ls=tr[x].l,rs=tr[x].r,ldis=1e9,rdis=1e9;
    if(ls)ldis=mindis(T,tr[ls]);if(rs)rdis=mindis(T,tr[rs]);
    if(ldis<rdis){if(ldis<res)query(ls);if(rdis<res)query(rs);}
    else{if(rdis<res)query(rs);if(ldis<res)query(ls);}
}

int main(){
    n=read();m=read();
    for(int i=1;i<=n;i++)tr[i].x[0]=read(),tr[i].x[1]=read();
    rt=buildtr(1,n,0);
    int typ;
    for(int i=1;i<=m;i++){
        typ=read();
        T.x[0]=T.mn[0]=T.mx[0]=read();
        T.x[1]=T.mn[1]=T.mx[1]=read();
        T.l=T.r=0;
        if(typ==1)insert(rt,0);else{res=1e9;query(rt);printf("%d\n",res);}
    }
}

 

posted @ 2018-05-16 20:33  lnyzo  阅读(112)  评论(0编辑  收藏  举报