Cod sursa(job #781748)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 24 august 2012 23:39:04
Problema Range minimum query Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.65 kb
uses math;
var f,g:text;
    a:array[0..20,1..100000] of integer;
    bufin,bufout:array[1..65000] of byte;
    n,m,i,x,y,j,b,aux:longint;
    x1:integer;

begin
 assign (f,'rmq.in'); reset (f);
 assign (g,'rmq.out'); rewrite (g);
 settextbuf (f,bufin);
 settextbuf (g,bufout);
 read (f,n,m);
 for i:=1 to n do
 begin
  readln (f,x1);
  a[0,i]:=x1;
 end;
 for i:=1 to trunc (log2(n)) do
  for j:=1 to (n-(1 shl (i))+1) do
   a[i,j]:=min (a[i-1,j],a[i-1,j+1 shl (i-1)]);
  for i:=1 to  m do
  begin
   read (F,x,y);
   b:=trunc (log2(y-x+1));
   aux:=y-x+1-(1 shl b);
   writeln (G,min(a[b,x],a[b,x+aux]));
  end;
  close (f); close (g);
end.