Pagini recente » Cod sursa (job #1315989) | Cod sursa (job #1313771) | Cod sursa (job #353800) | Cod sursa (job #1916850) | Cod sursa (job #3326887)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
int rmq[10][501][501], n, m, p2[10];
int main(){
fin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
fin>>rmq[0][i][j];
}
}
int maxe=log2(n);
p2[0]=1;
for(int i=1;i<=maxe;i++){
p2[i]=p2[i-1]*2;
}
for(int e=1;e<=maxe;e++){
//cout<<e<<endl;
for(int i=1;i<=n-p2[e]+1;i++){
for(int j=1;j<=n-p2[e]+1;j++){
rmq[e][i][j]=max(max(rmq[e-1][i][j],rmq[e-1][i+p2[e-1]][j+p2[e-1]]),max(rmq[e-1][i+p2[e-1]][j],rmq[e-1][i][j+p2[e-1]]));
//cout<<i<<' '<<j<<' '<<rmq[e][i][j]<<endl;
}
}
}
int x, y, k, ans, e;
for(int i=0;i<m;i++){
fin>>x>>y>>k;
e=log2(k);
ans=max(max(rmq[e][x][y], rmq[e][x+k-p2[e]][y+k-p2[e]]), max(rmq[e][x+k-p2[e]][y], rmq[e][x][y+k-p2[e]]));
fout<<ans<<'\n';
}
return 0;
}