Pagini recente » Cod sursa (job #2734365) | Cod sursa (job #2998575) | Cod sursa (job #2228458) | Borderou de evaluare (job #1291275) | Cod sursa (job #2566509)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;
const int DMAX = 510;
int M[10][DMAX][DMAX];
int mat[DMAX][DMAX];
int n,q;
void constr(){
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
M[0][i][j]=mat[i][j];
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++)
M[k][i][j]=max(M[k-1][i][j],max(M[k-1][i+(1<<(k-1))][j],max(M[k-1][i][j+(1<<(k-1))],M[k-1][i+(1<<(k-1))][j+(1<<(k-1))])));
}
int query(int x,int y,int k){
int kk=log2(k);
int ans=M[kk][x][y];
ans=max(ans,M[kk][x+k-(1<<kk)][y]);
ans=max(ans,M[kk][x][y+k-(1<<kk)]);
ans=max(ans,M[kk][x+k-(1<<kk)][y+k-(1<<kk)]);
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int t,i,j;
int x,y,k;
fin>>n>>q;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
fin>>mat[i][j];
constr();
while(q--){
fin>>x>>y>>k;
fout<<query(x,y,k)<<'\n';
}
return 0;
}