Cod sursa(job #2861694)

Utilizator DooMeDCristian Alexutan DooMeD Data 4 martie 2022 11:56:44
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>
using namespace std;
const int nmax = 500;
const int pmax = 9;

int rmq[nmax+5][nmax+5][pmax+5];
int gl[nmax+5];

int main() {
    ifstream f("plantatie.in");
    ofstream g("plantatie.out");

    int n,q; f >> n >> q;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++) {
            int x; f >> x;
            rmq[i][j][0] = x;
        }

    for(int p=1; (1<<p)<=n; p++)
        for(int i=1; i<=n-(1<<p)+1; i++)
            for(int j=1; j<=n-(1<<p)+1; j++) {
                int half = 1<<(p-1);
                rmq[i][j][p] = max(max(rmq[i][j][p-1], rmq[i][j+half][p-1]) , max(rmq[i+half][j][p-1], rmq[i+half][j+half][p-1]));
                //cout << i << " " << j << " " << i+half << " " << j+half << "\n";
            }
    for(int i=2; i<=n; i++) gl[i] = gl[i/2] + 1;

    for(int i=1; i<=q; i++) {
        int x,y,l; f >> x >> y >> l;
        int lx = x + l - 1;
        int ly = y + l - 1;
        int ans = max(max(rmq[x][y][gl[l]], rmq[x][ly-(1<<gl[l])+1][gl[l]]), max(rmq[lx-(1<<gl[l])+1][y][gl[l]], rmq[lx-(1<<gl[l])+1][ly-(1<<gl[l])+1][gl[l]]));
        g << ans << "\n";
    }
    return 0;
}