Cod sursa(job #35745)

Utilizator floringh06Florin Ghesu floringh06 Data 22 martie 2007 13:51:54
Problema Plantatie Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.19 kb

type   optim = array[0..513,0..513,0..11] of longword;

var fi,fo:text;
    i,j,n,k,ii,jj,p,pw:longint;
    m:longint;
    opt:optim;

  function maxim(a,b,c,d:longint):longint;
   var max:longint;
    begin
     max:=-1000;
     if a>max then max:=a;
     if b>max then max:=b;
     if c>max then max:=c;
     if d>max then max:=d;
    maxim:=max;
    end;



  procedure gosolve;
   var pw:longint;
   begin
    pw:=1;
    for k:=1 to p do
    begin
     for i:=1 to n do
      for j:=1 to n do
       if (i+pw<=n) and (j+pw<=n) then
         opt[i,j,k]:=maxim(opt[i,j,k-1],opt[i,j+pw,k-1],opt[i+pw,j,k-1],opt[i+pw,j+pw,k-1]);
     pw:=pw*2;
    end;
   end;




begin
 assign(fi,'plantatie.in'); reset(fi);
 assign(fo,'plantatie.out'); rewrite(fo);
 readln(fi,n,m);
 p:=1;
 while (1 shl p)<=n do
  inc(p);
 dec(p);
 for i:=1 to n do
  for j:=1 to n do
    read(fi,opt[i,j,0]);
 gosolve;
 for ii:=1 to m do
  begin
   readln(fi,i,j,k);
   pw:=1;
   p:=1;
   while (1 shl p)<=k do
    begin
     inc(p);
     pw:=pw*2;
    end;
   dec(p);
   writeln(fo,maxim(opt[i,j,p],opt[i,j+k-pw,p],opt[i+k-pw,j,p],opt[i+k-pw,j+k-pw,p]));
  end;
close(fo);
end.