Cod sursa(job #190789)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 24 mai 2008 08:01:22
Problema Arbori de intervale Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.11 kb
var a:array[1..400100] of longint;
    f,g:text;
    maxim,i,n,m,q,w,x,poz:longint;

function max(x,y:longint):longint; begin if x<y then max:=y else max:=x; end;

procedure update(nod,st,dr:longint);
 var mij:longint;
 begin
  if (st=dr) then
   a[nod]:=x
  else begin
   mij:=(st+dr) shr 1;
   if (poz<=mij) then update(nod shl 1,st,mij)
   else update(nod shl 1+1,mij+1,dr);
   a[nod]:=max(a[nod shl 1],a[nod shl 1+1]);
  end;
 end;

procedure query(nod,st,dr:longint);
 var mij:longint;
 begin
  if (q<=st) and (dr<=w) then
   maxim:=max(maxim,a[nod])
  else begin
   mij:=(st+dr) shr 1;
   if (q<=mij) then
    query(nod shl 1,st,mij);
   if (mij<w) then
    query(nod shl 1+1,mij+1,dr);
  end;
 end;

begin
 assign(f,'arbint.in'); reset(f);
 assign(g,'arbint.out'); rewrite(g);
 read(f,n,m);
 for i:=1 to n do begin
  read(f,x);
  poz:=i;
  update(1,1,n);
 end;
 for i:=1 to m do begin
  read(f,x,q,w);
  if x=0 then begin
   maxim:=-maxlongint;
   query(1,1,n);
   writeln(g,maxim);
  end
  else begin
   poz:=q; x:=w;
   update(1,1,n);
  end;
 end;
 close(f); close(g);
end.