Problem p03056 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int unsigned short
using namespace std;
#define gc getchar
inline int read() {
  register int x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = gc();
  }
  return (f == 1) ? x : -x;
}
int n, m, a[188][188], sum[188][188];
inline bool ok(int _x1, int _y1, int _x2, int _y2) {
  int xx = sum[_x2][_y2] - sum[_x2][_y1 - 1] - sum[_x1 - 1][_y2] +
           sum[_x1 - 1][_y1 - 1];
  return (xx == 0 || xx == (_x2 - _x1 + 1) * (_y2 - _y1 + 1));
}
int R[17][188][188][188], D[17][188][188][188];
inline void chkmax(int &a, const int &b) {
  if (a < b)
    a = b;
}
signed main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char ch = gc();
      while (ch != '.' && ch != '#')
        ch = gc();
      a[i][j] = (ch == '.' ? 0 : 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      int las = -1;
      for (int k = m; k; k--) {
        if (ok(i, k, j, k)) {
          if (las == -1 || !ok(i, k, j, las))
            las = k;
        } else
          las = -1;
        R[0][i][j][k] = las;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = i; j <= m; j++) {
      int las = -1;
      for (int k = n; k; k--) {
        if (ok(k, i, k, j)) {
          if (las == -1 || !ok(k, i, las, j))
            las = k;
        } else
          las = -1;
        D[0][i][j][k] = las;
      }
    }
  }
  //
  if (ok(1, 1, n, m)) {
    puts("0");
    return 0;
  }
  for (int as = 1;; as++) {
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        for (int k = 1; k <= m; k++) {
          if (R[as - 1][i][j][k] == m)
            R[as][i][j][k] = m;
          else if (R[as - 1][i][j][k] == -1)
            R[as][i][j][k] = -1;
          R[as][i][j][k] =
              max(R[as - 1][i][j][k], R[as - 1][i][j][R[as - 1][i][j][k] + 1]);
        }
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = i; j <= m; j++) {
        for (int k = 1; k <= n; k++) {
          if (D[as - 1][i][j][k] == n)
            D[as][i][j][k] = n;
          else if (D[as - 1][i][j][k] == -1)
            D[as][i][j][k] = -1;
          D[as][i][j][k] =
              max(D[as - 1][i][j][k], D[as - 1][i][j][D[as - 1][i][j][k] + 1]);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        int las = m + 1;
        for (int k = m; k; k--) {
          for (; las > k; las--) {
            int nxt = D[as - 1][k][las - 1][i];
            if (nxt == -1)
              continue;
            if (nxt < j) {
              if (D[as - 1][k][las - 1][nxt + 1] >= j)
                break;
            } else
              break;
          }
          if (las != k)
            chkmax(R[as][i][j][k], las - 1);
        }
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = i; j <= m; j++) {
        int las = n + 1;
        for (int k = n; k; k--) {
          for (; las > k; las--) {
            int nxt = R[as - 1][k][las - 1][i];
            if (nxt == -1)
              continue;
            if (nxt < j) {
              if (R[as - 1][k][las - 1][nxt + 1] >= j)
                break;
            } else
              break;
          }
          if (las != k)
            chkmax(D[as][i][j][k], las - 1);
        }
      }
    }
    if (R[as][1][n][1] == m) {
      cout << as;
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar
inline int read() {
  register int x = 0, f = 1;
  char ch = gc();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = gc();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = gc();
  }
  return (f == 1) ? x : -x;
}
int n, m, a[188][188], sum[188][188];
inline bool ok(int _x1, int _y1, int _x2, int _y2) {
  int xx = sum[_x2][_y2] - sum[_x2][_y1 - 1] - sum[_x1 - 1][_y2] +
           sum[_x1 - 1][_y1 - 1];
  return (xx == 0 || xx == (_x2 - _x1 + 1) * (_y2 - _y1 + 1));
}
int R[17][188][188][188], D[17][188][188][188];
inline void chkmax(int &a, const int &b) {
  if (a < b)
    a = b;
}
signed main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char ch = gc();
      while (ch != '.' && ch != '#')
        ch = gc();
      a[i][j] = (ch == '.' ? 0 : 1);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      int las = -1;
      for (int k = m; k; k--) {
        if (ok(i, k, j, k)) {
          if (las == -1 || !ok(i, k, j, las))
            las = k;
        } else
          las = -1;
        R[0][i][j][k] = las;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = i; j <= m; j++) {
      int las = -1;
      for (int k = n; k; k--) {
        if (ok(k, i, k, j)) {
          if (las == -1 || !ok(k, i, las, j))
            las = k;
        } else
          las = -1;
        D[0][i][j][k] = las;
      }
    }
  }
  //
  if (ok(1, 1, n, m)) {
    puts("0");
    return 0;
  }
  for (int as = 1;; as++) {
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        for (int k = 1; k <= m; k++) {
          if (R[as - 1][i][j][k] == m)
            R[as][i][j][k] = m;
          else if (R[as - 1][i][j][k] == -1)
            R[as][i][j][k] = -1;
          R[as][i][j][k] =
              max(R[as - 1][i][j][k], R[as - 1][i][j][R[as - 1][i][j][k] + 1]);
        }
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = i; j <= m; j++) {
        for (int k = 1; k <= n; k++) {
          if (D[as - 1][i][j][k] == n)
            D[as][i][j][k] = n;
          else if (D[as - 1][i][j][k] == -1)
            D[as][i][j][k] = -1;
          D[as][i][j][k] =
              max(D[as - 1][i][j][k], D[as - 1][i][j][D[as - 1][i][j][k] + 1]);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        int las = m + 1;
        for (int k = m; k; k--) {
          for (; las > k; las--) {
            int nxt = D[as - 1][k][las - 1][i];
            if (nxt == -1)
              continue;
            if (nxt < j) {
              if (D[as - 1][k][las - 1][nxt + 1] >= j)
                break;
            } else
              break;
          }
          if (las != k)
            chkmax(R[as][i][j][k], las - 1);
        }
      }
    }
    for (int i = 1; i <= m; i++) {
      for (int j = i; j <= m; j++) {
        int las = n + 1;
        for (int k = n; k; k--) {
          for (; las > k; las--) {
            int nxt = R[as - 1][k][las - 1][i];
            if (nxt == -1)
              continue;
            if (nxt < j) {
              if (R[as - 1][k][las - 1][nxt + 1] >= j)
                break;
            } else
              break;
          }
          if (las != k)
            chkmax(D[as][i][j][k], las - 1);
        }
      }
    }
    if (R[as][1][n][1] == m) {
      cout << as;
      return 0;
    }
  }
  return 0;
}