Cod sursa(job #579660)

Utilizator promix2012petruta andrei promix2012 Data 12 aprilie 2011 12:47:40
Problema Arbori de intervale Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.61 kb
program arbinterval;
const fi='arbint.in';
      fo='arbint.out';
var f,g:text;
n,i,y,x,max1,m,c:longint;
v:array[1..300000] of longint;
bufin,bufout:array[1..10000] of byte;
function max(a,b:longint):longint;
begin
if a>b then
  max:=a else
  max:=b;
end;
procedure initializare(nod,st,dr:Longint);
var mij:longint;
begin
if st=dr then
   read(f,v[nod]) else
begin
mij:=(st+dr)div 2;
initializare(nod*2,st,mij);
initializare(nod*2+1,mij+1,dr);
v[nod]:=max(v[2*nod],v[2*nod+1]);
end;
end;
procedure inlocuire(nod,st,dr:longint);
var mij:longint;
begin
if st=dr then
    v[nod]:=y else
      begin
    mij:=(st+dr) div 2;
    if mij>=x then
    inlocuire(nod*2,st,mij)
    else
    inlocuire(nod*2+1,mij+1,dr);
    v[nod]:=max(v[2*nod],v[2*nod+1]);
   end;
end;
procedure gaseste(nod, st, dr:longint);
var mij:longint;
  begin
  if (st>=x) and (dr<=y) then
    begin
    if v[nod]>max1 then max1:=v[nod];
    end
        else
    begin
    mij:=(st+dr) div 2;
    if (((x>=st) and (x<=mij)) or ((y>=st) and (y<=mij)) or ((x<=st) and (y>=mij))) then
  gaseste(nod*2, st, mij);
    if (((x>=mij+1) and (x<=dr)) or ((y>=mij+1) and (y<=dr)) or ((x<=mij+1) and (y>=dr))) then
    gaseste (nod*2+1, mij+1, dr);
    end;
  end;
begin
assign(f,fi);
reset(f);
assign(g,fo);
rewrite(g);
{settextbuf(f,bufin);
settextbuf(g,bufout); }
read(f,n,m);
initializare(1,1,n);
for i:=1 to m do
   begin
   read(f,c,x,y);
   if c=0 then
      begin
      max1:=0;
      gaseste(1,1,n);
      writeln(g,max1);
      end
      else
      inlocuire(1,1,n);
   end;
   close(f);
   close(g);
   end.