Cod sursa(job #621199)

Utilizator MihaiBunBunget Mihai MihaiBun Data 16 octombrie 2011 18:46:36
Problema Arbori de intervale Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.71 kb
program kk;
var f,g:text;
    max:array[1..300000] of longword;
    bufi,bufa:array[1..1 shl 17] of char;
    n,m,i,a,b,sol,op :longword;

function maxim(s,t:longword):longword;
begin
  if s<t then maxim:=t
         else maxim:=s;
end;

procedure facarbore(p,u,nod:longword);
var mij:longword;
begin
  if p=u then read(max[nod])

         else begin
                 mij:=(u+p) shr 1;
                 facarbore(p,mij,nod shl 1);
                 facarbore(mij+1,u,nod shl 1+1);
                 max[nod]:=maxim(max[nod shl 1],max[nod shl 1+1]);
              end;
end;

procedure aflamax(p,u,nod:longword);
var mij:longword;
begin
  if (a<=p)and(b>=u)then begin
                          if sol<max[nod] then sol:=max[nod];
                          exit();
                         end;
  mij:=(p+u) shr 1;
  if a<=mij then aflamax(p,mij,nod shl 1);
  if b>=mij+1 then aflamax(mij+1,u,nod shl 1+1);
end;

procedure update(p,u,nod:longword);
var mij:longword;
begin
   if p=u then max[nod]:=b
          else begin
                 mij:=(p+u) shr 1;
                 if a<=mij then update(p,mij,nod shl 1)
                           else update(mij+1,u,nod shl 1+1);
                 max[nod]:=maxim(max[nod*2],max[nod shl 1+1]);
          end;
end;

begin
  assign(input,'arbint.in');
  assign(output,'arbint.out');
  rewrite(g);
  reset(f);
  settextbuf(input,bufi);
  settextbuf(output,bufa);
  readln(n,m);
  facarbore(1,n,1);
  for i:=1 to m do
  begin
    readln(f,op,a,b);
    if op=0 then begin
                   sol:=0;
                   aflamax(1,n,1);
                   writeln(sol);
                 end
            else update(1,n,1);
  end;
close(f);
close(g);
end.