Cod sursa(job #1543767)

Utilizator BlackLordFMI Alex Oprea BlackLord Data 6 decembrie 2015 11:41:43
Problema Plantatie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#define FOR(a,b,c) for(int a=b; a<=c; ++a)
#define max(a,b) (a>b?a:b)
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int n, m, i, j, x, y, z;
int a[510][510][10];
int main(){
    f>>n>>m;
    FOR(i,1,n)
        FOR(j,1,n)
            f>>a[i][j][0];
    z=0;
    while( (1<<z++)<=n );
    z-=2;
    FOR(i,1,z)
        FOR(x,1,n)
            FOR(y,1,n)
                a[x][y][i]=max( max(a[x][y][i-1], a[x+( 1<<(i-1) )][y+( 1<<(i-1) )][i-1]), max(a[x+( 1<<(i-1) ) ][y][i-1], a[x][y+( 1<<(i-1) )][i-1]) );
    FOR(intrebari,1,m)
    {
        f>>x>>y>>z;
        i=0;
        while( (1<<i++)<=z );
        i-=2;
        g<<max( max(a[x][y][i], a[x+z-(1<<i)][y+z-(1<<i)][i]), max(a[x+z-(1<<i)][y][i], a[x][y+z-(1<<i)][i]) )<<"\n";
    }
    return 0;
}