Pagini recente » Cod sursa (job #2790904) | Cod sursa (job #468504) | Cod sursa (job #3269160) | Cod sursa (job #106998) | Cod sursa (job #3285423)
#include <bits/stdc++.h>
#define NMAX 500
#define MMAX 12
#define ll long long
#define ull unsigned long long
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
int n, m, i1, j1, i2, j2, k;
int rmq[MMAX][NMAX + 2][NMAX + 2], expo[NMAX + 2];
int main() {
fin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
fin >> rmq[0][i][j];
}
}
for (int p = 1, lat = 2; lat <= n; p++, lat *= 2) {
for (int l1 = 1; l1 <= n - lat + 1; l1++) {
for (int c1 = 1; c1 <= n - lat + 1; c1++) {
int l2 = l1 + (lat >> 1);
int c2 = c1 + (lat >> 1);
int max1 = max(rmq[p - 1][l1][c1], rmq[p - 1][l1][c2]);
int max2 = max(rmq[p - 1][l2][c1], rmq[p - 1][l2][c2]);
rmq[p][l1][c1] = max(max1, max2);
}
}
}
for (int i = 2; i <= n; i++) {
expo[i] = expo[i / 2] + 1;
}
while (m--) {
fin >> i1 >> j1 >> k;
int e = expo[k];
int lg = (1 << e);
i2 = i1 + k - lg;
j2 = j1 + k - lg;
int max1 = max(rmq[e][i1][j1], rmq[e][i1][j2]);
int max2 = max(rmq[e][i2][j1], rmq[e][i2][j2]);
fout << max(max1, max2) << '\n';
}
return 0;
}