Cod sursa(job #1870719)

Utilizator topala.andreiTopala Andrei topala.andrei Data 6 februarie 2017 20:57:15
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("plantatie.in");
ofstream g("plantatie.out");
const int maxn=505;
int N,M,plant[maxn][maxn],rmq[maxn][maxn][20],lg[maxn];
int main()
{
    int i,j,k,k2,x,y,l,p;
    f>>N>>M;
    for (i=1;i<=N;i++)
        for (j=1;j<=N;j++)
        {
            f>>plant[i][j];
            rmq[i][j][0]=plant[i][j];
        }
    lg[1]=0;
    for (i=2;i<=N;i++)
        lg[i]=lg[i/2]+1;
    for (k=1; (1 << k) <=N;k++)
    {
        for (i=1;i <= N - (1 << k)+1;i++)
        {
            for (j=1;j <= N - (1 << k)+1;j++)
            {
                k2=1<<(k-1);
                rmq[i][j][k]= max(max (rmq[i][j][k-1] , rmq[i][j+k2][k-1] ), max(rmq[i+k2][j][k-1],rmq[i+k2][j+k2][k-1]));
            }
        }
    }
    for (i=1;i<=M;i++)
    {
        f>>x>>y>>l;
        p=lg[l];
        g<<max(max(rmq[x][y][p],rmq[x][y+l-(1<<p)][p]),max(rmq[x+l-(1<<p)][y][p],rmq[x+l-(1<<p)][y+l-(1<<p)][p]))<<'\n';

    }
}