Cod sursa(job #2342930)

Utilizator AndreiTurcasTurcas Andrei AndreiTurcas Data 13 februarie 2019 15:40:44
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <fstream>
#define lim 501
using namespace std;

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

int n, m, rmq[lim][lim][20], Lg[lim];

void preprocess()
{
    for(int i = 2; i <= n; ++i)
        Lg[i] = Lg[i>>1] + 1;
    for(int k = 1; (1<<k) <= n; ++k)
        for(int i = 1; i+(1<<k)-1 <= n; ++i)
            for(int j = 1; j+(1<<k)-1 <= n; ++j)
    {
        int f1, f2, f3, f4, p = 1<<(k-1);
        f1 = rmq[i][j][k-1];
        f2 = rmq[i][j+p][k-1];
        f3 = rmq[i+p][j][k-1];
        f4 = rmq[i+p][j+p][k-1];
        rmq[i][j][k] = max(max(f1, f2), max(f3, f4));
    }
}

int main()
{
    ios::sync_with_stdio(false);
    f >> n >> m;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            f >> rmq[i][j][0];
    preprocess();
    for(int i = 1; i <= m; ++i)
    {
        int x, y, z, lg, sol1, sol2, sol3, sol4, p;
        f >> x >> y >> z;
        lg = Lg[z-1], p = 1<<lg;
        sol1 = rmq[x][y][lg];
        sol2 = rmq[x][y+z-p][lg];
        sol3 = rmq[x+z-p][y][lg];
        sol4 = rmq[x+z-p][y+z-p][lg];
        g << max(max(sol1, sol2), max(sol3, sol4)) << "\n";
    }
}