Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=2716
Since the distance is defined as the Manhattan distance, we first remove the absolute value symbol. This can be divided into four cases, but we can do some processing of the point horizontal ordinate (processing method can be seen in the program), you can make four kinds of situations become one: Ax>bx,ay>by. The distance is then expressed as dist (A, b) = (Ax+ay)-(Bx+by). For a point, we just need to find the largest point on the bottom left of the x+y. Then the problem becomes a simple CDQ division problem.
But like this slag of mine, tle several times, read into the optimization and some small optimization add in before, alas, too weak.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 #defineINF 40000006 #defineMAXN 10000107 using namespacestd;8 intN,m,cnt,tot,pos[maxn],ans[maxn],t[maxn],maxx,maxy;9 structfuck{intX,y,id,op;} E[MAXN],F[MAXN];Ten intRead () { One intx=0, f=1;Charch; A for(Ch=getchar ();ch<'0'|| Ch>'9'; Ch=getchar ())if(ch=='-') f=-1; - for(; ch>='0'&&ch<='9'; Ch=getchar ()) x=x*Ten+ch-'0'; - returnx*F; the } - voidChangeintXintY) { for(intI=x;i<=maxy;i+=i&-i) t[i]=Max (t[i],y);} - intQueryintx) {inty=0; for(intI=x;i;i-=i&-i) Y=max (Y,t[i]);returny==0?-inf:y;} - BOOLComp (Fuck A,fuck b) {returna.x<b.x;} + voidSolveintLintR) { - if(L==R)return; + intMid= (l+r) >>1; ASolve (L,mid); Solve (mid+1, R); atSort (f+l,f+mid+1, comp); Sort (f+mid+1, f+r+1, comp); - intI=l,j=mid+1, last=0; - while(j<=R) { - while(i<=mid&&f[i].op==2) i++; - while(j<=r&&f[j].op==1) J + +; - if(i<=mid&&f[i].x<=f[j].x) Change (F[I].Y,F[I].X+F[I].Y), last=i++; in Else if(j<=r) Ans[f[j].id]=min (Ans[f[j].id],f[j].x+f[j].y-query (F[J].Y)), J + +; - } to for(inti=l;i<=last;i++){ + if(f[i].op==1) - for(intJ=F[I].Y;J<=MAXY;J+=J&-J) t[j]=0; the } * } $ intMain () {Panax NotoginsengN=read (); m=read (); - for(intI=1; i<=n+m;i++) ans[i]=inf; the for(intI=1; i<=n;i++){ +e[i].op=1; E[i].id=i; E[i].x=read () +1; E[i].y=read () +1; AMaxx=max (maxx,e[i].x); maxy=Max (MAXY,E[I].Y); the } + inttype,x,y; - for(inti=n+1; i<=n+m;i++){ $E[i].op=read (); E[i].id=i; E[i].x=read () +1; E[i].y=read () +1; $Maxx=max (maxx,e[i].x); maxy=Max (MAXY,E[I].Y); - if(e[i].op==2) pos[++cnt]=i; - } themaxx++; maxy++; tot=n+m; - for(intI=1; i<=tot;i++) F[i]=e[i]; Solve1, tot);Wuyi for(intI=1; i<=tot;i++) f[i]=e[i],f[i].x=maxx-f[i].x; Solve1, tot); the for(intI=1; i<=tot;i++) F[i]=e[i],f[i].y=maxy-f[i].y; Solve1, tot); - for(intI=1; i<=tot;i++) F[i]=e[i],f[i].x=maxx-f[i].x,f[i].y=maxy-f[i].y; Solve1, tot); Wu for(intI=1; i<=cnt;i++) printf ("%d\n", Ans[pos[i]]); - return 0; About}
bzoj2716: [Violet 3] Angel Doll