Pagini recente » Cod sursa (job #2486594) | Cod sursa (job #1772221) | Cod sursa (job #1580926) | Cod sursa (job #1489942) | Cod sursa (job #131458)
Cod sursa(job #131458)
#include<stdio.h>
FILE*f=fopen("plantatie.in","r");
FILE*g=fopen("plantatie.out","w");
int a[501][501][10];
int n,m;
void read()
{
fscanf(f,"%d %d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
fscanf(f,"%d",&a[i][j][0]);
}
int max(int a, int b, int c, int d)
{
if(a>=b && a>=c && a>=d) return a;
else if(b>=a && b>=c && b>=d) return b;
else if(d>=c && d>=a && d>=b) return d;
else return c;
}
void proceseaza()
{
int i,j,k;
//pt k=0 => patratele de latura 1:
k=1;
while((1<<k)<=n)
{
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
a[i][j][k]=max(a[i][j][k-1],a[i][j+(1<<(k-1))][k-1],a[i+(1<<(k-1))][j][k-1],a[i+(1<<(k-1))][j+(1<<(k-1))][k-1]);
++k;
}
}
int main()
{
int i,j,k,sol,p;
read();
proceseaza();
while(m--)
{
fscanf(f,"%d %d %d",&i,&j,&k);
p=0;
while((1<<p)<=k) ++p;
p--;
sol=max(a[i][j][p],a[i][j+k-(1<<p)][p],a[i+k-(1<<p)][j][p],a[i+k-(1<<p)][j+k-(1<<p)][p]);
fprintf(g,"%d\n",sol);
}
return 0;
}