Pagini recente » Cod sursa (job #1060005) | Cod sursa (job #710693) | Cod sursa (job #3288347) | Cod sursa (job #3279417) | Cod sursa (job #166111)
Cod sursa(job #166111)
program rmq;
const
fin='rmq.in';
fout='rmq.out';
nmax=100000;
var
a:array[1..nmax] of longint;
i,j,l,maxlog,n,m,x,y:longint;
min:array[1..nmax,0..20] of longint;
function minim(x,y:longint):longint;
begin
if x<y then
minim:=x
else
minim:=y;
end;
begin
assign(input,fin);
assign(output,fout);
rewrite(output);
reset(input);
readln(n,m);
for i:=1 to n do
readln(a[i]);
maxlog:=trunc(ln(n)/ln(2));
for i:=1 to n do
min[i,0]:=i;
for j:=1 to maxlog do
for i:=1 to n-(1 shl j)+1 do
if a[min[i,j-1]]<a[min[i+(1 shl (j-1)),j-1]] then
min[i,j]:=min[i,j-1]
else
min[i,j]:=min[i+(1 shl (j-1)),j-1];
for i:=1 to m do
begin
readln(x,y);
l:=trunc(ln(y-x+1)/ln(2));
writeln(minim(a[min[x,l]],a[min[y-(1 shl l)+1,l]]));
end;
close(output);
close(input);
end.