Cod sursa(job #2450971)

Utilizator vladth11Vlad Haivas vladth11 Data 25 august 2019 12:03:14
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;
int n;
int sparse[505][505][18];
int v[505][505];
int logs[505];
void precalculate()
{
    logs[1] = 0;
    for(int i = 2; i <= n; i++)
    {
        logs[i] = logs[i/2] + 1;
    }
}
void build()
{
    for(int k = 0; k <= logs[n]; k++)
    {
        int lungime = (1 << k);
        for(int i = 1; i + lungime <= n + 1; i++)
        {
            for(int j = 1; j + lungime <= n + 1; j++)
            {
                if(lungime != 1)
                {
                    sparse[i][j][k] = max(sparse[i][j][k-1],max(sparse[i + lungime / 2][j][k-1],max(sparse[i][j + lungime / 2][k-1],sparse[i + lungime / 2][j + lungime / 2][k-1])));
                }else{
                    sparse[i][j][k] = v[i][j];
                }
            }
        }
    }
}
int getmin(int x,int y,int k){
    int p = logs[k];
    int lung = (1 << p);
    int i = x + k;
    int j = y + k;
    return max(sparse[x][y][p],max(sparse[i - lung][y][p],max(sparse[x][j - lung][p],sparse[i - lung][j - lung][p])));
}
int main()
{
    int m,x,y,k;
    ifstream cin("plantatie.in");
    ofstream cout("plantatie.out");
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            cin >> v[i][j];
        }
    }
    precalculate();
    build();
    while(m--){
        cin >> x >> y >> k;
        cout << getmin(x,y,k) << "\n";
    }
    return 0;
}