Cod sursa(job #193246)

Utilizator valytgjiu91stancu vlad valytgjiu91 Data 3 iunie 2008 10:56:35
Problema Plantatie Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.63 kb
    Program cel;  
    Type treiD=array[1..500,1..500,0..9] of longint;  
    var f,g:text;  
        b:treiD;  
        m,a,max,p,w:longint;  
        n,i,j,k,ex:integer;  
      
   procedure comparare(var max:longint;i,j,k:integer);  
    Begin  
    If max<b[i,j,k] then max:=b[i,j,k];  
   end;  
     
  Begin  
   Assign(f,'plantatie.in');Reset(f);  
   Assign(g,'plantatie.out');Rewrite(g);  
  Readln(f,n,m);  
   k:=0;  
   p:=1;  
   While p<=n do  
   Begin  
     i:=1;  
     While i<=n-p+1 do  
      Begin  
       j:=1;  
       While j<=n-p+1 do  
       Begin  
         If k=0 then Begin  
                     Read(f,a);  
                      b[i,j,k]:=a;  
                     end  
                else Begin  
                      max:=b[i,j,k-1];  
                     comparare(max,i+p div 2,j,k-1);  
                      comparare(max,i,j+p div 2,k-1);  
                   comparare(max,i+p div 2,j+p div 2,k-1);  
                      b[i,j,k]:=max;  
                     end;  
         inc(j);  
        end;  
       If k=0 then Readln(f);  
       inc(i);  
      end;  
     inc(k);  
     p:=p*2;  
    end;  
   For w:=1 to m do  
    Begin  
     Readln(f,i,j,k);  
     p:=1;  
     ex:=0;  
     While p*2<=k do  
      Begin  
       inc(ex);  
       p:=p*2;  
      end;  
     If p-k=0 then Writeln(g,b[i,j,ex])  
              else Begin  
     max:=b[i,j,ex];  
     comparare(max,i+k-p,j,ex);  
     comparare(max,i,j+k-p,ex);  
     comparare(max,i+k-p,j+k-p,ex);  
     Writeln(g,max);  
     end;  
    end;  
   Close(f);  
   Close(g);  
  end.