Cod sursa(job #1606570)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 20 februarie 2016 12:53:59
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <cmath>
#include <algorithm>

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

int N, M, RMQ[502][502][9], k, rs;
int main(){
	cin >> N >> M;
	for(int i = 1; i <= N; i++)
		for(int j = 1; j <= N; j++){
			cin >> RMQ[i][j][0];
	}
	for(int k = 1 ; k <= 9; k++){
		for(int i = 1; i + (1 << (k-1)) <= N; i++)
			for(int j = 1; j + (1 << (k-1)) <= N; j++){
			int max1 = max(RMQ[i][j][k-1] , RMQ[i][j + (1 << (k-1))][k-1]);
			int max2 = max(RMQ[i + (1 << (k-1))][j][k-1], RMQ[i + (1 << (k-1))][j + (1 << (k-1))][k-1]);
			RMQ[i][j][k] = max(max1, max2);
			}
		}
	for(int i1 = 1; i1 <= M; i1++){
		int i , j ,k; 
		cin >> i >> j >> k;
		int k1 = log2(k);
		int max1 = max(RMQ[i][j][k1], RMQ[i][j + k - (1<<k1)][k1]);
		int max2 = max(RMQ[i + k - (1<<k1)][j][k1], RMQ[i + k - (1<<k1)][j + k - (1<<k1)][k1]);
		rs = max(max1, max2);
		cout << rs << '\n';
		}
	return 0;
}