Pagini recente » Cod sursa (job #2024718) | Cod sursa (job #2753794) | Cod sursa (job #1021172) | Cod sursa (job #530141) | Cod sursa (job #1410488)
program RMQcuarb;
var f,g:text;
n,m,x,poz,a,b,min,i:longint;
arb:array[1..300001] of longint;
//----------------------------------
function minim(a, b:longint):longint;
begin
if a > b then minim := b
else minim := a;
end;
//---------------------------------
procedure update(nod, st, dr:longint);
var mij:longint;
begin
if st = dr then arb[nod] := x
else
begin
mij := (st + dr) div 2;
if poz <= mij then update(2*nod, st, mij)
else update(2*nod + 1, mij + 1, dr);
arb[nod] := minim(arb[2*nod], arb[2*nod + 1]);
end;
end;
//--------------------------------
procedure search(nod, st, dr:longint);
var mij:longint;
begin
if (a <= st) and (dr <= b) then
begin
if min > arb[nod] then min := arb[nod];
end
else
begin
mij := (st + dr) div 2;
if (a <= mij) then search(2*nod, st, mij);
if (mij < b) then search(2*nod+1, mij+1, dr);
end;
end;
//---------------------------------
begin
assign(f,'rmq.in'); reset(f);
assign(g,'rmq.out'); rewrite(g);
readln(f,n,m);
for i := 1 to n do
begin
readln(f, x);
poz := i;
update(1,1,n);
end;
for i := 1 to m do
begin
readln(f,a,b);
min := maxlongint;
search(1,1,n);
writeln(g,min);
end;
close(f); close(g);
end.