Cod sursa(job #22759)

Utilizator sims_glAlexandru Simion sims_gl Data 27 februarie 2007 12:33:43
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <stdio.h>

#define nm 512

int n, m, a[nm][nm][10], p[nm], sol;

int main()
{
	int i, j, k, tmp, crt;

	freopen("plantatie.in", "r", stdin);
    freopen("plantatie.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for (i = 1; i <= n; ++i)
    	for (j = 1; j <= n; ++j)
        	scanf("%d", &a[i][j][0]);

   	for (tmp = 1, k = 1; tmp <= n; ++k, tmp <<= 1)
	    for (i = 1; i <= n; ++i)
    		for (j = 1; j <= n; ++j)
            {
            	a[i][j][k] = a[i][j][k - 1];
                
                if (i + tmp <= n && a[i + tmp][j][k - 1] > a[i][j][k])
                	a[i][j][k] = a[i + tmp][j][k - 1];

                if (j + tmp <= n && a[i][j + tmp][k - 1] > a[i][j][k])
                	a[i][j][k] = a[i][j + tmp][k - 1];

                if (i + tmp <= n && j + tmp <= n && a[i + tmp][j + tmp][k - 1] > a[i][j][k])
                	a[i][j][k] = a[i + tmp][j + tmp][k - 1];
            }

    for (p[1] = 0, i = 2; i <= n; ++i)
    	p[i] = p[i / 2] + 1;

    for (crt = 1; crt <= m; ++crt)
    {
    	scanf("%d%d%d", &i, &j, &k);

        tmp = 1 << p[k];

        sol = a[i][j][p[k]];

        if (sol < a[i + k - tmp][j][p[k]])
        	sol = a[i + k - tmp][j][p[k]];
            
        if (sol < a[i][j + k - tmp][p[k]])
        	sol = a[i][j + k - tmp][p[k]];
            
        if (sol < a[i + k - tmp][j + k - tmp][p[k]])
        	sol = a[i + k - tmp][j + k - tmp][p[k]];

    	printf("%d\n", sol);
    }

	return 0;
}