Cod sursa(job #868768)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 31 ianuarie 2013 16:49:23
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <cmath>
#define nmax (1<<9)
#define lmax 10
using namespace std;

int N,Q,Answer,Log[nmax],Rmq[lmax][nmax][nmax];

int main() {

    int i,j,k;
    ifstream in("plantatie.in");
    ofstream out("plantatie.out");

    in>>N>>Q;

    for(i=1;i<=N;i++)
        for(j=1;j<=N;j++)
            in>>Rmq[0][i][j];

    for(k=1;k<lmax;k++)
        for(i=1;i+(1<<k)-1<=N;i++)
            for(j=1;j+(1<<k)-1<=N;j++)
                Rmq[k][i][j]= max(max( Rmq[k-1][i][j] , Rmq[k-1][i+(1<<k-1)][j] ), max( Rmq[k-1][i][j+(1<<k-1)] , Rmq[k-1][i+(1<<k-1)][j+(1<<k-1)] ));

    for(i=2;i<nmax;i++)
        Log[i]=Log[i/2]+1;

    while(Q--) {

        in>>i>>j>>k;

        Answer=max(max( Rmq[Log[k]][i][j] , Rmq[Log[k]][i+k-(1<<Log[k])][j] ), max( Rmq[Log[k]][i][j+k-(1<<Log[k])] , Rmq[Log[k]][i+k-(1<<Log[k])][j+k-(1<<Log[k])] ));

        out<<Answer<<'\n';

        }

    in.close();
    out.close();

    return 0;

}