Cod sursa(job #1697378)

Utilizator pringonGoje Samuel Andrei Daniel pringon Data 1 mai 2016 20:47:26
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>

using namespace std;

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

int mat[502][502][10], log[502], n, m, lin, col, lat;

int max(int a,int b) {
    if(a > b)
        return a;
    return b;
}

void preMat() {
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
           for(int k=1; (1<<k) <=j; k++)
            mat[i][j][k] = max(mat[i][j][k-1], mat[i][j - (1<<(k-1)) ][k-1]);
}

void preLog() {
    log[1] = 0;

    for(int j=2;j<=n;j++)
            log[j] = 1 + log[j/2];
}

void read() {
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            in>>mat[i][j][0];

    preMat();
    preLog();
}

void response() {
    for(int nr=1;nr<=m;nr++) {
        in>>lin>>col>>lat;

        int biggest = 0;
        int l, current;

        for(int i=lin;i<=(lin+lat-1);i++)
            for(int j=col;j<=(col+lat-1);j++) {
                l = log[j-i];
                current = max(mat[i][j][l], mat[i][j + (1<<l) - 1][l]);
                biggest = max(biggest, current);
            }
        out<<biggest<<"\n";
    }
}

int main()
{
    in>>n>>m;
    read();

    response();

    in.close();
    out.close();
    return 0;
}