Cod sursa(job #2790316)

Utilizator LukyenDracea Lucian Lukyen Data 28 octombrie 2021 19:38:26
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");

struct matrix
{
    int l;
    int c;
};

int latura, nr_query;
int teren[500][500];
matrix tabel[9][500][500];
int i, j, k;

void gen_tabel();
int rasp_query(int x, int y, int lat);

int main()
{
    fin >> latura >> nr_query;
    for (i = 0; i < latura; i++)
        for (j = 0; j < latura; j++)
            fin >> teren[i][j], tabel[0][i][j].l = i, tabel[0][i][j].c = j;

    gen_tabel();

    int x, y, l;
    for (k = 0; k < nr_query; k++)
    {
        fin >> x >> y >> l;
        fout << rasp_query(x - 1, y - 1, l) << "\n";
    }
    return 0;
}

void gen_tabel()
{
    int put;
    for (put = 1; (1 << put) <= latura; put++)
    {
        for (i = 0; i + (1 << put) - 1 < latura; i++)
            for (j = 0; j + (1 << put) - 1 < latura; j++)
            {
                matrix a, b, c, d;
                a.l = tabel[put - 1][i][j].l, a.c = tabel[put - 1][i][j].c;
                b.l = tabel[put - 1][i][j + (1 << put - 1)].l, b.c = tabel[put - 1][i][j + (1 << put - 1)].c;
                c.l = tabel[put - 1][i + (1 << put - 1)][j].l, c.c = tabel[put - 1][i + (1 << put - 1)][j].c;
                d.l = tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].l, d.c = tabel[put - 1][i + (1 << put - 1)][j + (1 << put - 1)].c;
                int max = teren[a.l][a.c];
                matrix maxpoz; 
                maxpoz.l = a.l, maxpoz.c = a.c;
                if (max < teren[b.l][b.c])
                    max = teren[b.l][b.c], maxpoz.l = b.l, maxpoz.c = b.c;
                if (max < teren[c.l][c.c])
                    max = teren[c.l][c.c], maxpoz.l = c.l, maxpoz.c = c.c;
                if (max < teren[d.l][d.c])
                    max = teren[d.l][d.c], maxpoz.l = d.l, maxpoz.c = d.c;
                tabel[put][i][j].l = maxpoz.l, tabel[put][i][j].c = maxpoz.c;
            }
    }
    return;
}

int rasp_query(int x, int y, int lat)
{
    int put = log2(lat);
    int dif = lat - (1 << put);

    int a, b, c, d;
    a = teren[tabel[put][x][y].l][tabel[put][x][y].c];
    b = teren[tabel[put][x][y + dif].l][tabel[put][x][y + dif].c];
    c = teren[tabel[put][x + dif][y].l][tabel[put][x + dif][y].c];
    d = teren[tabel[put][x + dif][y + dif].l][tabel[put][x + dif][y + dif].c];

    int max = a;
    if (max < b)
        max = b;
    if (max < c)
        max = c;
    if (max < d)
        max = d;
    return max;
}