Cod sursa(job #389153)

Utilizator alexandru92alexandru alexandru92 Data 1 februarie 2010 10:35:44
Problema Plantatie Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 2.01 kb
//      plantatie.c
//      
//      Copyright 2010 SpeeDemon <virtualdemon@ubuntu>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
#include <stdio.h>
#define Nmax 500
#define Log2Nmax 9

/*
 * 
 */
int RMQ[Nmax][Nmax][Log2Nmax];
inline int max( int x, int y, int w, int z )
{
	int maxim=x;
	if( maxim < y )
		maxim=y;
	if( maxim < w )
		maxim=w;
	if( maxim < z )
		maxim=z;
	return maxim;
}
inline int Log2( int n )
{
	int rez=0;
	if( n >= 1<<16 )
		n>>=16, rez+=16;
	if( n >= 1<<8 )
		n>>=8, rez+=8;
	if( n >= 1<<4 )
		n>>=4, rez+=4;
	if( n >= 1<<2 )
		n>>=2, rez+=2;
	if( n >= 1<<1 )
		rez+=1;
	if( !n )
		rez=-1;
	return rez;
}
int main()
{int n, m, i, j, k, c, c2, p, till;
	freopen( "plantatie.in", "rt", stdin );
	scanf("%d%d", &n, &m );
	for( i=0; i < n; ++i)
		for( j=0; j < n; ++j )
			scanf("%d", ( *( *(RMQ+i)+j)) );
	for( k=1; (1<<k) <= n; ++k )
	{
		c=k-1;
		c2=(1<<c);
		till=n-(1<<k)+1;
		for( i=0; i < till; ++i )
			for( j=0; j < till; ++j )
				RMQ[i][j][k]=max( RMQ[i][j][c], RMQ[i+c2][j][c], RMQ[i+c2][j+c2][c], RMQ[i][j+c2][c] );
	}
	freopen( "plantatie.out", "wt", stdout );
	for( ; m; --m )
	{
		scanf("%d%d%d", &i, &j, &k );
		p=Log2( k );
		c=(1<<p);
		i-=1; j-=1;
		printf("%d\n", max( RMQ[i][j][p], RMQ[i+k-c][j][p], RMQ[i+k-c][j+k-c][p], RMQ[i][j+k-c][p] ) );
	}
	return 0;
}