Cod sursa(job #1216768)

Utilizator xtreme77Patrick Sava xtreme77 Data 5 august 2014 18:12:55
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <fstream>

#define rint register int

const char IN [ ] = "plantatie.in" ;
const char OUT [ ] = "plantatie.out" ;
const int LOGMAX = 20 ;
const int MAX = 520 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

int R [ MAX ] [ MAX ] [ LOGMAX ] , log [ MAX ] ;

int main( )
{
    int n , m ;
    fin >> n >> m ;
    for ( rint i = 1 ; i <= n ; ++ i )
        for ( rint j = 1 ; j <= n ; ++ j )
            fin >> R [ i ] [ j ] [ 0 ] ;
    log [ 1 ] = 0 ;
    for ( rint i = 2 ; i <= n ; ++ i )
        log [ i ] =  log [ i >> 1 ] + 1 ;
    for ( rint k = 1 ; k <= log [ n ] ; ++ k )
        for ( rint i = 1 ; i <= n - ( 1 << k ) + 1 ; ++ i )
            for ( rint j = 1 ; j <= n - ( 1 << k ) + 1 ; ++ j )
                R [ i ] [ j ] [ k ] = max ( R [ i ] [ j ] [ k - 1 ] ,
                                           max ( R [ i + ( 1 << (k-1) ) ] [ j ] [ k - 1 ] ,
                                                max ( R [ i + ( 1 << (k-1) ) ] [ j + ( 1 << (k-1) ) ] [ k - 1 ] , R [ i ] [ j + ( 1 << (k-1) ) ] [ k  - 1 ] )
                                                )
                                        );
    while ( m -- )
    {
        int x ,y , z ;
        fin >> x >> y >> z ;
        int t = log [ z ] ;
        fout << max ( R [ x ] [ y ] [ t ] ,
                     max ( R[ x ] [ y + z - ( 1 << t ) ][ t ] ,
                          max( R [ x + z - ( 1 << t ) ] [ y ] [ t ] , R [ x + z - ( 1 << t ) ] [ y + z - ( 1 << t ) ] [ t ] )
                          )
                     )
                     << '\n' ;
    }
    return 0;
}