Pagini recente » Cod sursa (job #2140620) | Cod sursa (job #936229) | Cod sursa (job #2119119) | Cod sursa (job #2315663) | Cod sursa (job #1168493)
program rmq;
var a:array[1..100000] of longint;
m:array[1..500] of longint;
n,i,k,j,x,y,l,nr,min,ind:longint;
begin
assign(input,'rmq.in');
assign(output,'rmq.out');
reset(input);
rewrite(output);
readln(n,k);
for i:=1 to n do readln(a[i]);
l:=trunc(sqrt(n));
if n mod l=0 then nr:=n div l
else nr:=n div l+1;
for i:=n+1 to nr*nr do a[i]:=1 shl 30;
for i:=1 to nr do
begin
min:=1 shl 30;
for j:=(i-1)*l+1 to i*l do
if min>a[j] then min:=a[j];
m[i]:=min;
end;
while k>0 do begin
readln(x,y);
min:=1 shl 30;
i:=x;
while i mod l = 1 do
begin if a[i]<min then min:=a[i];
inc(i);
end;
ind:=i div l + 1;
while i<y do begin
if m[ind]<min then min:=m[ind];
inc(ind);
i:=i+l;
end;
for j:=i to y do
if min>a[j] then min:=a[j];
writeln(min);
dec(k);
end;
close(output);
end.