Cod sursa(job #2628215)

Utilizator sebastianp2003Popa Sebastian sebastianp2003 Data 15 iunie 2020 01:15:21
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int n, m;
vector<vector<vector<int>>> v;
int main()
{
    f >> n >> m;
    v.resize(1 + log2(n));
    v[0].resize(n, vector<int>(n));
    for (auto &i : v[0])
        for (auto &j : i)
            f >> j;
    for (int k = 1; k < (int)v.size(); k++)
    {
        int dim = (1 << k), bd = (1 << (k - 1));
        int req = n - dim + 1;
        v[k].resize(req, vector<int>(req));
        for (int i = 0; i < req; i++)
            for (int j = 0; j < req; j++)
            {
                int c1, c2, c3, c4;
                c1 = v[k - 1][i][j];
                c2 = v[k - 1][i][j + bd];
                c3 = v[k - 1][i + bd][j];
                c4 = v[k - 1][i + bd][j + bd];
                int a1 = max(c1, c2);
                int a2 = max(c3, c4);
                int ax = max(a1, a2);
                v[k][i][j] = ax;
            }
    }
    for (int x, y, k; m; m--)
    {
        f >> x >> y >> k;
        if (__builtin_popcount(k) == 1)
            g << v[log2(k)][x - 1][y - 1] << '\n';
        else
        {
            x--;
            y--;
            int ax = log2(k);
            int c1 = v[ax][x][y];
            int c2 = v[ax][x][y + k - (1 << ax)];
            int c3 = v[ax][x + k - (1 << ax)][y];
            int c4 = v[ax][x + k - (1 << ax)][y + k - (1 << ax)];
            int a1 = max(c1, c2);
            int a2 = max(c3, c4);
            int af = max(a1, a2);
            g << af << '\n';
        }
    }
    return 0;
}