Language: C++


#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

constexpr int d_max = 100;

int main() {
  int A, B;
  std::cin >> A >> B;
  std::vector<std::vector<int>> dist(A, std::vector<int>(B));
  for (auto &vec: dist) {
    for (auto &x: vec) {
      std::cin >> x;
    }
  }
  for (int i: range(0, A)) {
    int dif = d_max;
    for (int j: range(1, B)) {
      int tmp = dist[i][j] - dist[i][j - 1];
      if (tmp < 0 || tmp > dif) {
        std::cout << "Impossible\n";
        return 0;
      }
      dif = tmp;
    }
  }
  for (int j: range(0, B)) {
    int dif = d_max;
    for (int i: range(1, A)) {
      int tmp = dist[i][j] - dist[i - 1][j];
      if (tmp < 0 || tmp > dif) {
        std::cout << "Impossible\n";
        return 0;
      }
      dif = tmp;
    }
  }
  std::vector<std::vector<int>> lb_A(A, std::vector<int>(B, 0));
  std::vector<std::vector<int>> lb_B(A, std::vector<int>(B, 0));
  std::vector<std::vector<int>> ub_A(A, std::vector<int>(B, d_max));
  std::vector<std::vector<int>> ub_B(A, std::vector<int>(B, d_max));
  for (int i: range(0, A)) {
    for (int j: range(0, B)) {
      if (i > 0) {
        chmax(lb_A[i - 1][j], dist[i][j] - dist[i - 1][j]);
        chmin(ub_A[i][j], dist[i][j] - dist[i - 1][j]);
      }
      if (j > 0) {
        chmax(lb_B[i][j - 1], dist[i][j] - dist[i][j - 1]);
        chmin(ub_B[i][j], dist[i][j] - dist[i][j - 1]);
      }
    }
  }
  int N = 2 * (d_max + 1);
  std::array<std::array<int, d_max + 1>, d_max + 1> length{};
  for (auto &vec: length) {
    for (auto &x: vec) {
      x = -1;
    }
  }
  for (int i: range(0, A)) {
    for (int j: range(0, B)) {
      if (![&] {
        for (int a: range(lb_A[i][j], ub_A[i][j] + 1)) {
          for (int b: range(lb_B[i][j], ub_B[i][j] + 1)) {
            int rem = dist[i][j] - (i + 1) * a - (j + 1) * b;
            if (0 <= rem && rem <= d_max) {
              length[a][b] = rem;
              return true;
            }
          }
        }
        return false;
      }()) {
        std::cout << "Impossible1\n";
        return 0;
      }
    }
  }
  int M = 2 * d_max;
  for (const auto &vec: length) {
    for (auto x: vec) {
      if (x != -1) {
        ++M;
      }
    }
  }
  std::cout << N << ' ' << M << '\n';
  for (int i: range(1, N / 2)) {
    std::cout << i << ' ' << i + 1 << " X\n";
  }
  for (int i: range(N / 2 + 1, N)) {
    std::cout << i << ' ' << i + 1 << " Y\n";
  }
  for (int i: range(0, d_max + 1)) {
    for (int j: range(0, d_max + 1)) {
      if (length[i][j] != -1) {
        std::cout << i + 1 << ' ' << N - j << ' ' << length[i][j] << '\n';
      }
    }
  }
  std::cout << "1 " << N << '\n';
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:106:53: error: variable 'std::array<std::array<int, 101>, 101> length' has initializer but incomplete type
  106 |   std::array<std::array<int, d_max + 1>, d_max + 1> length{};
      |                                                     ^~~~~~
a.cc:7:1: note: 'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'
    6 | #include <numeric>
  +++ |+#include <array>
    7 |