Cod sursa(job #239252)

Utilizator ProtomanAndrei Purice Protoman Data 4 ianuarie 2009 14:19:28
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

int n, m, x, y, l;
int lg[1 << 10];
int a[512][512][11];


int main()
{
	freopen("plantatie.in","r",stdin);
	freopen("plantatie.out","w",stdout);
	scanf("%d %d", &n, &m);
	for (int i = 2; i <= n; i++)
		lg[i] = lg[i / 2] + 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			scanf("%ld", &a[i][j][0]);
	for (int lc = 0; lc < lg[n]; lc++)
	{
		int lg2 = 1 << lc;
		for (int i = 1; i <= n - lg2 + 1; i++)
			for (int j = 1; j <= n - lg2 + 1; j++)
				a[i][j][lc + 1] = max(max(a[i][j][lc], a[i + lg2][j + lg2][lc]), 
					max(a[i + lg2][j][lc], a[i][j + lg2][lc]));
	}
	for (; m; m--)
	{
		scanf("%d %d %d", &x, &y, &l);
		int maxim = 0;
		maxim =  max(max(a[x][y][lg[l]], a[x + l - (1 << lg[l])][y + l - (1 << lg[l])][lg[l]]),
			max(a[x + l - (1 << lg[l])][y][lg[l]], a[x][y + l - (1 << lg[l])][lg[l]]));
		printf("%d\n", maxim);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}