Cod sursa(job #1609783)

Utilizator radarobertRada Robert Gabriel radarobert Data 22 februarie 2016 23:58:38
Problema Plantatie Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
#include <cmath>

using namespace std;

inline int max(int a, int b)
{
    if (a > b)
        return a;
    return b;
}

int rmq[502][502][20];

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

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

    for (int k = 1; k <= log2(n); k++)
    {
        for (int i = 1; i <= n; i++)
            for (int j = 1, ni, nj; j <= n; j++)
            {
                ni = i + (1<<(k-1));
                nj = j + (1<<(k-1));
                rmq[i][j][k] = rmq[i][j][k-1];
                if (ni <= n)
                {
                    rmq[i][j][k] = max(rmq[i][j][k], rmq[ni][j][k-1]);
                    if (nj <= n)
                        rmq[i][j][k] = max(rmq[i][j][k], rmq[ni][nj][k-1]);
                }
                if (nj <= n)
                    rmq[i][j][k] = max(rmq[i][j][k], rmq[i][nj][k-1]);
            }
    }

    int x, y, k;
    int t, l;
    while (m--)
    {
        scanf("%d%d%d", &x, &y, &k);
        l = log2(k-1);
        t = rmq[x][y][l];
        t = max(t, rmq[x+k-(1<<l)][y][l]);
        t = max(t, rmq[x][y+k-(1<<l)][l]);
        t = max(t, rmq[x+k-(1<<l)][y+k-(1<<l)][l]);
        printf("%d\n", t);
    }

    return 0;
}