Cod sursa(job #144385)

Utilizator CezarMocanCezar Mocan CezarMocan Data 27 februarie 2008 15:52:45
Problema Arbori de intervale Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.55 kb
var arb,v:array[1..200000] of longint;
    n,m,i,j,x,a,b,poz,max:longint;

function mx(a,b:longint):longint;
begin
if a>b then
        mx:=a
else
        mx:=b;
end;

procedure update(nod,l,r:longint);
var m:longint;
begin
//fac un fel de cautare binara ca sa gasesc unde e pozitia X
m:=(l+r) div 2;
if (l=poz)and(r=poz) then
        begin
        arb[nod]:=x;
        exit;
        end;
if poz<=m then
        update(nod*2,l,m)
else
        update(nod*2+1,m+1,r);
//vad care din cei 2 fii e mai mare
arb[nod]:=mx(arb[2*nod],arb[2*nod+1]);
end;

procedure query(nod,l,r:longint);
var m:longint;
begin
//verific daca am tot intervalu curent inclus in ce-mi trebe
if (l>=a)and(r<=b) then
        begin
        if arb[nod]>max then
                max:=arb[nod];
        exit;
        end;
//daca am de ce merge in stanga ma duc
m:=(l+r) div 2;
if a<=m then
        query(2*nod,l,m);
if b>m then
        query(2*nod+1,m+1,r);
end;

begin
assign(input,'arbint.in');reset(input);
assign(output,'arbint.out');rewrite(output);
readln(n,m);
for i:=1 to n do
        begin
        read(x);
        poz:=i;
        update(1,1,n);
        end;
for i:=1 to m do
        begin
        readln(x,a,b);
        if x=0 then
                begin
                max:=-1;
                query(1,1,n);
                writeln(max);
                end
        else
                begin
                x:=b;
                poz:=a;
                update(1,1,n);
                end;
        end;
close(input);close(output);
end.