Cod sursa(job #3230514)

Utilizator izabelamariaJilavu Izabela izabelamaria Data 21 mai 2024 20:21:57
Problema Plantatie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <iostream>
#include <fstream>
#define NMAX 501
#define LOGN 11

using namespace std;

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

int n, m, lg[NMAX], plantatie[NMAX][NMAX][LOGN];

void computelg() {
  lg[1] = 0;
  for (int i = 2; i < NMAX; i++) 
    lg[i] = lg[i << 1] + 1;
}


int main() {
  fin >> n >> m;

  computelg();

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      fin >> plantatie[i][j][0];

  for (int p = 1; (1 << p) <= n; p++)  {
    for (int i = 1; i + (1 << p) - 1 <= n; i++)
        for (int j = 1; j + (1 << p) - 1 <= n; j++)
          plantatie[i][j][p] = max(max(plantatie[i][j][p - 1], plantatie[i + (1 << (p - 1))][j][p - 1]),
                max(plantatie[i][j + (1 << (p - 1))][p - 1], plantatie[i + (1 << (p - 1))][j + (1 << (p - 1))][p - 1]));
  }


  for (int q = 1; q <= m; q++) {
    int x, y, k;
    fin >> x >> y >> k;

    fout << max(max(plantatie[x][y][lg[k]], plantatie[x + k - (1 << lg[k])][y][lg[k]]), max(plantatie[x][y + k - (1 << lg[k])][lg[k]], plantatie[x + k - (1 << lg[k])][y + k -(1 << lg[k])][lg[k]])) << '\n';
  }
}