Cod sursa(job #1201786)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 26 iunie 2014 00:18:57
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#define DIMN 501
#define MLOG 11

using namespace std;

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

int RMQ[DIMN][DIMN][MLOG], B[DIMN];

int n, q, ll, x, y, z;

inline int maxim (int x, int y, int z, int t) {
    x = x<y ? y : x;
    x = x<z ? z : x;
    x = x<t ? t : x;
    return x;
}

int main() {
    f >> n >> q;
    for (int i=1; i<=n; ++i)
        for (int j=1; j<=n; ++j)
            f >> RMQ[i][j][0];
    for (int l=1; (1<<l)<=n; ++l) {
        ll = (1<<(l-1));
        for (int i=1; i<=n; ++i)
            for (int j=1; j<=n; ++j)
                if ( i+(ll<<1)>n || j+(ll<<1) > n )
                    RMQ[i][j][l] = 0;
                else
                    RMQ[i][j][l]=maxim(RMQ[i][j][l-1],RMQ[i][j+ll][l-1],RMQ[i+ll][j][l-1],RMQ[i+ll][j+ll][l-1]);
    }
    for (int i=2; i<=n; ++i)
        B[i] = B[i/2] + i%2;
    for (;q;--q) {
        f >> x >> y >> z;
        int l = B[z];
        g << maxim(RMQ[x][y][l],RMQ[x+z-(1<<l)][y][l],RMQ[x][y+z-(1<<l)][l],RMQ[x+z-(1<<l)][y+z-(1<<l)][l]) << "\n";
    }
    return 0;
}