Pagini recente » Cod sursa (job #321664) | Cod sursa (job #474518) | Cod sursa (job #2077830) | Cod sursa (job #1572325) | Cod sursa (job #294943)
Cod sursa(job #294943)
var a:array[0..20,1..100000]of longint;
log:array[0..100000]of shortint;
x,y,n,m,i,t,j:longint;
function min(a,b:longint):longint;
begin
if a<b then min:=a
else min:=b;
end;
begin
assign(input,'rmq.in');reset(input);
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 div 2]+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 div 2)]);
end;
for i:=0 to log[n] do begin
for j:=1 to n 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.