Cod sursa(job #3242827)

Utilizator vvalentinCiun Valentin vvalentin Data 14 septembrie 2024 11:22:35
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 FILENAME = "date";
ifstream fin(FILENAME+".in");
ofstream fout(FILENAME+".out");

const int N_MAX = 500;
long long int n, q;
long long int rmq[17][N_MAX+2][N_MAX+2];

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

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

    for(int pw=1; (1<<pw)<=n; pw++)
    {
        for(int i=1; i<=n-(1<<pw)+1; i++)
        {
            for(int j=1; j<=n-(1<<pw)+1; j++)
            {
                rmq[pw][i][j] = max4(rmq[pw-1][i][j], rmq[pw-1][i+(1<<(pw-1))][j], rmq[pw-1][i][j+(1<<(pw-1))], rmq[pw-1][i+(1<<(pw-1))][j+(1<<(pw-1))]);
            }
        }
    }

    for(int i=1; i<=q; i++)
    {
        int sqI, sqJ, sqLg;
        fin >> sqI >> sqJ >> sqLg;
        int pw = 31-__builtin_clz(sqLg);

        fout << max4(rmq[pw][sqI][sqJ], rmq[pw][sqI][sqJ+sqLg-(1<<pw)], rmq[pw][sqI+sqLg-(1<<pw)][sqJ], rmq[pw][sqI+sqLg-(1<<pw)][sqJ+sqLg-(1<<pw)]) << '\n';
    }

    return 0;
}