Cod sursa(job #3149109)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 6 septembrie 2023 14:23:29
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#define pb push_back
using namespace std;
ifstream cin("plantatie.in");
ofstream cout("plantatie.out");
using pii = pair<int,int>;
const int nmax = 500 + 1;
int dp[9][nmax][nmax] , n , q , x , y , k , lg[nmax];
signed main()
{
    cin >> n >> q;
    lg[0] = -1;
    for(int i = 1 ; i <= n ; i++)
    {
        lg[i] = lg[i/2] + 1;
        for(int j = 1 ; j <= n ; j++)
        {
            cin >> dp[0][i][j];
        }
    }
    int tp = 1;
    for(int pw = 1 ; pw <= lg[n] ; pw++)
    {
        tp *= 2;
        for(int i = tp ; i <= n ; i++)
        {
            for(int j = tp ; j <= n ; j++)
            {
                dp[pw][i][j] = max(max(dp[pw-1][i][j],dp[pw-1][i-(1<<(pw-1))][j-(1<<(pw-1))]),
                                   max(dp[pw-1][i-(1<<(pw-1))][j],dp[pw-1][i][j-(1<<(pw-1))]));
            }
        }
    }
    while(q--)
    {
        cin >> x >> y >> k;
        x+=k-1;
        y+=k-1;
        int l = lg[k];
        int ans = dp[l][x][y];
        ans = max(ans,max(dp[l][x-k+(1<<l)][y],dp[l][x][y-k+(1<<l)]));
        ans = max(ans,dp[l][x-k+(1<<l)][y-k+(1<<l)]);
        cout << ans << '\n';
    }
    return 0;
}