讨论 / 二分
646740136 2011-03-23 21:14:00
点我顶贴 收藏 删除
var

a:array[0..2000000] of longint;

l,r,mid,tmp,x1,y1,x2,y2,n,m,i,j,ans:longint;

procedure qsort(l,r:longint);

var

k,i,j,mid:longint;

begin

i:=l; j:=r; mid:=a[(l+r) shr 1];

repeat

while a[i]<mid do inc(i);

while a[j]>mid do dec(j);

if i<=j then

begin

k:=a[i]; a[i]:=a[j]; a[j]:=k;

inc(i); dec(j);

end;

until i>j;

if i<r then qsort(i,r);

if l<j then qsort(l,j);

end;

begin

readln(n);

for i:=1 to n do read(a[i]);

qsort(1,n);

readln(m);

for m:=1 to m do

begin

readln(x1,y1,x2,y2);

if x1>x2 then begin tmp:=x1; x1:=x2; x2:=tmp; end;

ans:=0;

if ((y1<0) and (y2<0)) or ((y1>0) and (y2>0)) then ans:=abs(x2-x1)+abs(y2-y1)

else

begin

ans:=abs(x2-x1)+abs(y2-y1);

tmp:=maxlongint;

l:=1; r:=n;

while l<=r do

begin

mid:=(l+r) shr 1;

if a[mid]<x1 then begin if x1-a[mid]<tmp then tmp:=x1-a[mid]; l:=mid+1; end;

if a[mid]>x2 then begin if a[mid]-x2<tmp then tmp:=a[mid]-x2; r:=mid-1; end;

if (a[mid]>=x1) and (a[mid]<=x2) then begin tmp:=0; break; end;

end;

ans:=ans+tmp*2;

end;

writeln(ans);

end;

end.

查看更多回复
提交回复