Cod sursa(job #3232332)

Utilizator TonyC205Chirilus Antonie TonyC205 Data 29 mai 2024 23:00:36
Problema Plantatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>

using namespace std;


int Max(int a, int b, int c, int d)
{
  return max(max(a,b), max(c,d));
}

const int N = 501;
int log2_table[N];
int plantation[N][N][9];
int n,q;
int main()
{
    ifstream fin("plantatie.in");
    ofstream fout("plantatie.out");
  
  for (int i = 2; i < N; i++)
  {
    log2_table[i] = 1 + log2_table[i / 2];
  }

  fin >> n >> q;
 

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


  for (int k = 1; (1 << k) <= n; k++)
  {
    for (int i = 1; i + (1 << k) - 1 <= n; i++)
    {
      for (int j = 1; j + (1 << k) - 1 <= n; j++)
      {
        plantation[i][j][k] = Max(plantation[i][j][k - 1], plantation[i][j + (1 << (k - 1))][k - 1], plantation[i + (1 << (k - 1))][j][k - 1], plantation[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1]);
      }
    }
  }

    while (q--)
   {
   int i, j, k;
   cin >> i >> j >> k;
   k = log2_table[k];
   fout << Max(plantation[i][j][k], plantation[i][j + (1 << k) - 1][k], plantation[i + (1 << k) - 1][j][k], plantation[i + (1 << k) - 1][j + (1 << k) - 1][k]) << endl;
   }
return 0;
}