Pagini recente » Cod sursa (job #1743611) | Cod sursa (job #2948341) | Cod sursa (job #1189999) | Cod sursa (job #3165705) | Cod sursa (job #1543563)
#include <fstream>
#define FOR(a,b,c) for(int a=b; a<=c; ++a)
#define max(a,b) (a>b?a:b)
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int n, m, i, j, x, y, z;
unsigned short int a[510][510][9][9];
int main(){
f>>n>>m;
FOR(i,1,n)
FOR(j,1,n)
f>>a[i][j][0][0];
z=0;
while( (1<<z++)<=n );
z-=2;
FOR(i,0,z)
{
if(i!=0)
FOR(x,1,n)
FOR(y,1,n)
a[x][y][i][0]=max(a[x][y][i-1][0], a[x+( 1<<(i-1) ) ][y][i-1][0]);
FOR(j,1,z)
FOR(x,1,n)
FOR(y,1,n)
a[x][y][i][j]=max(a[x][y][i][j-1], a[x][y+( 1<<(j-1) )][i][j-1]);
}
FOR(intrebari,1,m)
{
f>>x>>y>>z;
int i=0;
while( (1<<i++)<=z );
i-=2;
g<<max(a[x][y][i][i], a[x+z-(1<<i)][y+z-(1<<i)][i][i])<<"\n";
}
return 0;
}