Cod sursa(job #2486099)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 2 noiembrie 2019 12:06:37
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
ll n, q;
ll pd[509][509][30];
ll lg2[509];
ll x,y, L, ans,z;
int main()
{
    f >> n >> q;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            f >> pd[i][j][0];

    lg2[0] = lg2[1] = 0;

    for(int i = 2; i <= n; ++i)
        lg2[i] = lg2[i >> 1] + 1;

    for(int k = 1; k <= lg2[n]; ++k)
        for(int i = 1; i + (1 << (k - 1))<= n; ++i)
        for(int j = 1; j + (1 << (k - 1))<= n; ++j)
                pd[i][j][k] = max(  max( pd[i][j][k-1], pd[i][j + (1 << (k-1))][k-1]),
                                    max( pd[i + (1 << (k-1))][j][k-1],pd[i + (1 << (k-1))][j + (1 << (k-1))][k-1]) );
    while(q--)
    {
        f >> x >> y >> z;
        L = lg2[z];
        ll ans = max(max(pd[x][y][L], pd[x][y + z - (1 << L)][L]),
                    max(pd[x + z - (1 << L)][y][L] , pd[x + z - (1 << L)][y + z - (1 << L)][L]));
        g << ans << '\n';
    }
    return 0;
}