Pagini recente » Cod sursa (job #3292388) | Cod sursa (job #3227382) | Cod sursa (job #36378) | Cod sursa (job #2599946) | Cod sursa (job #3293682)
#include <fstream>
#include <iostream>
const int NMAX=510;
const int logN=16;
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
int product_max[logN][NMAX][NMAX],a[NMAX][NMAX],exp[NMAX],n,m;
int maxim(int lin, int col, int lat)
{
int e = exp[lat];
int p = (1 << e);
return (max(max(product_max[e][lin-lat+p][col-lat+p], product_max[e][lin-lat+p][col]),
max(product_max[e][lin][col-lat+p], product_max[e][lin][col])));
}
int main()
{
fin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
fin>>a[i][j];
product_max[0][i][j]=a[i][j];
}
}
for (int i = 2; i <= n; i++)
{
exp[i] = 1 + exp[i/2];
}
for(int l=1;l<=exp[n];l++)
{
int k=(1<<l);
for(int i=k;i<=n;i++)
{
for(int j=k;j<=n;j++)
{
int p=1<<(l-1);
product_max[l][i][j]=max(max(product_max[l-1][i-p][j-p],product_max[l-1][i-p][j]),
max(product_max[l-1][i][j-p],product_max[l-1][i][j]));
}
}
}
for(int i=1;i<=m;i++)
{
int l,c,k;
fin>>l>>c>>k;
int p=1<<(exp[k]);
fout<<maxim(l+k-1,c+k-1,k)<<'\n';
}
return 0;
}