Pagini recente » Cod sursa (job #2969381) | Cod sursa (job #352167) | Cod sursa (job #2799951) | Cod sursa (job #1672280) | Cod sursa (job #2862592)
#include <fstream>
using namespace std;
int max4( int a, int b, int c, int d){
return max( max (a, b) , max (c, d) );
}
const int NMAX = 501;
const int LOG = 10;
int bilog[NMAX];
int mat[LOG][NMAX][NMAX];
int main()
{
ifstream in("plantatie.in");
ofstream out("plantatie.out");
int n, m;
in >> n >> m;
bilog[0] = 1;
for(int i = 2; i <= n; i++){
bilog[i] = 1 + bilog[i / 2];
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
in >> mat[0][i][j];
}
}
for(int l = 1; l <= LOG; l++){
for(int i = 1; i + (1 << l) - 1 <= n; i++){
for(int j = 1; j + (1 << l) - 1 <= n; j++){
int lat = (1 << (l - 1));
mat[l][i][j] = max4(mat[l - 1][i][j], mat[l - 1][i][j + lat], mat[l - 1][i + lat][j], mat[l - 1][i + lat][j + lat]);
//out << mat[l][i][j] << " ";
}
//out << "\n";
}
//out << "\n";
}
int l, c, k;
for(int i = 0; i < m; i++){
in >> l >> c >> k;
int p = bilog[k];
int lat = k - (1 << p);
out << max4(mat[p][l][c], mat[p][l + lat][c], mat[p][l][c + lat], mat[p][l + lat][c + lat]);
out << '\n';
}
return 0;
}