Cod sursa(job #2851479)

Utilizator lolismekAlex Jerpelea lolismek Data 18 februarie 2022 18:29:05
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <cmath>

#define int long long

using namespace std;

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

const int N = 500, LOG = 9;
int rmq[LOG + 1][N + 1][N + 1];

signed 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 <= LOG; 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 lat2 = log2(k), lat3 = (1 << lat2);
        fout << max(
            max( rmq[lat2][l - k + lat3][c - k + lat3], rmq[lat2][l - k + lat3][c]),
            max( rmq[lat2][l][c - k + lat3], rmq[lat2][l][c])
        ) << '\n';
    }
    return 0;
}