Pagini recente » Cod sursa (job #496665) | Cod sursa (job #2756827) | Profil flaviulupu | Cod sursa (job #1308843) | Cod sursa (job #1610059)
#include <fstream>
using namespace std;
int plantation[501][501][10];
int n, log[501];
void preCalcLog2()
{
for (int j = 0; j < 9; ++j)
for (int i = 1 << j, iMAX = 1 << (j+1); i < iMAX; ++i)
log[i] = j;
}
void calcPlantation()
{
int*ptr;
int powk = 1 << (k-1);
for (int k = 1; k <= log[n]; ++k)
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
plantation[i][j][k] = plantation[i][j][k-1];
ptr = &plantation[i][j + powk][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
ptr = &plantation[i + powk][j][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
ptr = &plantation[i + powk][j + powk][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
}
}
int calcMax(int i, int j, int k)
{
int lgk = log[k];
int powk = 1 << lgk;
int res = plantation[i][j][lgk];
int *ptr = &plantation[i][j + k - powk][lgk];
if (res < *ptr)
res = *ptr;
ptr = &plantation[i + k - powk][j][lgk];
if (res < *ptr)
res = *ptr;
ptr = &plantation[i + k - powk][j + k - powk][lgk];
if (res < *ptr)
res = *ptr;
return res;
}
int main()
{
FILE * fin = fopen("plantatie.in", "r");
FILE * fout = fopen("plantatie.out", "w");
int m;
fscanf(fin, "%d %d", &n, &m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
{
fscanf(fin, "%d", &plantation[i][j][0]);
}
preCalcLog2();
calcPlantation();
int x, y, k;
while(m--)
{
fscanf(fin, "%d %d %h", &x, &y, &k);
fprintf(fout, "%d\n", calcMax(x-1, y-1, k));
}
fclose(fin);
fclose(fout);
return 0;
}