Pagini recente » Cod sursa (job #1261339) | Cod sursa (job #949248) | Cod sursa (job #370426) | Cod sursa (job #128651) | Cod sursa (job #1168533)
program rmq;
var a:array[1..100000] of longint;
m:array[1..100000,0..18]of longint;
n,t,i,j,x,y,k:longint;
bufin,bufout:array[1..1 shl 16] of char;
function min(a,b:longint):longint;
begin
if a<b then min:=a else min:=b;
end;
begin
assign(input,'rmq.in');
assign(output,'rmq.out');
reset(input);
rewrite(output);
settextbuf(input,bufin);
settextbuf(output,bufout);
readln(n,t);
for i:=1 to n do
readln(a[i]);
for i:=1 to n do m[i,0]:=a[i];
j:=1;
while (1 shl j<=n) do
begin
i:=1;
while i + 1 shl j -1 <= n do
begin
m[i,j]:=min(m[i,j-1],m[i + 1 shl (j-1),j-1]);
inc(i);
end;
inc(j);
end;
for i:=1 to t do
begin
readln(x,y);
k:=1;
while 1 shl k<y-x+1 do inc(k);
dec(k);
writeln(min(m[x,k],m[y-1 shl k+1,k]));
end;
{ writeln;
for i:=1 to n do begin
for j:=0 to 4 do
write(m[i,j],' ');
writeln; end; }
close(output);
end.