Pagini recente » Cod sursa (job #2383194) | Cod sursa (job #3173636) | Cod sursa (job #2542678) | Cod sursa (job #46682) | Cod sursa (job #3228211)
#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];
//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;
}