Cod sursa(job #2097361)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 30 decembrie 2017 23:58:19
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdio>

using namespace std;
FILE *in,*out;
const int logmax = 10;
const int nmax = 1000;
int lg[1+nmax];
int v[1+nmax][1+nmax][1+logmax];
int max(int a,int b)
{
    if(a > b)
        return a;
    return b;
}
int main()
{
    in = fopen("plantatie.in","r");
    out = fopen("plantatie.out","w");
    int n,m;
    fscanf(in,"%d %d",&n,&m);
    for(int i = 2; i <= nmax; i ++)
        lg[i] = lg[i >> 1] + 1;
    for(int i =1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            fscanf(in,"%d",&v[i][j][0]);
    for(int p = 1; p <= lg[n]; p ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                v[i][j][p] = max(v[i+(1<<(p-1))][j][p-1],max( v[i][j+(1<<(p-1))][p-1],max( v[i+(1<<(p-1))][j+(1<<(p-1))][p-1] ,v[i][j][p-1])));
    for(int t = 1; t <= m; t ++)
    {
        int i,j,k;
        fscanf(in,"%d %d %d",&i,&j,&k);
        int sol = max(v[i][j][lg[k]],max(v[i+k-(1 << lg[k])][j][lg[k]], max(v[i][j+k-(1 << lg[k])][lg[k]], v[i+k-(1 << lg[k])][j+k-(1 << lg[k])][lg[k]])));
        fprintf(out,"%d\n",sol);
    }
    return 0;
}