Cod sursa(job #2821506)

Utilizator Gabriel_DascalescuGabriel Dascalescu Gabriel_Dascalescu Data 22 decembrie 2021 17:39:33
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#define nmax 505

using namespace std;

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

int d[nmax][nmax][11];

int mat[nmax][nmax];

int doi[nmax], lg[nmax];

int n, m, a, b, c;

int main()
{
    in>>n>>m;
    for(int i=2; i<=n; i++)
    {
        lg[i] = lg[i/2] + 1;
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            in>>mat[i][j];
            d[i][j][0] = mat[i][j];
        }
    }
    for(int k=1; k<11; k++)
    {
        for(int i=1; i<=n - (1<<k) + 1; i++)
        {
            for(int j=1; j<=n - (1<<k) + 1; j++)
            {
                int aux = (1<<(k - 1));
                d[i][j][k] = max(max(d[i + aux][j][k - 1], d[i + aux][j + aux][k - 1]), max(d[i][j + aux][k - 1], d[i][j][k - 1]));
            }
        }
    }
    for(int i=1; i<=m; i++)
    {
        in>>a>>b>>c;
        //out<<lg[c]<<" "<<a<<" "<<b<<" "<<c<<" ";
        out<<max(d[a][b][lg[c]], max(d[a][b + c - (1<<lg[c])][lg[c]], max(d[a + c - (1<<lg[c])][b][lg[c]], d[a + c - (1<<lg[c])][b + c - (1<<lg[c])][lg[c]])))<<"\n";
        //out<<d[a][b][lg[c]]<<" "<<d[a][b + c - (1<<lg[c])][lg[c]]<<" "<<d[a + c - (1<<lg[c])][b][lg[c]]<<" "<<d[a + c - (1<<lg[c])][b + c - (1<<lg[c])][lg[c]]<<"\n";
    }
    return 0;
}