Cod sursa(job #2058400)

Utilizator abccnuCamelia Zalum abccnu Data 5 noiembrie 2017 16:48:27
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int NMAX = 500 + 5;

int N , M;
int mat[NMAX][NMAX];
int rmq[NMAX][NMAX][9], lg[NMAX];


void calcul()
{
    lg[2] = 1;

    for (int i = 3; i < NMAX; ++i)lg[i] = lg[i >> 1] + 1;

    for (int i = 1 ; i < lg[N] ; ++i)
    {
        for (int j = 1; j<= N - (1 << i) + 1; ++j)
        {
            for (int k = 1; k<= N - (1 << i) + 1; ++k)
            {
                int latura  = 1 << (i - 1);
                rmq[j][k][i] = max (rmq[j][k][i-1], max (rmq[j][k + latura][i-1], max (rmq[j+latura][k][i-1], rmq[j+latura][k+latura][i-1])));
            }
        }
    }
}

int raspuns(int x, int y, int l)
{
    int dif = lg[y - x + 1];
    int lat = 1 << dif;
    int nx = x + l - 1, ny = y + l - 1;
    return max (rmq[x][y][dif], max(rmq[x][ny - (1 << dif) + 1][dif], max (rmq[nx- (1 << dif) + 1][y][dif], rmq[nx- (1 << dif) + 1][ny- (1 << dif) + 1][dif])));
}


void read()
{
    fin >> N >> M;


    for (int i =1; i<=N; ++i)
        for (int j =1; j<=N; ++j)
        {
            fin >> mat[i][j];
            rmq[i][j][0] = mat[i][j];
        }
    calcul();
    int x, y, l;
    for (int i =1; i<=M; ++i)
    {
        fin >> x >> y >> l;
        fout << raspuns(x, y, l)<< "\n";
    }
}

int main()
{
    read();
    return 0;
}