Pagini recente » Cod sursa (job #2555359) | Cod sursa (job #435697) | Cod sursa (job #382913) | Cod sursa (job #1154074) | Cod sursa (job #257168)
Cod sursa(job #257168)
type longin=0..250000000000;
var a,b,c,s:array[1..250100] of longin;
v:array[1..250100] of longint;
j,m1,m2,l1,l2,i,n,m,x,y:longint;
cost,rez,poz:longin;
function cb(l,r:longint):longint;
begin
if v[l]>=s[r]-s[l] then cb:=l
else if (v[r]>=s[r-1]-s[l-1]) then cb:=r
else
begin
while l<=r do
begin
m:=(l+r) div 2;
if (s[m]-s[x-1]<=s[y]-s[m])and (s[m+1]-s[x-1]>s[y]-s[m+1]) then begin cb:=m; l:=r+1; end
else if s[m]-s[x-1]<=s[y]-s[m] then l:=m
else r:=m;
end;
end;
end;
begin
assign(input,'cuburi2.in'); reset(input);
assign(output,'cuburi2.out'); rewrite(output);
readln(n,m);
for i:=1 to n do read(v[i]);
s[1] :=v[1];
for i:=2 to n do s[i]:=s[i-1]+v[i];
b[1]:=v[1];
for i:=2 to n do b[i]:=b[i-1]+s[i-1]+v[i];
a[n]:=v[n];
for i:=n-1 downto 1 do a[i]:=a[i+1]+s[n]-s[i]+v[i];
c[1]:=a[2]; c[n]:=b[n-1];
for i:=2 to n-1 do c[i]:=a[i+1]+b[i-1];
for j:=1 to m do
begin
readln(x,y);
poz:=cb(x,y);
l1:=poz-7;
if l1<x then l1:=x;
l2:=poz+7;
if l2>y then l2:=y;
cost:=250000000000;
for i:=l1 to l2 do
begin
m1:=i-x; m2:=y-i; rez:=c[i];
rez:=rez-a[y+1]-b[x-1];
rez:=rez-m1*s[x-1];
rez:=rez-m2*(s[n]-s[y]);
if rez<=cost then
begin
cost:=rez; poz:=i;
end;
end;
writeln(poz,' ',cost);
end;
close(output);
close(input);
end.