Cod sursa(job #3366990)

Utilizator andrei_sevescusevescu andrei andrei_sevescu Data 5 octombrie 2026 16:43:35
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <iostream>
#include <fstream>

#define N 500

int max(int a, int b, int c, int d) {
    if (a > b) std::swap(a, b);
    if (c > d) std::swap(c, d);
    if (b > d) std::swap(b, d);
    return d;
}

using namespace std;
int r[10][N+1][N+1],mat[N+1][N+1],log2[N+1];
int main() {
    ifstream fin("plantatie.in");
    ofstream fout("plantatie.out");
    int n, i, q, j, st, dr,k;
    fin>>n>>q;
    //printf("%d" ,n);

    for (i=1;i<=n;i++)
        for (j=1;j<=n;j++)
            fin>>mat[i][j];
    log2[1] = 0;
    for (i = 2; i <= n; i++) {
        log2[i] = log2[i / 2] + 1;
    }

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            r[0][i][j] = mat[i][j];
        }
    }
    for (int e=1; e <= log2[n]; e++) {
        int p = (1 << (e - 1));
        for (i = 1<<e; i <= n; i++) {
            for (j = 1<<e; j <= n; j++) {
                r[e][i][j] = max(r[e - 1][i][j], r[e - 1][i][j - p], r[e - 1][i - p][j], r[e - 1][i - p][j - p]);
            }
        }
    }
    for (i=0; i<q; i++){
        fin >> st >> dr >> k;

        st = st + k - 1;
        dr = dr + k - 1;
        int e = log2[k];
        int p = (1 << e);

        fout << max(r[e][st - k + p][dr - k + p], r[e][st - k + p][dr], r[e][st][dr - k + p], r[e][st][dr]) << '\n';
    }
    return 0;
}