Cod sursa(job #2893028)

Utilizator EduardSanduSandu Eduard Alexandru EduardSandu Data 24 aprilie 2022 18:04:04
Problema Plantatie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;

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

int mat[11][501][501];

int findpower(int x)
{
    int pow = -1;
    while(x)
    {
        x >>= 1;
        pow++;
    }
    return pow;
}

int main()
{
    int n,i,j,k,m,linienoua,coloananoua,x,y,p,putere;
    fin>>n>>m;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            fin>>mat[0][i][j];
        }
    }

    for(i=1; (1<<i) <= n; i++) // range -- 2^i interval
    {
        for(j=1; j + (1<<i) - 1 <= n; j++) // position on x-dimension
        {
            for(k=1; k + (1<<i) - 1 <= n; k++) // position on y-dimension
            {
                linienoua = j + (1 << (i-1));
                coloananoua = k + (1 << (i-1));
                mat[i][j][k] = max(mat[i-1][j][k], max(mat[i-1][j][coloananoua], max(mat[i-1][linienoua][k], mat[i-1][linienoua][coloananoua])));
            }
        }
    }

    for(i=1;i<=m;i++)
    {
        // rezolvam fiecare interogare
        fin>>x>>y>>p;
        putere = findpower(p);
        linienoua = x + p - (1 << putere);
        coloananoua = y + p - (1 << putere);
        fout << max(mat[putere][j][k], max(mat[putere][j][coloananoua], max(mat[putere][linienoua][k], mat[putere][linienoua][coloananoua]))) << '\n';
    }

    return 0;
}