Cod sursa(job #3161447)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 27 octombrie 2023 09:04:30
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
using namespace std;
ifstream cin("plantatie.in");
ofstream cout("plantatie.out");
int n,m,i1,i2,j1,i,j,j2,lat,L,len,k,r[10][502][502],E[502];
int main()
{
    cin>>n>>m;
    for ( i=1; i<=n; i++)
        for ( j=1; j<=n; j++)
            cin>>r[0][i][j];
    for (int p=1,lat=2; lat<=n; p++,lat*=2)
        for ( i1=1; i1<=n-lat+1; i1++)
            for ( j1=1; j1<=n-lat+1; j1++)
            {
                i2=i1+(lat>>1);
                j2=j1+(lat>>1);
                r[p][i1][j1] = max(max(r[p-1][i1][j1],r[p-1][i2][j1]),max(r[p-1][i1][j2],r[p-1][i2][j2]));
            }
    E[1]= 0;
    for ( i=2; i<=n; i++)
        E[i] = 1 + E[i/2];
    while(m--)
    {
        cin>>i1>>j1>>L;
        k = E[L];
        len = (1<<k);
        i2 = i1+L-len;
        j2 = j1+L-len;
        cout<<max(max(r[k][i1][j1],r[k][i1][j2]),max(r[k][i2][j1],r[k][i2][j2]))<<"\n";
    }
    return 0;
}