Cod sursa(job #1486919)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 15 septembrie 2015 18:37:25
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <vector>
#include <array>
#include <utility>
#include <algorithm>
#include <cmath>
using namespace std;

class two_d_rmq{
	int n;
	array<array<array<int, 10>, 500>, 500> buf;
public:
	two_d_rmq(const vector<vector<int> >& mat):
		n(mat.size()){
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j){
				buf[i][j][0] = mat[i][j]; } }
		for(int adanc = 1, hf_sz = 1; adanc < 10; ++adanc, hf_sz *= 2){
			for(int i = 0; i+hf_sz < n; ++i){
				for(int j = 0; j+hf_sz < n; ++j){
					buf[i][j][adanc] = max({
						buf[i      ][j      ][adanc-1],
						buf[i+hf_sz][j      ][adanc-1],
						buf[i      ][j+hf_sz][adanc-1],
						buf[i+hf_sz][j+hf_sz][adanc-1]}); } } } }
	int query(const int x, const int y, const int k){
		const int adanc = log2(k), dist = 1<<adanc;
		return max({
			buf[x][y][adanc],
			buf[x+k-dist][y][adanc],
			buf[x][y+k-dist][adanc],
			buf[x+k-dist][y+k-dist][adanc]}); } };

int main(){
	ifstream f("plantatie.in");
	ofstream g("plantatie.out");
	int n, m;
	f >> n >> m;
	vector<vector<int> > mat(n, vector<int>(n));
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			f >> mat[i][j]; } }

	two_d_rmq* r = new two_d_rmq(mat);
	for(int i = 0, x, y, k; i < m; ++i){
		f >> x >> y >> k;
		g << r->query(x-1, y-1, k) << '\n'; }
	return 0; }