Pagini recente » Cod sursa (job #315086) | Cod sursa (job #503553) | Diferente pentru autumn-warmup-2007/solutii/runda-3 intre reviziile 31 si 32 | Istoria paginii utilizator/mrnyrolf | Cod sursa (job #1610053)
#include <fstream>
using namespace std;
int plantation[501][501][10];
short n, log[501];
void preCalcLog2()
{
for (short j = 0; j < 9; ++j)
for (short i = 1 << j, iMAX = 1 << (j+1); i < iMAX; ++i)
log[i] = j;
}
void calcPlantation()
{
int*ptr;
for (short k = 1; k <= log[n]; ++k)
for (short i = 0; i < n; ++i)
for (short j = 0; j < n; ++j)
{
plantation[i][j][k] = plantation[i][j][k-1];
ptr = &plantation[i][j + (1<<(k-1))][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
ptr = &plantation[i + (1<<(k-1))][j][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
ptr = &plantation[i + (1<<(k-1))][j + (1<<(k-1))][k-1];
if (plantation[i][j][k] < *ptr)
plantation[i][j][k] = *ptr;
}
}
int calcMax(short i, short j, short k)
{
short lgk = log[k];
short 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, "%hd %d", &n, &m);
for (short i = 0; i < n; ++i)
for (short j = 0; j < n; ++j)
{
fscanf(fin, "%d", &plantation[i][j][0]);
}
preCalcLog2();
calcPlantation();
short x, y, k;
while(m--)
{
fscanf(fin, "%hd %hd %hd", &x, &y, &k);
fprintf(fout, "%d\n", calcMax(x-1, y-1, k));
}
fclose(fin);
fclose(fout);
return 0;
}