Cod sursa(job #562112)

Utilizator andrei31Andrei Datcu andrei31 Data 22 martie 2011 12:43:34
Problema Arbori de intervale Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.37 kb
var op:byte;
    i,x,maxim,n,m,a,b:longword;
    max:array[1..1 shl 18]of longword;

procedure actualizare(l,r,pos:longword);
var m:longword;
begin
if x>max[pos] then max[pos]:=x;
if l<>r then
 begin
 m:=(l+r)div 2;
 if i<=m then actualizare(l,m,2*pos)
         else actualizare(m+1,r,2*pos+1);
 end;
end;

procedure interogare(l,r,pos:longword);
var m:longword;
begin
if (a<=l)and(r<=b) then begin
                        if maxim<max[pos] then maxim:=max[pos];
                        end
 else
  begin
  m:=(l+r)div 2;
  if a<=m then interogare(l,m,2*pos);
  if b>m then interogare(m+1,r,2*pos+1);
  end;
end;

procedure updatare(l,r,pos:longword);
var m:longword;
begin
if l<>r then
  begin
  m:=(l+r)div 2;
  if a<=m then updatare(l,m,2*pos)
          else updatare(m+1,r,2*pos+1);
  if max[2*pos]>max[2*pos+1] then max[pos]:=max[2*pos]
   else max[pos]:=max[2*pos+1];
  end
 else max[pos]:=b;
end;

begin
assign(input,'arbint.in');reset(input);
assign(output,'arbint.out');rewrite(output);
fillchar(max,sizeof(max),0);
read(n,m);
for i:=1 to n do
 begin
 read(x);
 actualizare(1,n,1);
 end;
for i:=1 to m do
 begin
 read(op,a,b);
 if op=0 then begin
              maxim:=0;
              interogare(1,n,1);
              writeln(maxim);
              end
         else updatare(1,n,1);
 end;
close(output);
close(input);
end.