Cod sursa(job #2795583)

Utilizator cristiWTCristi Tanase cristiWT Data 6 noiembrie 2021 17:08:40
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
// plantatie.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <bits/stdc++.h>
#define NMAX 510

using namespace std;

ifstream f("plantatie.in");
ofstream g("plantatie.out");

int n, m, a[510][510][15];

void build() {
	for (int k = 1; (1 << k) <= n; k++)
		for (int i = 1; i <= n - (1 << k) + 1; i++)
			for (int j = 1; j <= n - (1 << k) + 1; j++)
				a[i][j][k] = max(max(a[i][j][k - 1], a[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1]),
							max(a[i + (1 << (k - 1))][j][k - 1], a[i][j + (1 << (k - 1))][k-1]));
}

int main()
{
	f >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			f >> a[i][j][0];
	
	build();

	for (int i = 1; i <= m; i++) {
		int x, y, L;
		f >> x >> y >> L;
		int k = log2(L);
		int new_x = x + L - (1 << k), new_y = y + L - (1 << k);
		g << max(max(a[x][y][k], a[new_x][new_y][k]), max(a[new_x][y][k], a[x][new_y][k])) << '\n';
	}
}