Cod sursa(job #21514)

Utilizator adi_nmAdrian Negreanu adi_nm Data 23 februarie 2007 19:26:13
Problema Plantatie Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.97 kb
var p:array[1..1024,1..1024,0..9]of longint;
    n,m,i,j,k,t,p2,f,x1,x2:longint;
    r:longint;
begin
  assign(input,'plantatie.in'); reset(input);
  assign(output,'plantatie.out'); rewrite(output);
  readln(n,m);
  for i:=1 to n do begin
    for j:=1 to n do read(p[i,j,0]);
    readln;
  end;
  for k:=1 to 9 do begin
    f:=1 shl (k-1);
    for i:=1 to n do
      for j:=1 to n do begin
        p[i,j,k]:=p[i,j,k-1];x1:=i+f; x2:=j+f;
        if p[i,x2,k-1]>p[i,j,k] then p[i,j,k]:=p[i,x2,k-1];
        if p[x1,j,k-1]>p[i,j,k] then p[i,j,k]:=p[x1,j,k-1];
        if p[x1,x2,k-1]>p[i,j,k] then p[i,j,k]:=p[x1,x2,k-1];
      end;
  end;
  for t:=1 to m do begin
    readln(i,j,k);
    p2:=trunc(ln(k)/ln(2)); f:=1 shl p2;
        r:=p[i,j,p2];
        if p[i,j+k-f,p2]>r then r:=p[i,j+k-f,p2];
        if p[i+k-f,j,p2]>r then r:=p[i+k-f,j,p2];
        if p[i+k-f,j+k-f,p2]>r then r:=p[i+k-f,j+k-f,p2];
    writeln(r);
  end;
  close(input); close(output);
end.