Pagini recente » Cod sursa (job #3292880) | Cod sursa (job #1585859) | Clasament preONI 2007, Runda 3, Clasa a 10-a | Cod sursa (job #168959) | Cod sursa (job #166129)
Cod sursa(job #166129)
program rmq;
const
fin='rmq.in';
fout='rmq.out';
nmax=100002;
type
intarray=array[1..nmax] of longint;
pintarray=^intarray;
var
a:array[0..nmax] of longint;
i,j,l,maxlog,n,m,x,y:longint;
min:array[0..18] of pintarray;
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:=0 to maxlog do
new(min[i]);
for i:=1 to n do
min[0]^[i]:=a[i];
for j:=1 to maxlog do
for i:=1 to n-(1 shl j)+1 do
if min[j-1]^[i]<min[j-1]^[i+(1 shl (j-1))] then
min[j]^[i]:=min[j-1]^[i]
else
min[j]^[i]:=min[j-1]^[i+(1 shl (j-1))];
for i:=1 to m do
begin
readln(x,y);
l:=trunc(ln(y-x+1)/ln(2));
writeln(minim(min[l]^[x],min[l]^[y-(1 shl l)+1]));
end;
close(output);
close(input);
end.