Cod sursa(job #887655)

Utilizator Theorytheo .c Theory Data 23 februarie 2013 23:15:54
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include<fstream>

using namespace std;

ifstream fin("plantatie.in");
ofstream fout("plantatie.out");

const int Nmax = 505;

int N; int T; int log[Nmax]; int A[10][Nmax][Nmax];


void Read(){

    fin >> N >> T;
    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j)
            fin >> A[0][i][j];
}

int Max(const int &X, const int &Y){
    return X > Y ? X : Y;
}


void Build(){

    for(int i = 1; i <= N; ++i) log[i] = log[i >> 1] + 1;

    for(int k = 1; k <= log[N]; ++k){

        int Dist = (1 << (k - 1));

        for(int i = 1; i <= N - Dist; ++i)
            for(int j = 1; j <= N - Dist; ++j){

                A[k][i][j] = Max(A[k - 1][i][j], A[k - 1][i + Dist][j]);
                A[k][i][j] = Max(A[k][i][j], A[k - 1][i][j + Dist]);
                A[k][i][j] = Max(A[k][i][j], A[k - 1][i + Dist][j + Dist]);
            }
    }

}

void Query(const int &X, const int &Y, const int &K){

    int Max1 = 0; int Max2 = 0;

    int L = log[K] - 1;int Dist = (1 << L);

    Max1 = Max(A[L][X][Y], A[L][X + K  - Dist][Y + K - Dist]);
    Max2 = Max(A[L][X + K - Dist][Y], A[L][X][Y + K - Dist]);

    fout << Max(Max1, Max2)<<'\n';
}


int main(){

    Read(); Build();

    while(T--){
        int X, Y, K;
        fin >> X >> Y >> K;
        Query(X, Y, K);
    }

    return 0;
}