Cod sursa(job #1610059)

Utilizator PhilipDumitruPhilip Dumitru PhilipDumitru Data 23 februarie 2016 11:24:02
Problema Plantatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#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;
}