Cod sursa(job #1408813)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 30 martie 2015 11:39:02
Problema Arbori de intervale Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.66 kb
program arbint;
var     n,i,j,k,m,x,a,b,goi,gof,poz,max:longint;
        arb:array[1..300001] of longint;

function  maxim(x,y:longint):longint;
begin
  if x>y then maxim:=x
  else maxim:=y;
end;

procedure update(ni,st,dr:longint);
var       mij:longint;
begin
  if st=dr then arb[ni]:=x
  else
    begin
      mij:=(st+dr) div 2;
      if poz<=mij then update(2*ni,st,mij)
      else update(2*ni+1,mij+1,dr);
      //vad care din cei 2 fii e mai mare
      arb[ni]:=maxim(arb[2*ni],arb[2*ni+1])
    end;
end;

procedure caut(ni,st,dr:longint);
var       mij:longint;
begin
  //verific daca am tot intervalu curent inclus in ce-mi trebe
  if (goi<=st) and (dr<=gof) then
    begin
      if max<arb[ni] then max:=arb[ni];//daca max este mai mic decat
    end
  else
    begin
      mij:=(st+dr) div 2;
      //daca am de ce merge in stanga ma duc
      if (goi<=mij) then caut(2*ni,st,mij);
      if (mij<gof) then caut(2*ni+1,mij+1,dr);
    end;
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); //pun pe pozitia poz, din arborele de intervale, x-ul
    end;
  for i:=1 to m do
    begin
      readln(x,a,b);
      if x=0 then
        begin
          max:=0;
          goi:=a;
          gof:=b;
          caut(1,1,n);//caut maximul din intervalul [a,b]=[goi,gof]
          writeln(max);
        end
      else
        begin
          poz:=a;
          x:=b;
          update(1,1,n);//actualizez(pun) pe pozitia poz x-ul
        end;
    end;
  close(input); close(output);
end.