Cod sursa(job #2480579)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 25 octombrie 2019 20:11:34
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 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[5 + N][5 + N][10];
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[i][j][0];
        }
    }
    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[i][j][x] = MAX(rmq[i][j][x - 1], rmq[i + (1 << (x - 1))][j][x - 1], rmq[i][j + (1 << (x - 1))][x - 1], rmq[i + (1 << (x - 1))][j + (1 << (x - 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[p][q][x], rmq[p][q0 - (1 << x) + 1][x], rmq[p0 - (1 << x) + 1][q][x], rmq[p0 - (1 << x) + 1][q0 - (1 << x) + 1][x]));
    }
    return 0;
}