Cod sursa(job #332655)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 19 iulie 2009 01:42:52
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
# include <algorithm>
using namespace std;

# define DIM 501
# define LOG 10

int n, m, pow[ DIM ], sol[ DIM ][ DIM ][ LOG ];

inline int max ( int w, int x, int y, int z ) {

    if ( w > x && w > y && w > z )
        return w;

    if ( x > w && x > y && x > z )
        return x;

    if ( y > w && y > x && y > z )
        return y;

    return z;
}

void calc_sol () {

    int i, j, k;

    for ( k = 1; ( 1 << k ) <= n; ++ k )
        for ( i = 1; i <= n; ++ i ) if ( i + ( 1 << ( k - 1 ) ) <= n )
            for ( j = 1; j <= n; ++ j ) if ( j + ( 1 << ( k - 1 ) ) <= n )
                sol[ i ][ j ][ k ] = max ( sol[ i ][ j ][ k - 1 ], sol[ i ][ j + ( 1 << ( k - 1 ) ) ][ k - 1 ], sol[ i + ( 1 << ( k - 1 ) ) ][ j ][ k - 1 ], sol[ i + ( 1 << ( k - 1 ) ) ][ j + ( 1 << ( k - 1 ) ) ][ k - 1 ] );
}

void calc_pow () {

    int i, p;

    p = 0;
    for ( i = 1; i <= n; ++ i ) {

        if ( ( 1 << ( p + 1 ) ) == i )
            ++ p;
        pow[ i ] = p;
    }
}

void solve () {

    int i, j, k, d, p, x, y;

    scanf ( "%d%d", &n, &m );
    for ( i = 1; i <= n; ++ i )
        for ( j = 1; j <= n; ++ j )
            scanf ( "%d", &sol[ i ][ j ][ 0 ] );
    calc_sol ();
    // calc_pow ();

    /*
    for ( i = 0; i < m; ++ i ) {

        scanf ( "%d%d%d", &x, &y, &k );
        p = pow[ k ];
        d = k - ( 1 << pow[ k ] );
        printf ( "%d\n", max ( sol[ x ][ y ][ p ], sol[ x ][ y + d ][ p ], sol[ x + d ][ y ][ p ], sol[ x + d ][ y + d ][ p ] ) );
    }
    */

    int tmp = 0;

     for (int i=1;i<=n;++i)
     {
          if (i==(1<<(tmp+1)))
               ++tmp;
          pow[i] = tmp;
     }

     #define dif(q) (q-(1<<(pow[q])))

     for (;m>0;--m)
     {
          int x,y,k;
          scanf("%d%d%d", &x,&y,&k);
          printf("%d\n", max(sol[x][y][pow[k]],sol[x+dif(k)][y][pow[k]],sol[x][y+dif(k)][pow[k]],sol[x+dif(k)][y+dif(k)][pow[k]]));
     }

}

int main () {

    freopen ( "plantatie.in", "r", stdin );
    freopen ( "plantatie.out", "w", stdout );

    solve ();

    return 0;
}