Cod sursa(job #19578)

Utilizator VmanDuta Vlad Vman Data 19 februarie 2007 19:30:55
Problema Plantatie Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.53 kb
program plantatie;
var n:integer;
    m,i,j,x,y,z:longint;
    a:array[1..500,1..500,0..9]of longint;
    {a[i][j][k]=maximul din patratul cu coltu i,j si latura 2^k}
    p:array[0..10]of longint;
    f,g:text;

function getmax(x,y,z:longint):longint;
var k:integer;
    max:longint;
begin
k:=0;
while p[k]<z do inc(k);
dec(k);
max:=a[x][y][k];
if a[x+z-p[k]][y][k]>max then max:=a[x+z-p[k]][y][k];
if a[x][y+z-p[k]][k]>max then max:=a[x][y+z-p[k]][k];
if a[x+z-p[k]][y+z-p[k]][k]>max then max:=a[x+z-p[k]][y+z-p[k]][k];
getmax:=max;
end;

procedure constr; {construiesc arbore de intervale}
var i,j,k:integer;
begin
p[0]:=1;
for i:=1 to 10 do
    p[i]:=p[i-1] shl 1;
k:=1;
while p[k]<=n do begin
      for i:=1 to n do
          if i+p[k]-1<=n then
             for j:=1 to n do
                 if j+p[k]-1<=n then
                  begin
                  a[i][j][k]:=a[i][j][k-1];
                  if a[i+p[k-1]][j][k-1]>a[i][j][k] then a[i][j][k]:=a[i+p[k-1]][j][k-1];
                  if a[i][j+p[k-1]][k-1]>a[i][j][k] then a[i][j][k]:=a[i][j+p[k-1]][k-1];
                  if a[i+p[k-1]][j+p[k-1]][k-1]>a[i][j][k] then a[i][j][k]:=a[i+p[k-1]][j+p[k-1]][k-1];
                  end;
inc(k);
end;
end;

begin
assign(f,'plantatie.in');reset(f);
assign(g,'plantatie.out');rewrite(g);
readln(f,n,m);
for i:=1 to n do begin
    for j:=1 to n do
        read(f,a[i][j][0]);
    readln(f);
end;
constr;
for i:=1 to m do begin
    readln(f,x,y,z);
    writeln(g,getmax(x,y,z));
end;
close(f);
close(g);
end.