Cod sursa(job #372695)

Utilizator philipPhilip philip Data 11 decembrie 2009 12:43:25
Problema Range minimum query Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.81 kb
var i,j,n,m,l,r:longint;
    p:array[0..100000,0..18] of longint; {17}
    log:array[1..100000] of byte;
    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]);
  for i:=2 to n do
    log[i]:=log[i shr 1]+1;

  for j:=1 to log[n] 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);
    if p[l,log[r-l+1]]<p[r-(1 shl log[r-l+1])+1,log[r-l+1]] then
      writeln(p[l,log[r-l+1]])
        else writeln(p[r-(1 shl log[r-l+1])+1,log[r-l+1]]);
    end;
  close(input);
  close(output);
end.