Cod sursa(job #2621968)

Utilizator DiagrDiana Grigore Diagr Data 31 mai 2020 10:30:00
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, l, matrice[503][503][110];
int plantatie[503][503];
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int main() {
    f >> n >> m;
    for (int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++) {
            f >> plantatie[i][j];
            matrice[i][j][0] = plantatie[i][j];
        }
    for (int k = 1; k <= log2(n) + 1; k++)
        for(int i = 1; i <= n && i + (1 << k) <= n + 1; i++)
            for(int j = 1; j <= n && j +(1 << k) <= n + 1; j++)
            {
                matrice[i][j][k] = max(matrice[i][j][k - 1], matrice[i + (1 << (k - 1))][j + (1 << (k  - 1))][k - 1]);
                int max1 = max(matrice[i + (1 << (k - 1))][j][k - 1], matrice[i][j + (1 << (k - 1))][k - 1]);
                matrice[i][j][k] = max(matrice[i][j][k], max1);
            }
    while(m)
    {
        f >> x >> y >> l;
        int poz = log2(l);
        int r = max(matrice[x][y][poz], matrice[x + l - (1 << poz)][y][poz]);
        int final = max(matrice[x][y + l - (1 << poz)][poz], matrice[x + l - (1 << poz)][y + l - (1 << poz)][poz]);
        final = max(final, r);
        g << final << '\n';
        m--;
    }


    f.close();
    g.close();
    return 0;
}