Cod sursa(job #3127855)

Utilizator GFA03Gavrila Florin-Alexandru GFA03 Data 7 mai 2023 21:31:51
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>

int rmq[501][501][10], v[501][501];

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

int main()
{
    auto start = std::chrono::high_resolution_clock::now();
    int n, m, s, max1, max2, temp;
    fin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            fin >> s;
            v[i][j] = s;
            rmq[i][j][0] = v[i][j];
        }
    }
    for(int length = 1; length <= (int)log2(n); ++length)
    {
        for(int i = 1; i + (1 << length)-1 <= n; ++i)
        {
            for(int j = 1; j + (1 << length)-1 <= n; ++j)
            {
                temp = 1 << (length-1);
                max1 = std::max(rmq[i][j][length-1], rmq[i][j + temp][length-1]);
                max2 = std::max(rmq[i+temp][j][length-1], rmq[i + temp][j + temp][length-1]);
                rmq[i][j][length] = std::max(max1, max2);
            }
        }
    }
    int x, y, z, k;
    for(int i = 1; i <= m; ++i)
    {
        fin >> x >> y >> z;
        k = (int)log2(z);
        temp = 1 << k;
        max1 = std::max(rmq[x][y][k], rmq[x][y + z - temp][k]);
        max2 = std::max(rmq[x + z - temp][y][k], rmq[x + z - temp][y + z - temp][k]);
        fout << std::max(max1, max2) << '\n';
    }
    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
    std::cout << duration.count() << '\n';
}