Pagini recente » Cod sursa (job #575686) | Cod sursa (job #544479) | Cod sursa (job #1876122) | Cod sursa (job #264061) | Cod sursa (job #468881)
Cod sursa(job #468881)
var a:array[0..20,1..100000]of longint;
log:array[0..100000]of shortint;
x,y,n,m,i,t,j:longint;
buf:array[1..65000]of byte;
function min(a,b:longint):longint;
begin
if a<b then min:=a
else min:=b;
end;
begin
assign(input,'rmq.in'); reset(input);
settextbuf(input,buf);
assign(output,'rmq.out'); rewrite(output);
read(n,m); log[0]:=-1;
for i:=1 to n do begin
read(x);
log[i]:=log[i shr 1]+1;
a[0,i]:=x;
end;
t:=1;
for i:=1 to log[n] do begin
t:=t shl 1;
for j:=1 to n-t+1 do a[i,j]:=min(a[i-1,j],a[i-1,j+(t shr 1)]);
end;
{
t:=1;
for i:=0 to log[n] do begin
t:=t shl 1;
for j:=1 to n-t+1 do write(a[i,j],' ');
writeln;
end;
}
for i:=1 to m do begin
read(x,y);
j:=log[y-x+1];
t:=(1 shl j)-1;
writeln(min(a[j,x],a[j,y-t]));
end;
close(input); close(output);
end.