Cod sursa(job #2968386)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 20 ianuarie 2023 22:57:40
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
#define PII pair < int , int >
#define NMAX 505
#define LOG 9

using namespace std;

int n, m;
int R[NMAX][NMAX][LOG], L[NMAX];

int solve(int x, int y, int z) {
    int k = L[z];
    int xx = x + z - 1;
    int yy = y + z - 1;

    return max({
        R[x][y][k],
        R[x][yy - (1 << k) + 1][k],
        R[xx - (1 << k) + 1][y][k],
        R[xx - (1 << k) + 1][yy - (1 << k) + 1][k]
    });
}

int main(){
    ifstream cin("plantatie.in");
    ofstream cout("plantatie.out");
    
    cin >> n >> m;
    for (int i = 2; i <= n; i++) {
        L[i] = L[i / 2] + 1;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1, x; j <= n; j++) {
            cin >> x;
            R[i][j][0] = x;
        }
    }

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

    while (m--) {
        int x, y, z;
        cin >> x >> y >> z;
        cout << solve(x, y, z) << "\n";
    }

    return 0;
}