Problem p03056 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cout << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int MX = 190;
const int B = 18;

int r[MX][MX];
string s[MX];
int tor[B][MX][MX][MX], tou[B][MX][MX][MX];

int main() {
  int h, w;
  cin >> h >> w;

  rep(i, h) {
    cin >> s[i];
    rep(j, w) {
      r[i + 1][j + 1] = r[i][j + 1] + r[i + 1][j] - r[i][j];
      if (s[i][j] == '#') {
        ++r[i + 1][j + 1];
      }
    }
  }

  memset(tor, -1, sizeof(tor));
  memset(tou, -1, sizeof(tou));

  auto get = [&](int a, int b, int c, int d) {
    int num = r[c + 1][d + 1] - r[a][d + 1] - r[c + 1][b] + r[a][b];
    return num == 0 || num == (c - a + 1) * (d - b + 1);
  };

  for (int i = 0; i < h; ++i) {
    for (int j = i; j < h; ++j) {
      int l = -1;
      for (int k = 0; k < w; ++k) {
        chmax(l, k - 1);
        while (l + 1 < w) {
          if (get(i, k, j, l + 1)) {
            ++l;
          } else {
            break;
          }
        }
        tor[0][i][j][k] = l;
      }
    }
  }

  for (int k = 0; k < w; ++k) {
    for (int l = k; l < w; ++l) {
      int j = -1;
      for (int i = 0; i < h; ++i) {
        chmax(j, i - 1);
        while (j + 1 < h) {
          if (get(i, k, j + 1, l)) {
            ++j;
          } else {
            break;
          }
        }
        tou[0][k][l][i] = j;
      }
    }
  }

  for (int t = 1; t < B; ++t) {
    if (tor[t - 1][0][h - 1][0] == w - 1) {
      cout << t - 1 << endl;
      return 0;
    }

    for (int i = 0; i < h; ++i) {
      for (int j = i; j < h; ++j) {
        int r = -1;

        for (int k = 0; k < w; ++k) {
          chmax(tor[t][i][j][k], tor[t - 1][i][j][k]);
          if (tor[t - 1][i][j][k] >= k) {
            chmax(tor[t][i][j][k], tor[t - 1][i][j][tor[t - 1][i][j][k] + 1]);
          }

          chmax(r, k - 1);

          while (r + 1 < w) {
            if (tou[t - 1][k][r + 1][i] >= j ||
                (tou[t - 1][k][r + 1][i] >= i &&
                 tou[t - 1][k][r + 1][tou[t - 1][k][r + 1][i] + 1] >= j)) {
              ++r;
            } else {
              break;
            }
          }
          chmax(tor[t][i][j][k], r);
        }
      }
    }

    for (int k = 0; k < w; ++k) {
      for (int l = k; l < w; ++l) {
        int u = -1;

        for (int i = 0; i < h; ++i) {
          chmax(tou[t][k][l][i], tou[t - 1][k][l][i]);
          if (tou[t - 1][k][l][i] >= i) {
            chmax(tou[t][k][l][i], tou[t - 1][k][l][tou[t - 1][k][l][i] + 1]);
          }

          chmax(u, i - 1);

          while (u + 1 < h) {
            if (tor[t - 1][i][u + 1][k] >= l ||
                (tor[t - 1][i][u + 1][k] >= k &&
                 tor[t - 1][i][u + 1][tor[t - 1][i][u + 1][k] + 1] >= l)) {
              ++u;
            } else {
              break;
            }
          }
          chmax(tou[t][k][l][i], u);
        }
      }
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cout << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int MX = 190;
const int B = 18;

int r[MX][MX];
string s[MX];
short tor[B][MX][MX][MX], tou[B][MX][MX][MX];

int main() {
  int h, w;
  cin >> h >> w;

  rep(i, h) {
    cin >> s[i];
    rep(j, w) {
      r[i + 1][j + 1] = r[i][j + 1] + r[i + 1][j] - r[i][j];
      if (s[i][j] == '#') {
        ++r[i + 1][j + 1];
      }
    }
  }

  memset(tor, -1, sizeof(tor));
  memset(tou, -1, sizeof(tou));

  auto get = [&](int a, int b, int c, int d) {
    int num = r[c + 1][d + 1] - r[a][d + 1] - r[c + 1][b] + r[a][b];
    return num == 0 || num == (c - a + 1) * (d - b + 1);
  };

  for (int i = 0; i < h; ++i) {
    for (int j = i; j < h; ++j) {
      int l = -1;
      for (int k = 0; k < w; ++k) {
        chmax(l, k - 1);
        while (l + 1 < w) {
          if (get(i, k, j, l + 1)) {
            ++l;
          } else {
            break;
          }
        }
        tor[0][i][j][k] = l;
      }
    }
  }

  for (int k = 0; k < w; ++k) {
    for (int l = k; l < w; ++l) {
      int j = -1;
      for (int i = 0; i < h; ++i) {
        chmax(j, i - 1);
        while (j + 1 < h) {
          if (get(i, k, j + 1, l)) {
            ++j;
          } else {
            break;
          }
        }
        tou[0][k][l][i] = j;
      }
    }
  }

  for (int t = 1; t < B; ++t) {
    if (tor[t - 1][0][h - 1][0] == w - 1) {
      cout << t - 1 << endl;
      return 0;
    }

    for (int i = 0; i < h; ++i) {
      for (int j = i; j < h; ++j) {
        int r = -1;

        for (int k = 0; k < w; ++k) {
          chmax(tor[t][i][j][k], tor[t - 1][i][j][k]);
          if (tor[t - 1][i][j][k] >= k) {
            chmax(tor[t][i][j][k], tor[t - 1][i][j][tor[t - 1][i][j][k] + 1]);
          }

          chmax(r, k - 1);

          while (r + 1 < w) {
            if (tou[t - 1][k][r + 1][i] >= j ||
                (tou[t - 1][k][r + 1][i] >= i &&
                 tou[t - 1][k][r + 1][tou[t - 1][k][r + 1][i] + 1] >= j)) {
              ++r;
            } else {
              break;
            }
          }
          chmax(tor[t][i][j][k], r);
        }
      }
    }

    for (int k = 0; k < w; ++k) {
      for (int l = k; l < w; ++l) {
        int u = -1;

        for (int i = 0; i < h; ++i) {
          chmax(tou[t][k][l][i], tou[t - 1][k][l][i]);
          if (tou[t - 1][k][l][i] >= i) {
            chmax(tou[t][k][l][i], tou[t - 1][k][l][tou[t - 1][k][l][i] + 1]);
          }

          chmax(u, i - 1);

          while (u + 1 < h) {
            if (tor[t - 1][i][u + 1][k] >= l ||
                (tor[t - 1][i][u + 1][k] >= k &&
                 tor[t - 1][i][u + 1][tor[t - 1][i][u + 1][k] + 1] >= l)) {
              ++u;
            } else {
              break;
            }
          }
          chmax(tou[t][k][l][i], u);
        }
      }
    }
  }

  return 0;
}