Cod sursa(job #3291245)

Utilizator TeodorVTeodorV TeodorV Data 3 aprilie 2025 19:44:50
Problema Plantatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

const string NUMEFISIER="plantatie";
ifstream fin(NUMEFISIER+".in");
ofstream fout(NUMEFISIER+".out");

const int Nmax=505;

int rmq[(int)log2(Nmax)+2][Nmax][Nmax];

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

int n;
void initRmq()
{
    int logn=log2(n)+1;
    for(int exp=1; exp<logn; exp++)
    {
        for(int i=(1<<exp)-1; i<n; i++)
        for(int j=(1<<exp)-1; i<n; j++)
        {
            rmq[exp][i][j]=max(rmq[exp-1][i][j], rmq[exp-1][i-(1<<(exp-1))][j], rmq[exp-1][i][j-(1<<(exp-1))], rmq[exp-1][i-(1<<(exp-1))][j-(1<<(exp-1))]);
        }
    }

}

int query(int i, int j, int k)
{
    static int exp,dif;
    exp=log2(k);
    dif=k-(1<<exp);
    return max(rmq[exp][i][j], rmq[exp][i-dif][j], rmq[exp][i][j-dif], rmq[exp][i-dif][j-dif]);
}

int main()
{
    int m;
    fin>>n>>m;
    for(int i=0; i<n; i++)
    for(int j=0; j<n; j++)
        fin>>rmq[0][i][j];
    initRmq();

    int i,j,k;
    for(int q=1; q<=m; q++)
    {
        fin>>i>>j>>k;
        fout<<query(i+k-2, j+k-2, k)<<'\n';
    }
    return 0;
}