Description
Rotten girl to Birthday, Pty want to give a gift, but rotten girl in the classroom away from Pty classroom too far, so Pty will be moved and a star's Djy help send gifts. Djy in the school set up a plane Cartesian coordinate system, he stood at (0,0) point, the rotten girl at (x0,y0) point, djy each can only go up and down about four directions to move one step, in the middle of a rectangular teaching building, each teaching building gives two diagonal coordinates, and ensure that the surrounding area of each teaching building (as shown) there will be no other teaching building, that is, DJY can walk around a teaching building will not encounter any obstacles, now djy want to know from the starting point to the end of any teaching building, the shortest number of steps required.
Data Constraint
Ensure that all y-coordinates are in [ -10^6,10^6]
All x-coordinates in [0,10^6]
70% Data Guarantee: n<=1000
100% Data Guarantee: n<=10^5 solution
Obviously we find that the starting point (0,0) only goes to the right, not to the left. So we scan the line from left to right, maintaining a tree of segments, each node in the tree represents the shortest distance from the starting point to the current ordinate. Each time you encounter a left endpoint, the corresponding interval [l,r] value is emptied, and a right-side iodine is encountered to update the corresponding interval [l,r] with the values of L-1 and r+1. Code
#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <
Algorithm> using namespace std;
const int maxn1=2e5+5,maxn=1e6+1; struct code{int x,y,yy,z;}
A[MAXN1]; struct code1{int bz,sum,bz1;}
F[MAXN*10];
int n,i,t,j,k,l,sx,sy,x,y,z,ans,bz; BOOL CMP (code X,code y) {return x.x<y.x | | x.x==y.x && x.z>y.z;} void Insert (int l,int R,int v) {in
T mid= (l+r)/2;
if (l==r) {f[v].sum=abs (L-MAXN); return;
Insert (l,mid,v*2); insert (mid+1,r,v*2+1);
} void make (int v) {if (f[v].bz==1) f[v*2].bz=f[v*2+1].bz=1;
else if (f[v].bz==2) F[v*2].bz=f[v*2+1].bz=2,f[v*2].bz1=f[v*2+1].bz1=f[v].bz1;
else if (f[v].bz==3) F[v*2].bz=f[v*2+1].bz=3,f[v*2].bz1=f[v*2+1].bz1=f[v].bz1;
} void Change (int l,int r,int v,int x,int y) {int mid= (L+R)/2;
if (x>y) return;
if (f[v].bz) {if (l!=r) make (v);
else if (f[v].bz==1) f[v].sum=maxn*1000; else if (f[v].bz==2) F[v].sum=f[v].bz1+l;
else f[v].sum=f[v].bz1-l;
f[v].bz=0;
} if (L>=x && r<=y) {f[v].bz=z;
if (z!=1) f[v].bz1=t;
Return
} if (l<=y && mid>=x) Change (l,mid,v*2,x,y);
if (mid<y && r>=x) Change (mid+1,r,v*2+1,x,y);
} void Find (int l,int r,int v,int x) {int mid= (L+R)/2;
if (f[v].bz) {if (l!=r) make (v);
else if (f[v].bz==1) f[v].sum=maxn*1000;
else if (f[v].bz==2) f[v].sum=f[v].bz1+l;
else f[v].sum=f[v].bz1-l;
f[v].bz=0;
} if (l==r) {t=f[v].sum;
Return
} if (mid>=x) find (l,mid,v*2,x);
else find (mid+1,r,v*2+1,x);
} int main () {freopen ("bl.in", "R", stdin), Freopen ("Bl.out", "w", stdout);
scanf ("%d%d%d", &sx,&sy,&n); SY+=MAXN; for (i=1;i<=n;i++) {scanf ("%d%d%d%d", &a[i].x,&a[i].y,&a[i+n].x,&a[i].yy), a[i].y+=maxn,a[i].yy
+=MAXN;
if (a[i+n].x<a[i].x) a[i+n].z=1; Else A[i].z=1;
if (a[i].y>a[i].yy) swap (a[i].y,a[i].yy); a[i+n].y=a[i].y,a[i+n].yy=a[i].yy;
} sort (a+1,a+2*n+1,cmp); bz=0;
for (i=1;i<=2*n;i++) {if (A[I].X>SX) break;
if (!BZ) insert (1,2*maxn,1);
if (a[i].z) Z=1,change (1,MAXN*2,1,A[I].Y,A[I].YY);
else{t=0;z=0;
Find (1,maxn*2,1,a[i].y-1); k=t;
Find (1,maxn*2,1,a[i].yy+1); swap (t,k);
x= (a[i].y+a[i].yy-t+k)/2;
T-=a[i].y-1;
Z=2;change (1,maxn*2,1,a[i].y,min (a[i].yy,x)); z=3;t=k+a[i].yy+1;
Change (1,maxn*2,1,max (a[i].y,x+1), a[i].yy);
} bz++;
} t=0;z=1;ans=1e9;
Find (1,maxn*2,1,sy);
T+=SX;
printf ("%d\n", t); }