Cod sursa(job #3228210)

Utilizator iulia_morariuIulia Ela Morariu iulia_morariu Data 6 mai 2024 18:43:26
Problema Plantatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <iostream>
#include <fstream>
#include <vector>
//#include <bits/stdc++.h>

using namespace std;

ifstream fin("plantatie.in");
ofstream fout("plantatie.out");

int rmq[ 501 ][ 501 ][ 10 ];

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

}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, q; fin >> n >> q;
    int v[n][n];
    int lg = log2(n);

    //cout << "n = " << n << " q = " << q << '\n';

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            fin >> v[i][j];
            //cout << "i = " << i << " j = " << j << " v = " << v[i][j] << '\n';
        }
    }

    /*cout << "v : " << '\n';
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++) cout << v[i][j] << " ";
        cout << '\n';
    }*/

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            rmq[i][j][0] = v[i][j];
        }
    }

    int power = 1;
    for(int p = 1; power < n; p++){
        for(int i = 0; i + power < n; i++){
            for(int j = 0; j + power < n; j++){
                rmq[i][j][p] = max( rmq[i][j][p - 1], rmq[i + power][j][p - 1], rmq[i][j + power][p - 1], rmq[i + power][j + power][p - 1] );
            }
        }
        power *= 2;
    }

    for(int i = 0; i < q; i++){
        int x, y, l; fin >> x >> y >> l;
        x--, y--;
        int lmax = 1;
        int p = 0;
        while(lmax * 2 < l){ lmax *= 2; p++; }
        //cout << "lungimea max mai mica decat l = " << lmax << '\n';
        //cout << "  -- > La puterea " << p << '\n';

        int sol = max( rmq[x][y][p], rmq[x][ y + l - lmax ][p], rmq[x + l - lmax][y][p], rmq[x + l - lmax][y + l - lmax][p] );
        fout << sol << '\n';

    }

    return 0;
}