Cod sursa(job #3297080)

Utilizator DalvDalvGhita Vladut DalvDalv Data 20 mai 2025 22:35:10
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bit>
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

int plantatie[501][501][9];

#include <bit>
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

int getAt(int i, int j, int p) {
	if(i < 0 || i >= 500 || j < 0 || j >= 500) return -1;
	return plantatie[i][j][p];
}

int main() {
	ifstream cin("plantatie.in");
	ofstream cout("plantatie.out");
	int n, m; cin >> n >> m;
	int log2n = log2(n) + 1;

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cin >> plantatie[i][j][0];
		}
	}

	for(int p = 1; p < log2n; p++) {
		int offs = 1 << p - 1;
		for(int i = 0; i < n + (1 << p - 1); i++) {
			for(int j = 0; j < n + (1 << p - 1); j++) {
				plantatie[i][j][p] = max(
					max(getAt(i, j, p-1), getAt(i + offs, j + (1 << p - 1), p - 1)),
					max(getAt(i, j + offs, p - 1), getAt(i + offs, j, p - 1))
				);
			}
		}
	}

	while(m-- > 0) {
		int i, j, l; cin >> i >> j >> l;
		i--;j--;
		int p = log2(l);
		int offs = l - (1 << p);
		int res = max(
			max(getAt(i, j, p), getAt(i + offs, j + offs, p)),
			max(getAt(i + offs, j, p), getAt(i, j + offs, p))
		);
		cout << res << "\n";
	}

	::cin >> n;
}