Cod sursa(job #52628)

Utilizator ScrazyRobert Szasz Scrazy Data 19 aprilie 2007 15:49:32
Problema Plantatie Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.69 kb
const MAX_N=510;
      MAX_M=75010;
var a:array[1..MAX_N,1..MAX_N] of longint;
    c:array[1..MAX_N,1..MAX_N,0..10] of longint;
    n:word;
    m,i,j,k,hatv,z,e,sz,h:longint;
    fin,fout:text;

function gyors(a,k:longint):longint;
var x:longint;
begin
  if k=1 then gyors:=a
  else begin
    x:=gyors(a,k div 2);
    if odd(k) then
      gyors:=x*x*a
    else
      gyors:=x*x;
  end;
end;

function max(a,b,c,d:longint):longint;
begin
  if a>b then
    if a>c then
      if a>d then max:=a
      else max:=d
    else
    if c>d then max:=c
    else max:=d
  else
    if b>c then
      if b>d then max:=b
      else max:=d
    else
      if c>d then max:=c
      else max:=d;
end;

begin
  assign(fin,'plantatie.in');reset(fin);
  readln(fin,n,m);
  for i:=1 to n do begin
    for j:=1 to n do read(fin,a[i,j]);
    readln(fin);
  end;

  for i:=1 to n do
    for j:=1 to n do c[i,j,0]:=a[i,j];

  hatv:=1;
  k:=0;

  repeat
    k:=k+1;
    hatv:=hatv*2;
    i:=1;
    while i+hatv-1<=n do begin
      j:=1;
      while j+hatv-1<=n do begin
        c[i,j,k]:=max(c[i,j,k-1],
                      c[i+hatv div 2,j,k-1],
                      c[i,j+hatv div 2,k-1],
                      c[i+hatv div 2,j+hatv div 2,k-1]);
       j:=j+1;
      end;
      i:=i+1;
    end;

  until hatv>n;

  assign(fout,'plantatie.out');rewrite(fout);
  for z:=1 to m do begin
    readln(fin,i,j,k);
    sz:=k;
    h:=0;
    while sz div 2>=1 do begin
      sz:=sz div 2;
      h:=h+1;
    end;
    hatv:=gyors(2,h);
    e:=max(c[i,j,h],c[i,j+k-hatv,h],c[i+k-hatv,j,h],c[i+k-hatv,j+k-hatv,h]);
    writeln(fout,e);
  end;
  close(fout);
  close(fin);
end.