Cod sursa(job #2754435)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 25 mai 2021 20:13:07
Problema Plantatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("plantatie.in");
ofstream out("plantatie.out");

int n,m,a[505][505],r[20][505][505],ln[505];

int main()
{
    int i,j,q,x,y,k,a1,b1;
    in >> n >> m;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            in >> a[i][j];
    ln[1] = 0;
    for (i = 2; i <= n; i++)
        ln[i] = ln[i / 2] + 1;
    for (j = 1; j <= n; j++)
        for (q = 1; q <= n; q++)
            r[0][j][q] = a[j][q];
    for (q = 1; q <= ln[n]; q++)
        for (i = 1 << q; i <= n; i++)
            for (j = 1 << q; j <= n; j++)
                r[q][i][j] = max(max(r[q - 1][i][j],r[q - 1][i][j - (1 << (q - 1))]),max(r[q - 1][i - (1 << (q - 1))][j],r[q - 1][i - (1 << (q - 1))][j - (1 << (q - 1))]));
    for (i = 1; i <= m; i++)
    {
        in >> x >> y >> k;
        q = ln[k];
        a1 = x + k - 1;
        b1 = y + k - 1;
        out << max(max(r[q][a1][b1],r[q][a1][y + (1 << q) - 1]),max(r[q][x + (1 << q) - 1][b1],r[q][x + (1 << q) - 1][y + (1 << q) - 1])) << '\n';
    }
    return 0;
}
/*
a1,b1

a1,b1 - k + 2^q
a1 - k + 2^q,b1




r[q][x + (1 << q) - 1][y + (1 << q) - 1]
r[q][x + 2^q - 1][y + k]
r[q][x + k][y + 2^q]
r[q][x + k - 1][y + k -1]
*/