Cod sursa(job #1870441)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 6 februarie 2017 17:34:25
Problema Plantatie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 500;
const int logNMax = 9;

int RMQ[logNMax][NMax][NMax];

int main() {

    ios::sync_with_stdio(false);

    int n, t;
    fin >> n >> t;

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

    for(int k = 1; k <= log2(n); k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j + (1 << k) - 1 <= n; j++) {
                RMQ[k][i][j] = max(RMQ[k - 1][i][j], RMQ[k - 1][i][j + (1 << (k - 1))]);
            }
        }
    }

    while(t--) {
        int x, y, r;
        fin >> x >> y >> r;

        int k = log2(r - 1);

        int ans = 0;
        for(int i = x; i <= x + r - 1; i++) {
            ans = max(ans, max(RMQ[k][i][y], RMQ[k][i][y + r - (1 << k)]));
        }

        fout << ans << "\n";
    }
    return 0;
}