Cod sursa(job #3228690)

Utilizator petrutgGheorghies Petrut-Rares petrutg Data 10 mai 2024 05:09:21
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>
#define N 505
using namespace std;
ifstream fin("plantatie.in");
ofstream fout("plantatie.out");
int n,m,a[N][N],rmq[N][N][10];
int main()
{
    int i,j,k;
    fin>>n>>m;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
         fin>>a[i][j];
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
         rmq[i][j][0]=a[i][j];
    for(k=1;k<=log2(n);k++)
        for(i=1;i+(1<<k)-1<=n;i++)
         for(j=1;j+(1<<k)-1<=n;j++)
           {
               int M[5];
               M[1]=rmq[i][j][k-1];
               M[2]=rmq[i][j+(1<<(k-1))][k-1];
               M[3]=rmq[i+(1<<(k-1))][j][k-1];
               M[4]=rmq[i+(1<<(k-1))][j+(1<<(k-1))][k-1];
               rmq[i][j][k]=*max_element(M+1,M+5);
           }
    while(m--)
    {
        fin>>i>>j>>k;
        int M[5],lg=log2(k);
        M[1]=rmq[i][j][lg];
        M[2]=rmq[i][j+k-(1<<lg)][lg];
        M[3]=rmq[i+k-(1<<lg)][j][lg];
        M[4]=rmq[i+k-(1<<lg)][j+k-(1<<lg)][lg];
        fout<<*max_element(M+1,M+5)<<"\n";
    }
    return 0;
}