Cod sursa(job #2015512)

Utilizator GoogalAbabei Daniel Googal Data 26 august 2017 15:10:21
Problema Plantatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

const int NMAX = 500;
const int LOGMAX = 10;
const int DIM = 1000000;

int n, m, pos;
int dp[1 + NMAX][1 + NMAX][10 + LOGMAX];
char buff[DIM];

void read(int &no){
  no = 0;
  while(!isdigit(buff[pos])){
    if(++pos == DIM){
      in.read(buff, DIM);
      pos = 0;
    }
  }

  while(isdigit(buff[pos])){
    no = (no * 10) + (buff[pos] - '0');
    if(++pos == DIM){
      in.read(buff, DIM);
      pos = 0;
    }
  }
}

int main()
{
  //in >> n >> m;
  read(n);
  read(m);
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      //in >> dp[i][j][0];
      read(dp[i][j][0]);
    }
  }

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

  for(int i = 1; i <= m; i++){
    int x, y, z, lz, result;
    //in >> x >> y >> z;
    read(x);
    read(y);
    read(z);
    lz = log2(z);

    result = dp[x][y][lz];
    result = max(result, dp[x][y + z - (1 << lz)][lz]);
    result = max(result, dp[x + z - (1 << lz)][y][lz]);
    result = max(result, dp[x + z - (1 << lz)][y + z - (1 << lz)][lz]);

    out << result << '\n';
  }
  in.close();
  out.close();
  return 0;
}