Pagini recente » Cod sursa (job #818330) | Cod sursa (job #2382236) | Cod sursa (job #2687428) | Cod sursa (job #3196968) | Cod sursa (job #460460)
Cod sursa(job #460460)
#include <stdio.h>
#define lung 500
#define lng 10
int a[lung][lung][lng],N,M;
int p2[11];
int max(int a,int b,int c,int d)
{
int m;
m=a;
if (b>m)
m=b;
if (c>m)
m=c;
if (d>m)
m=d;
return m;
}
void citire()
{
int i,j;
freopen("plantatie.in","r",stdin);
scanf("%d%d",&N,&M);
for (i=0;i<N;i++)
for (j=0;j<N;j++)
scanf("%d",&a[i][j][0]);
}
void procesare()
{
int n,i,j,k;
n=N;k=1;
while (p2[k]<=N)
{
n=N-p2[k];
for (i=0;i<=n;i++)
for (j=0;j<=n;j++)
{
a[i][j][k]= max ( a[i][j][k-1] , a[i][j+p2[k]/2][k-1] , a[i+p2[k]/2][j][k-1] , a[i+p2[k]/2][j+p2[k]/2][k-1] );
}
k++;
}
}
void scrie_raspunsuri()
{
int x,y,l,i,j;
freopen("plantatie.out","w",stdout);
for (i=0;i<M;i++)
{
scanf("%d%d%d",&x,&y,&l);x--;y--;
j=0;
while (p2[j]<=l)
j++;
j--;
printf("%d\n", max ( a[x][y][j] , a[ (x+l-1) - p2[j] +1][y][j] , a[x][ (y+l-1) - p2[j]][j]+1 , a[ (x+l-1) - p2[j]+1 ][ (y+l-1) - p2[j]+1][j]) );
}
fclose(stdin);fclose(stdout);
}
void init()
{
int i,p;
p=1;
for (i=0;i<11;i++)
{
p2[i]=p;
p*=2;
}
}
int main()
{
init();
citire();
procesare();
scrie_raspunsuri();
return 0;
}