Pagini recente » Cod sursa (job #2721390) | Cod sursa (job #230929) | Cod sursa (job #1284590) | Cod sursa (job #2113219) | Cod sursa (job #458216)
Cod sursa(job #458216)
program p1;
type index=record lin:shortint;col:longint;end;
var f,g:text;
a:array[1..100000] of index;
i,n,c,s,xi,yi,xf,yf,m,aux:longint;
procedure part(st,dr:longint;var m:longint);
var p,aux:index;
s,d:longint;
begin
p:=a[st];s:=st;d:=dr;
while s<d do
begin
while(s<=dr)and(a[s].col<=p.col) do s:=s+1;
while a[d].col>p.col do d:=d-1;
if s<d then begin aux:=a[s];a[s]:=a[d];a[d]:=aux;end;
end;
a[st]:=a[d];
a[d]:=p;
m:=d;
end;
procedure quick(st,dr:longint);
var m:longint;
begin
part(st,dr,m);
if st<m-1 then quick(st,m-1);
if dr>m+1 then quick(m+1,dr);
end;
function cauta(x:longint):longint;
var pz,st,dr,mij:longint;
begin
pz:=n;st:=1;dr:=n;
while st<=dr do
begin
mij:=(st+dr)div 2;
if a[mij].col<yi then st:=mij+1
else begin dr:=mij-1;pz:=mij;end;
end;
cauta:=pz;
end;
begin
assign(f,'gropi.in');reset(f);
assign(g,'gropi.out');rewrite(g);
read(f,c,n);
for i:=1 to n do read(f,a[i].lin,a[i].col);
quick(1,n);
a[n+1].col:=maxlongint;
read(f,m);
for i:=1 to m do
begin
read(f,xi,yi,xf,yf);
if yf<yi then
begin
aux:=yf;
yf:=yi;
yi:=aux;
aux:=xf;
xf:=xi;
xi:=aux;
end;
c:=cauta(yi);
s:=1;
while yi<yf do
begin
if a[c].col<yf then
begin
s:=s+a[c].col-yi;
yi:=a[c].col;
if xi=a[c].lin then begin s:=s+1;xi:=3-xi;end;
end
else
begin
s:=s+yf-yi;
if xi<>xf then s:=s+1;
yi:=yf;
end;
c:=c+1;
end;
writeln(g,s);
end;
close(f);
close(g);
end.