Cod sursa(job #2903650)

Utilizator deboradeleanuDebora Deleanu deboradeleanu Data 17 mai 2022 19:24:53
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("plantatie.in");
ofstream out("plantatie.out");
int mat[11][501][501];
int putere(int x)
{
    int pow = -1;
    while(x){
        x >>= 1;
        pow++;
    }
    return pow;
}

int main()
{
    int n,i,j,k,m,l,c,x,y,p,p2;
    in>>n>>m;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            in>>mat[0][i][j];
        }
    }

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

    for(i=1; i<=m; i++)
    {
        in>>x>>y>>p;
        p2 = putere(p);
        l = x + p - (1 << p2);
        c = y + p - (1 << p2);
        out << max(max(mat[p2][x][y], mat[p2][x][c]), max(mat[p2][l][y], mat[p2][l][c])) << '\n';
    }

    return 0;
}