Cod sursa(job #1005469)

Utilizator nicuvladNicu Vlad nicuvlad Data 5 octombrie 2013 07:56:05
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>
#include <algorithm>
#define N 503
using namespace std;

int rmq[11][N][N], lg[N];
int n;

void init()
{
	int i, j, k, l;
	for(k=1;(1<<k)<=n;k++)
	{
		for(i=1;i+(1<<k)-1<=n;i++)
		{
			for(j=1;j+(1<<k)-1<=n;j++)
			{
				l=1<<(k-1);
				rmq[k][i][j]=max( max(rmq[k-1][i][j], rmq[k-1][i][j+l]), max(rmq[k-1][i+l][j], rmq[k-1][i+l][j+l]) );
			}
		}
	}
}

int query(int x1, int y1, int l)
{
	int ret=0;
	int x2, y2, max1, max2;
	x2=x1+l-1;
	y2=y1+l-1;
	l=lg[l];
	max1=max(rmq[l][x1][y1],rmq[l][x2-(1<<l)+1][y2-(1<<l)+1]);
	max2=max(rmq[l][x1][y2-(1<<l)+1],rmq[l][x2-(1<<l)+1][y1]);
	ret=max(max1, max2);
	return ret;
}

int main()
{
	freopen("plantatie.in", "r", stdin);
	freopen("plantatie.out", "w", stdout);
	int i, j, q, x, y, k, sol=0;
	scanf("%d%d", &n, &q);
	for(i=2;i<=n;i++)
	{
		lg[i]=lg[i>>1]+1;
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			scanf("%d", &rmq[0][i][j]);
		}
	}
	init();
	for(;q;q--)
	{
		scanf("%d%d%d", &x, &y, &k);
		sol=query(x, y, k);
		printf("%d\n", sol);
	}
}