Cod sursa(job #1345280)

Utilizator gapdanPopescu George gapdan Data 17 februarie 2015 15:02:59
Problema Plantatie Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include<cstdio>
#define max(a, b) (a > b ? a : b)

using namespace std;

int n, m, k;
int a[505][505], R[505][505][10];
int lg[505];

void citire()
{
    freopen("plantatie.in", "r" ,stdin);
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j){

            scanf("%d",&a[i][j]);
            R[i][j][0]=a[i][j];
        }
    lg[1]=0;
    for(int i = 2; i <= n; ++i) lg[i]=lg[i/2]+1;

}

void rmq()
{
    int l;
    for(int k = 1; 1<<k <= n; ++k)
        for(int i = 1; i + (1<<k) - 1 <= n; ++i)
            for(int j = 1; j + (1<<k) - 1 <= n; ++j)
            {
                l = 1 << (k-1);
                R[i][j][k] = max(R[i][j][k-1], R[i+l][j+l][k-1]);
                R[i][j][k] = max(R[i][j][k], R[i+l][j][k-1]);
                R[i][j][k] = max(R[i][j][k], R[i][j+l][k-1]);
            }
}

void afis()
{
    freopen("plantatie.out", "w", stdout);
    int l, sh, x, y, sol;
    for(int i = 1; i <= m; ++i)
    {
        scanf("%d%d%d",&x,&y,&k);
        l=lg[k];
        sh = k - (1<<l);
        sol = max(R[x][y][l], R[x+sh][y+sh][l]);
        sol = max(sol,R[x+sh][y][l]);
        sol = max(sol, R[x][y+sh][l]);
        printf("%d\n", max(R[x][y][l], R[x+sh][y+sh][l]) );
    }
}

int main()
{
    citire();
    rmq();
    afis();
    return 0;
}