Cod sursa(job #561892)

Utilizator andrei31Andrei Datcu andrei31 Data 21 martie 2011 22:06:45
Problema Arbori de intervale Scor 40
Compilator fpc Status done
Runda Arhiva educationala Marime 1.11 kb
var t:array[1..131073] of longword;

 function max(a,b:longword):longword;
 begin
 if a>b then max:=a else max:=b;
 end;

 procedure update(nod:longword;l,r,p,v:longword);
 var m:longword;
 begin
 m:=(l+r) div 2;
 if l<r then
  if p<=m then
  update(2*nod,l,m,p,v)
   else
    update(2*nod+1,m+1,r,p,v);
 if l=r then
   t[nod]:=v else t[nod]:=max(t[2*nod],t[2*nod+1]);
 end;

 function query(nod:longword;l,r,a,b:longword):longword;
 var m,tt:longword;
 begin
 tt:=0;
 if (a<=l) and (r<=b) then tt:=t[nod]
  else
   begin
   m:=(l+r) div 2;
   if a<=m then tt:=max(tt,query(2*nod,l,m,a,b));
   if m<b then tt:=max(tt,query(2*nod+1,m+1,r,a,b));
   end;
 query:=tt;
 end;

 procedure citire;
 var n,m,i:longword;
     x,y,z:longword;
 begin
 assign(input,'arbint.in');reset(input);
 readln(n,m);
 for i:=1 to n do
  begin
  read(x);
  update(1,1,n,i,x);
  end;
 assign(output,'arbint.out');rewrite(output);
 for i:=1 to m do
  begin
  readln(x,y,z);
  if x=0 then writeln(query(1,1,n,y,z))
   else update(1,1,n,y,z);
  end;
 close(input);close(output);
 end;

begin
citire;
end.