Cod sursa(job #2465120)

Utilizator HumikoPostu Alexandru Humiko Data 29 septembrie 2019 14:23:07
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cstring>

using namespace std;

//#include <iostream>
#include <fstream>

//ifstream cin ("input.in");
//ofstream cout ("output.out");

ifstream cin ("plantatie.in");
ofstream cout ("plantatie.out");

static const int NMAX = 505;

int logarithm[NMAX];

int dp[NMAX][NMAX][15];

void preCalculate(int n) {
	for ( int i = 2; i <= n; ++i ) {
			logarithm[i] = logarithm[i/2]+1;
		}

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

int getAnswer(int i, int j, int k) {
	int intermI = i+k-(1<<logarithm[k]);
	int intermJ = j+k-(1<<logarithm[k]);

	int answer = max(max(dp[i][j][logarithm[k]], dp[intermI][j][logarithm[k]]),
					 max(dp[i][intermJ][logarithm[k]], dp[intermI][intermJ][logarithm[k]])
					);
	
	return answer;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	int n, m;
	cin>>n>>m;
	for ( int i = 1; i <= n; ++i ) {
		for ( int j = 1; j <= n; ++j ) {
			cin>>dp[i][j][0];
		}
	}

	preCalculate(n);

	for ( int q = 1; q <= m; ++q ) {
		int i, j, k;
		cin>>i>>j>>k;

		cout<<getAnswer(i, j, k)<<'\n';
	}

}