Cod sursa(job #1488949)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 20 septembrie 2015 11:27:01
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 500;
const int MAX_LOG = 9;

int a[MAX_LOG][MAX_N][MAX_N];

int main()
{
    ifstream in("plantatie.in");
    ofstream out("plantatie.out");
    int n, m;
    int l, r, x;

    in >> n >> m;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            in >> a[0][i][j];
    for (int i = 1; (1 << i) <= n; i++)
        for (int j = 0; j + (1 << i) <= n; j++)
            for (int k = 0; k + (1 << i) <= n; k++)
                a[i][j][k] = max(max(a[i - 1][j][k], a[i - 1][j + (1 << (i - 1))][k + (1 << (i - 1))]), max(a[i - 1][j + (1 << (i - 1))][k], a[i - 1][j][k + (1 << (i - 1))]));
    for (int i = 0; i < m; i++)
    {
        in >> l >> r >> x;
        l--;
        r--;
        int topL = l + x - 1;
        int topR = r + x - 1;
        x = 31 - __builtin_clz(x);
        out << max(max(a[x][l][r], a[x][topL - (1 << x) + 1][topR - (1 << x) + 1]), max(a[x][topL - (1 << x) + 1][r], a[x][l][topR - (1 << x) + 1])) << '\n';
    }
    in.close();
    out.close();
    return 0;
}