Cod sursa(job #3161528)

Utilizator Vlad_NistorNIstor Vlad Vlad_Nistor Data 27 octombrie 2023 13:27:21
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h>
using namespace std;
#define NMAX 105 * 5
#define MAXLOG 10


int n,Q;

int rmq[MAXLOG][NMAX][NMAX], lg[NMAX];

void __init__(){
    for(int logs = 1, lat = 2;lat<=n;lat *= 2, logs++){
        for(int i = 1;i<=n - lat + 1;i++){
            for(int j =1;j <= n - lat + 1;j++){
                int newi = i + (lat / 2);
                int newj = j + (lat / 2);
                rmq[logs][i][j] = max( max( rmq[logs - 1][i][j], rmq[logs - 1][newi][j] ), max( rmq[logs - 1][i][newj], rmq[logs - 1][newi][newj] ) );
            }
        }
    }
    for(int i = 2;i<=n;i++){
        lg[i] = lg[i / 2] + 1;
    }
}
int ansQuery(int i, int j, int lat){
    int e = lg[lat];
    int newi = i + lat - (1 << e);
    int newj = j + lat - (1 << e);
    return  max( max( rmq[e][i][j], rmq[e][newi][j] ), max( rmq[e][i][newj], rmq[e][newi][newj] ) );
}


int main(void){
    ofstream cout("plantatie.out");
    ifstream cin("plantatie.in");


    cin >> n >> Q;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cin >> rmq[0][i][j];
        }
    }
    __init__();
    while(Q--){
        int i,j, len;
        cin >> i >> j >> len;
        cout << ansQuery(i,j,len) << '\n';
    }



}