Cod sursa(job #966641)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 26 iunie 2013 13:09:03
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <ctime>
#include <utility>

using namespace std;

ifstream cin("plantatie.in");
ofstream cout("plantatie.out");

const int N = 505, LgMAX = 10;
int n, m, RMQ[N][N][LgMAX], lg[LgMAX];

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

const inline int max(const int a, const int b, const int c, const int d)
{
    return max(a, max(b, max(c, d)));
}

int main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; ++ i)
    {
        for(int j = 1 ; j <= n ; ++ j)
            cin >> RMQ[i][j][0];
        if(i>1)
            lg[i] = lg[i>>1] + 1;
    }
    for(int k = 1 ; (1<<k) <= n ; ++ k)
        for(int i = 1 ; i <= n - (1 << (k-1)) + 1 ; ++ i)
            for(int j = 1 ; j <= n - (1 << (k-1)) + 1 ; ++ j)
            {
                int l = 1 << (k-1);
                int a = RMQ[i][j][k-1],
                    b = RMQ[i+l][j][k-1],
                    c = RMQ[i][j+l][k-1],
                    d = RMQ[i+l][j+l][k-1];
                RMQ[i][j][k] = max(a, b, c, d);
            }
    for(int i = 1 ; i <= m ; ++ i)
    {
        int x, y, z;
        cin >> x>> y >> z;
        int LOG = lg[z],
            l = z - (1 << (LOG)),
            a = RMQ[x][y][LOG],
            b = RMQ[x+l][y][LOG],
            c = RMQ[x][y+l][LOG],
            d = RMQ[x+l][y+l][LOG];
        cout << max(a, b, c, d) << "\n";
    }
    return 0;
}