Cod sursa(job #2717527)

Utilizator AACthAirinei Andrei Cristian AACth Data 7 martie 2021 15:49:35
Problema Plantatie Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int n,q;
int rmq[501][32][501];
int mxi;
int ans;
int main()
{
     f>>n>>q;
    int i,j,which;
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            f>>rmq[i][0][j];
    mxi = log2(n);
    for(which=0; which<n; which++)
        for(i = 1; i<=mxi; ++i)
            for(j=0; j + (1<<i) - 1 < n; ++j)
                rmq[which][i][j] = max(rmq[which][i-1][j], rmq[which][i-1][j + (1 << (i-1))]);
    for(i=1;i<=q;i++)
    {
        int xs,ys,dim;
        f>>xs>>ys>>dim;
        --xs,--ys,ans = 0;
        int half_log = log2(dim);
        for(int j = xs; j<=xs + dim - 1; ++j)
            ans = max(ans,max(rmq[j][half_log][ys], rmq[j][half_log][ys + dim - (1<<half_log)]));
        g<<ans<<'\n';
    }

    return 0;
}