Cod sursa(job #2480582)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 25 octombrie 2019 20:14:43
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <cstdio>

using namespace std;

const int N = 500;

int MAX(int a, int b, int c, int d){
    return max(a, max(b, max(c, d)));
}

int rmq[10][5 + N][5 + N];
int log[5 + N];

int main()
{
    freopen("plantatie.in", "r", stdin);
    freopen("plantatie.out", "w", stdout);
    int n, m, i, j;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            cin >> rmq[0][i][j];
        }
    }
    log[1] = 0;
    for(i = 2; i <= n; i++) log[i] = log[i >> 1] + 1;

    for(int x = 1; x <= log[n]; x++){

        for(i = 1; i + (1 << x) <= n + 1; i++){
            for(j = 1; j + (1 << x) <= n + 1; j++){
                rmq[x][i][j] = MAX(rmq[x-1][i][j], rmq[x-1][i + (1 << (x - 1))][j], rmq[x-1][i][j + (1 << (x - 1))], rmq[x-1][i + (1 << (x - 1))][j + (1 << (x - 1))]);
            }
        }
    }

    for(i = 1; i <= m; i++){
        int p, q, k, x;
        int p0, q0;
        cin >> p >> q >> k;
        x = log[k];
        p0 = p + k - 1;
        q0 = q + k - 1;
        printf("%d\n", MAX(rmq[x][p][q], rmq[x][p][q0 - (1 << x) + 1], rmq[x][p0 - (1 << x) + 1][q], rmq[x][p0 - (1 << x) + 1][q0 - (1 << x) + 1]));
    }
    return 0;
}