Cod sursa(job #2885949)

Utilizator AndreiCroitoruAndrei Croitoru AndreiCroitoru Data 6 aprilie 2022 19:55:06
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 501;
const int M = 75001;
int ma[10][N][N];
int logg[N];
int main()
{

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

    int n, q;
    cin >> n >> q;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <=n; j++)
        {
            cin >> ma[0][i][j];
        }
    }
    for(int i = 2 ; i <= n ; i++)
    {
        logg[i] = 1 + logg[i / 2];
    }
    for(int i = 1 ; i <= 1 + logg[n] ; i ++)
    {
        for(int j = 1 << i ; j <= n ; j ++)
        {
            for(int k = 1 << i ; k <= n ; k ++)
            {
                ma[i][j][k] = max (ma[i - 1][j - (1 << (i - 1))][k - (1 << (i - 1))], ma[i - 1][j][k]);
                ma[i][j][k] = max (ma[i][j][k], ma[i - 1][j - (1 << (i - 1))][k]);
                ma[i][j][k] = max (ma[i][j][k], ma[i - 1][j][k - (1 << (i - 1))]);
            }
        }
    }
    for(int i = 1 ; i <= q; i ++)
    {
        int a , b , c;
        cin >> a >> b >> c;
        int l = (1 << logg[c]);
        int ans , lg = logg[c];
        if (l == c)
            ans = ma[lg][a + c - 1][b + c - 1];
        else
            ans = max (ma[lg][a + c - 1][b + c - 1] , max (ma[lg][a + c - 1][b + l - 1] , max (ma[lg][a + l - 1][b + c - 1] , ma[lg][a + l - 1][b + l - 1])));
        cout << ans << "\n";
    }
    return 0;
}