Cod sursa(job #1921429)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 10 martie 2017 12:40:09
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 505;
const int logNMax = 9;

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

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[i][j][0];
        }
    }

    for(int k = 1; k <= log2(n); k++) {
        for(int i = 1; i + (1 << k) - 1 <= n; i++) {
            for(int j = 1; j + (1 << k) - 1 <= n; j++) {
                int a = RMQ[i][j][k - 1];
                int b = RMQ[i][j + (1 << (k - 1))][k - 1];
                int c = RMQ[i + (1 << (k - 1))][j][k - 1];
                int d = RMQ[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1];

                RMQ[i][j][k] = max(a, max(b, max(c, d)));
            }
        }
    }

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

        int k = log2(r);

        int a = RMQ[x][y][k];
        int b = RMQ[x][y + r - (1 << k)][k];
        int c = RMQ[x + r - (1 << k)][y][k];
        int d = RMQ[x + r - (1 << k)][y + r - (1 << k)][k];

        fout << max(a, max(b, max(c, d))) << "\n";
    }

    return 0;
}