Pagini recente » Cod sursa (job #1528335) | Cod sursa (job #409567) | Cod sursa (job #3219192) | Cod sursa (job #843766) | Cod sursa (job #372689)
Cod sursa(job #372689)
var i,j,n,m,l,r,log:longint;
p:array[0..100000,0..18] of longint; {17}
bufin:array[1..65000] of byte;
begin
assign(input,'rmq.in');
reset(input);
settextbuf(input,bufin);
assign(output,'rmq.out');
rewrite(output);
readln(n,m);
for i:=1 to n do readln(p[i,0]);
j:=n;
while j>1 do begin
j:=j div 2;
inc(log);
end;
for j:=1 to log do
for i:=1 to n-(1 shl j)+1 do begin
if p[i,j-1]<p[i+(1 shl (j-1)),j-1] then
p[i,j]:=p[i,j-1]
else
p[i,j]:=p[i+(1 shl (j-1)),j-1];
end;
for i:=1 to m do begin
readln(l,r);
log:=0;
j:=r-l;
while j>1 do begin
j:=j div 2;
inc(log);
end;
if p[l,log]<p[r-(1 shl log)+1,log] then
writeln(p[l,log])
else writeln(p[r-(1 shl log)+1,log]);
end;
close(input);
close(output);
end.