# BZOJ2716：[Violet 3]天使玩偶——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=2716

2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

### 样例输出

1
2

————————————————————————————————

x+y-(x1+y1)      (x>x1,y>y1)

x-y-(x1-y1)        (x>x1,y<y1)

-x+y-(y1-x1)      (x<x1,y>y1)

-x-y-(-x1-y1)      (x<x1,y<y1)

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=500010;
const int N=1000010;
const int INF=10*N;
int X=0,w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
struct rem{
int t;
int x;
int y;
int pos;
}q[2*M],tmp[2*M];
int m,n,cnt=0,ans[M],tree[N],maxy=-1;
bool cmp(rem a,rem b){
return (a.t<b.t||(a.t==b.t&&a.x<b.x)||(a.t==b.t&&a.x==b.x&&a.y<b.y)||(a.t==b.t&&a.x==b.x&&a.y==b.y&&a.pos<b.pos));
}
inline int lowbit(int t){return t&(-t);}
for(int i=x;i<=maxy;i+=lowbit(i))tree[i]=max(tree[i],y);
return;
}
int query(int x){
int res=-INF;
for(int i=x;i>0;i-=lowbit(i))res=max(res,tree[i]);
return res;
}
void cdq(int l,int r){
if(l>=r)return;
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
for(int i=l,j=l,p=mid+1;i<=r;i++){
if(j<=mid&&(p>r||q[j].x<=q[p].x))tmp[i]=q[j++];
else tmp[i]=q[p++];
}
///////////
for(int i=l;i<=r;i++){
q[i]=tmp[i];
if(q[i].t>mid&&q[i].pos)ans[q[i].pos]=min(ans[q[i].pos],q[i].x+q[i].y-query(q[i].y));
}
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos){
for(int j=q[i].y;j<=maxy;j+=lowbit(j))tree[j]=-INF;
}
}
///////////
for(int i=l;i<=r;i++){
if(q[i].t>mid&&q[i].pos)ans[q[i].pos]=min(ans[q[i].pos],q[i].x-q[i].y-query(maxy-q[i].y));
}
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos){
for(int j=maxy-q[i].y;j<=maxy;j+=lowbit(j))tree[j]=-INF;
}
}
///////////
for(int i=r;i>=l;i--){
if(q[i].t>mid&&q[i].pos)ans[q[i].pos]=min(ans[q[i].pos],-q[i].x+q[i].y-query(q[i].y));
}
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos){
for(int j=q[i].y;j<=maxy;j+=lowbit(j))tree[j]=-INF;
}
}
///////////
for(int i=r;i>=l;i--){
if(q[i].t>mid&&q[i].pos)ans[q[i].pos]=min(ans[q[i].pos],-q[i].x-q[i].y-query(maxy-q[i].y));
}
for(int i=l;i<=r;i++){
if(q[i].t<=mid&&!q[i].pos){
for(int j=maxy-q[i].y;j<=maxy;j+=lowbit(j))tree[j]=-INF;
}
}
///////////
return;
}
void clear(){
maxy++;
for(int i=1;i<=cnt;i++)ans[i]=INF;
for(int i=1;i<=maxy;i++)tree[i]=-INF;
return;
}
int main(){
for(int i=1;i<=n;i++){
q[i].t=i;
maxy=max(maxy,q[i].y);
}
for(int i=n+1;i<=m+n;i++){
if(w==1){
q[i].t=i;
maxy=max(maxy,q[i].y);
}else{
q[i].t=i;
q[i].pos=++cnt;
maxy=max(maxy,q[i].y);
}
}
sort(q+1,q+m+n+1,cmp);
clear();
cdq(1,m+n);
for(int i=1;i<=cnt;i++)printf("%d\n",ans[i]);
return 0;
}

posted @ 2017-12-16 14:49  luyouqi233  阅读(245)  评论(0编辑  收藏  举报