Pagini recente » Cod sursa (job #638005) | Cod sursa (job #2145481) | Cod sursa (job #1426569) | Cod sursa (job #2350253) | Cod sursa (job #2566465)
#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;
class rmq{
private:
int n;
vector <vector <vector <int>>> M;
public:
rmq(int n){
this->n=n;
M.resize(n+1,vector <vector <int>>(n+1,vector <int> (10)));
return;
}
void constr(int mat[DMAX][DMAX]){
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
M[i][j][0]=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[i][j][k]=max(M[i][j][k-1],max(M[i+(1<<(k-1))][j][k-1],max(M[i][j+(1<<(k-1))][k-1],M[i+(1<<(k-1))][j+(1<<(k-1))][k-1])));
}
int query(int x,int y,int k){
int kk=log2(k);
int ans=M[x][y][kk];
ans=max(ans,M[x+k-(1<<kk)][y][kk]);
ans=max(ans,M[x][y+k-(1<<kk)][kk]);
ans=max(ans,M[x+k-(1<<kk)][y+k-(1<<kk)][kk]);
return ans;
}
};
int M[DMAX][DMAX];
int n,q;
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>>M[i][j];
rmq vec(n);
vec.constr(M);
while(q--){
fin>>x>>y>>k;
fout<<vec.query(x,y,k)<<'\n';
}
return 0;
}