Cod sursa(job #2593026)

Utilizator cip_ionescuCiprian Ionescu cip_ionescu Data 2 aprilie 2020 19:03:49
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <algorithm>

#define MAX_N 500
#define MAX_L 9

using std::cin;
using std::cout;

int rmq[MAX_L][MAX_N + 1][MAX_N + 1], log[MAX_N + 1];

int main() {
    std::ifstream fin("plantatie.in");
    std::ofstream fout("plantatie.out");

    int n, m;
    fin >> n >> m;

    for (auto i = 2 ; i <= n ; i++)
        log[i] = log[i >> 1] + 1;

    for (auto i = 1 ; i <= n ; i++)
        for (auto j = 1 ; j <= n ; j++)
            fin >> rmq[0][i][j];
    
    for (auto i = 1 ; i <= log[n] ; i++) {
        const auto half = 1 << (i - 1);

        for (auto j = 1 << i ; j <= n ; j++)
            for (auto k = 1 << i ; k <= n ; k++)
                rmq[i][j][k] = std::max({
                    rmq[i - 1][j][k],
                    rmq[i - 1][j][k - half],
                    rmq[i - 1][j - half][k],
                    rmq[i - 1][j - half][k - half]
                });
    }

    for (auto i = 1 ; i <= m ; i++) {
        int x, y, k = 69;
        fin >> x >> y >> k;

        x += k - 1;
        y += k - 1;

        const auto l = log[k];
        const auto p = 1 << l;

        fout << std::max({
            rmq[l][x][y],
            rmq[l][x][y - k + p],
            rmq[l][x - k + p][y],
            rmq[l][x - k + p][y - k + p]
        }) << '\n';
    }
    return 0;
}