Cod sursa(job #3227245)

Utilizator dariadragomir23Dragomir Daria dariadragomir23 Data 28 aprilie 2024 18:30:45
Problema Plantatie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

int rmq[10][501][501];

int main()
{
    int n, q;
    fin >> n >> q;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            fin >> rmq[0][i][j];
    for(int p = 1; p <= 9; p++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                int lat = (1 << (p - 1));
                rmq[p][i][j] = max(
                    max( rmq[p - 1][i - lat][j], rmq[p - 1][i][j - lat]),
                    max( rmq[p - 1][i][j], rmq[p - 1][i - lat][j - lat])
                );
            }
        }
    }

    while(q--){
        int l, c, k;
        fin >> l >> c >> k;
        l += (k - 1);
        c += (k - 1);
        int p = log2(k), lat2 = (1 << p);
        fout << (
            max( rmq[p][l - k + lat2][c - k + lat2], rmq[p][l - k + lat2][c]),
            max( rmq[p][l][c - k + lat2], rmq[p][l][c])
        ) << '\n';
    }
    return 0;
}