Cod sursa(job #600218)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 30 iunie 2011 20:38:25
Problema Range minimum query Scor 70
Compilator fpc Status done
Runda Arhiva educationala Marime 1.6 kb
const   fin = 'rmq.in'; fout = 'rmq.out'; inf = 1000 * 1000 + 1;

type    buffer = array[0..1 shl 17] of char;
        arbore = array[0..1 shl 18] of longword;

var     bufin, bufout : buffer;
        arb : arbore;
        n ,r ,x ,y : longword;

function min( a, b : longword ) : longword;
begin
        if (a < b) then min := a else min := b;
end;

procedure construct( nod, st, dr : longword );
var     mij : longword;
begin
        if (st = dr) then read( arb[nod] ) else
        begin
                mij := (st + dr) div 2;
                construct( nod * 2, st, mij );
                construct( nod * 2 + 1, mij + 1, dr );
                arb[nod] := min( arb[nod * 2], arb[nod * 2 + 1] );
        end;
end;

procedure querry( nod, st, dr : longword );
var     mij : longword;
begin
        if (x <= st) and (dr <= y) then
        begin
                if (arb[nod] < r) then r := arb[nod];
                exit();
        end;
        mij := (st + dr) div 2;
        if (x <= mij) then querry( nod * 2, st, mij );
        if (mij < y) then querry( nod * 2 + 1, mij + 1, dr);
end;

procedure main();
var     m ,i : longword;
begin
        assign( input, fin ); reset( input );
        assign( output, fout ); rewrite( output );
        settextbuf( input, bufin );
        settextbuf( output, bufout );
        readln( n, m );

        construct( 1, 1, n );

        for i := 1 to m do
        begin
                read( x, y );
                r := inf;
                querry( 1, 1, n );
                write( r, #10 );
        end;
end;

begin
        main();
end.