Cod sursa(job #2754271)

Utilizator icnsrNicula Ionut icnsr Data 25 mai 2021 16:10:19
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.3 kb
#include <fstream>
std::ifstream fin("plantatie.in");
std::ofstream fout("plantatie.out");

#include <array>
#include <vector>
//#include <fmt/core.h>
#include <limits>

constexpr unsigned NMAX = 505;
constexpr unsigned LMAX = 15;

//using fmt::print;

int main()
{

        unsigned n, m;
        fin >> n >> m;

        std::vector<unsigned> log2(NMAX, 0);
        log2[1] = 0;
        for(unsigned i = 2; i <= n; ++i)
        {
                log2[i] = log2[i / 2] + 1;
        }

        std::vector<std::vector<std::vector<unsigned>>> rmq(
            LMAX, std::vector<std::vector<unsigned>>(NMAX, std::vector<unsigned>(NMAX, 0)));

        for(unsigned i = 1; i <= n; ++i)
        {
                for(unsigned j = 1; j <= n; ++j)
                {
                        fin >> rmq[0][i][j];
                }
        }

        for(unsigned plength = 1; (1u << plength) <= n; ++plength)
        {
                for(unsigned i = 1; i + (1u << plength) - 1 <= n; ++i)
                {
                        for(unsigned j = 1; j + (1u << plength) - 1 <= n; ++j)
                        {
                                const unsigned len = 1 << (plength - 1);

                                const unsigned c1 = rmq[plength - 1][i][j];
                                const unsigned c2 = rmq[plength - 1][i][j + len];
                                const unsigned c3 = rmq[plength - 1][i + len][j];
                                const unsigned c4 = rmq[plength - 1][i + len][j + len];

                                rmq[plength][i][j] =
                                    std::max(std::max(c1, c2), std::max(c3, c4));
                        }
                }
        }

        for(unsigned tmp = 0; tmp < m; ++tmp)
        {
                unsigned i, j, len;
                fin >> i >> j >> len;
                // print(stderr, "{},{},{}\n", i, j, len);

                const unsigned plength = log2[len];
                const unsigned offset = len - (1 << plength);

                const unsigned c1 = rmq[plength][i][j];
                const unsigned c2 = rmq[plength][i][j + offset];
                const unsigned c3 = rmq[plength][i + offset][j];
                const unsigned c4 = rmq[plength][i + offset][j + offset];

                fout << std::max(std::max(c1, c2), std::max(c3, c4)) << '\n';
        }
}