Title Description
The leader has recently been plagued by too many people to worship him, so he added a barrier to his garden.
The area near the leader's garden can be pumped into a network of square grids, each corresponding to a coordinate (all integers, possibly negative), if two meshes (x1, y1), (x2, y2) have |x1-x2| + |y1-y2| = 1, the two meshes are adjacent, otherwise they are not adjacent.
The leader sets a barrier on the grid at y = 0 for the entire line, which is a grid of all coordinates (x, 0). Of course, he also has to solve his own and internal personnel access problems, so the Bishop set n entrance A1, A2, ..., an accessible, that is, for all meshes on y = 0, only (A1, 0), (A2, 0), ..., (an, 0) can be passed, except that all grids with an ordinate of 0 cannot pass, And for a grid (x, y) with y not 0, it can be considered as random.
Now the leader wants to know that given m-point pairs (x1, y1), (x2, y2), and these points are not on the barrier, ask the shortest distance from one point to another point, each time only from one lattice to the adjacent lattice.
Input
The 1th behavior of the input is a positive integer n, which is the number of ingress on the barrier.
The 2nd line has n integers, a1, A2, ..., an, separated by a space for the horizontal axis of the N inlet.
The 3rd act is a positive integer m, which represents M queries.
Next m line, 4 integers per line x1, y1, x2, y2, with Y1 and y2 not equal to 0, represents a query from (x1, y1) to (x2, y2) the shortest path.
Output
The output contains m rows, and the first line of the input is the shortest distance from (x1, y1) to (x2, y2)
Sample input
2
2-1
2
0 1 0-1
1 1 2 2
Sample output
4
2
Tips
"Data scope and conventions"
For 20% of the data, there is n,m≤10,ai,xi,yi absolute value of not more than 100;
For 40% of the data, there is n,m≤100,ai,xi,yi absolute value of not more than 1000;
For 60% of the data, there is n,m≤1000,ai,xi,yi absolute value of not more than 100000;
For 100% of the data, there is a n,m≤100000,ai,xi,yi absolute value of not more than 100000000.
This problem also has two kinds of cases: y1,y2, direct horizontal ordinate absolute difference is the solution.
The second is not on the same side, then judge the x-axis between the two can pass the gap, if any, by mathematical relations can be obtained is also the horizontal ordinate absolute difference. If not, you can only choose a location closest to the endpoint.
How to judge, first the notch sort, and then define Len as X1,x2 midpoint, two points to find his nearest notch Pos, if x1->x2 inside the words, go straight. Otherwise, determine which endpoint it is close to, and where to bypass it. But at this point the POS value is not necessarily accurate, but very close, the real value must appear in the pos-1,pos,pos+1.
1 #defineMAXN 100010UL2 3#include <cstdio>4#include <algorithm>5 using namespacestd;6 intn,m;7 intA[MAXN];8 9 Ten One intJued (intAintb) { Aa=a-b; - if(a<0)return-A; - Else returnA; the } - - intMIN (intAintb) { - if(A<B)returnA; + Else returnb; - } + A intErfen (intg) { at intL=1, r=N; - intOsc=N; - while(l<=R) { - intMid= (L+R)/2; - if(a[mid]<g) l=mid+1; - Elser=mid-1, osc=mid; in } - returnOSC; to } + intMain () { - intX1,x2,y1,y2; thescanf"%d",&n); * for(intI=1; i<=n;i++){ $scanf"%d",&a[i]);Panax Notoginseng } -Sort (A +1, a+n+1); thescanf"%d",&m); + for(intI=1; i<=m;i++){ Ascanf"%d%d%d%d",&x1,&y1,&x2,&y2); the if((y1<0&&y2<0)|| (y1>0&&y2>0)){ +printf"%d\n", jued (y1,y2) +jued (x1,x2)); - Continue; $ } $ if(x1>x2) { - swap (X1,X2); - } the intlen= (X1+X2)/2; - intpos=Erfen (len);Wuyi if(a[pos]>=x1&&a[pos]<=x2) { theprintf"%d\n", jued (y1,y2) +jued (x1,x2)); - } Wu Else{ - intT1=jued (a[pos],x1) +jued (a[pos],x2) +jued (y1,y2), t2=0x7fffffff, t3=0x7fffffff; About if(pos>1){ $T2=jued (a[pos-1],X1) +jued (a[pos-1],X2) +jued (y1,y2); - } - if(pos<N) { -T3=jued (a[pos+1],X1) +jued (a[pos+1],X2) +jued (y1,y2); A } + if(T1>T2) t1=T2; the if(T1>T3) t1=T3; -printf"%d\n", T1); $ } the } the}
View Code
The Garden of the bishop