Pagini recente » Cod sursa (job #708935) | Cod sursa (job #363088) | Cod sursa (job #1489222) | Cod sursa (job #1781238) | Cod sursa (job #332646)
Cod sursa(job #332646)
# include <algorithm>
using namespace std;
# define DIM 501
# define LOG 10
int n, m, sol[ DIM ][ DIM ][ LOG ];
inline int max ( int w, int x, int y, int z ) {
if ( w > x && w > y && w > z )
return w;
if ( x > w && x > y && x > z )
return x;
if ( y > w && y > x && y > z )
return y;
return z;
}
int calc ( int val ) {
int p;
for ( p = 0; 1 << ( p + 1 ) <= val; ++ p );
return p;
}
void calc_sol () {
int i, j, k;
for ( k = 1; 1 << k <= n; ++ k )
for ( i = 1; i + ( 1 << k ) - 1 <= n; ++ i )
for ( j = 1; j + ( 1 << k ) - 1 <= n; ++ j )
sol[ i ][ j ][ k ] = max ( sol[ i ][ j ][ k - 1 ], sol[ i ][ j + ( 1 << ( k - 1 ) ) ][ k - 1 ], sol[ i + ( 1 << ( k - 1 ) ) ][ j ][ k - 1 ], sol[ i + ( 1 << ( k - 1 ) ) ][ j + ( 1 << ( k - 1 ) ) ][ k - 1 ] );
}
void solve () {
int i, j, l, p, x, y;
scanf ( "%d%d", &n, &m );
for ( i = 1; i <= n; ++ i )
for ( j = 1; j <= n; ++ j )
scanf ( "%d", &sol[ i ][ j ][ 0 ] );
calc_sol ();
for ( i = 0; i < m; ++ i ) {
scanf ( "%d%d%d", &x, &y, &l );
p = calc ( l );
if ( 1 << p == l )
printf ( "%d\n", sol[ x ][ y ][ p ] );
else
printf ( "%d\n", max ( sol[ x ][ y ][ p ], sol[ x ][ y + l - ( 1 << p ) ][ p ], sol[ x + l - ( 1 << p ) ][ y ][ p ], sol[ x + l - ( 1 << p ) ][ y + l - ( 1 << p ) ][ p ] ) );
}
}
int main () {
freopen ( "plantatie.in", "r", stdin );
freopen ( "plantatie.out", "w", stdout );
solve ();
return 0;
}