Problem p03056 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
// #define int long long
#define lowbit(x) ((x) & (-x))
using namespace std;
const int maxn = 190;
int n, m, sum[maxn][maxn], a[maxn][maxn], dp[maxn][maxn][maxn][20];
char ch[maxn][maxn];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline void write(int a) {
  if (a < 0) {
    putchar('-');
    putchar('1');
  } else {
    if (a >= 10)
      write(a / 10);
    putchar(a % 10 + '0');
  }
}
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    scanf("%s", ch[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (ch[i][j] == '.')
        a[i][j] = 1;
      else
        a[i][j] = 0;
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
    }
  for (int shang = 1; shang <= n; ++shang)
    for (int zuo = 1; zuo <= m; ++zuo)
      for (int you = zuo; you <= m; ++you) {
        int l = shang, r = n, xia = shang - 1;
        while (l <= r) {
          int mid = (l + r) >> 1;
          int geshu = sum[mid][you] - sum[mid][zuo - 1] - sum[shang - 1][you] +
                      sum[shang - 1][zuo - 1];
          if (!geshu || geshu == (mid - shang + 1) * (you - zuo + 1))
            xia = mid, l = mid + 1;
          else
            r = mid - 1;
        }
        dp[shang][zuo][you][0] = xia;
        //				cout<<shang<<' '<<zuo<<' '<<you<<'
        //'<<xia<<endl;
      }
  int k;
  for (k = 1; dp[1][1][m][k - 1] < n; ++k)
    for (int kuan = 1; kuan <= m; ++kuan)
      for (int shang = 1; shang <= n; ++shang)
        for (int zuo = 1, you = zuo + kuan - 1; you <= m; ++zuo, ++you) {
          if (dp[shang][zuo][you][k - 1] == n) {
            dp[shang][zuo][you][k] = n;
            continue;
          }
          dp[shang][zuo][you][k] =
              dp[dp[shang][zuo][you][k - 1] + 1][zuo][you][k - 1];
          if (dp[shang][zuo][you][k] == n)
            continue;
          int l = zuo, r = you - 1, kotae = 0;
          while (l <= r) {
            int mid = (l + r) >> 1;
            int x = dp[shang][zuo][mid][k - 1],
                y = dp[shang][mid + 1][you][k - 1];
            kotae = max(kotae, min(x, y));
            if (x < y)
              r = mid - 1;
            else
              l = mid + 1;
          }
          dp[shang][zuo][you][k] = max(dp[shang][zuo][you][k], kotae);
        }
  write(k - 1);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
// #define int long long
#define lowbit(x) ((x) & (-x))
using namespace std;
const int maxn = 190;
int n, m, sum[maxn][maxn], a[maxn][maxn], dp[maxn][maxn][maxn][15];
char ch[maxn][maxn];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline void write(int a) {
  if (a < 0) {
    putchar('-');
    putchar('1');
  } else {
    if (a >= 10)
      write(a / 10);
    putchar(a % 10 + '0');
  }
}
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    scanf("%s", ch[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      if (ch[i][j] == '.')
        a[i][j] = 1;
      else
        a[i][j] = 0;
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
    }
  for (int shang = 1; shang <= n; ++shang)
    for (int zuo = 1; zuo <= m; ++zuo)
      for (int you = zuo; you <= m; ++you) {
        int l = shang, r = n, xia = shang - 1;
        while (l <= r) {
          int mid = (l + r) >> 1;
          int geshu = sum[mid][you] - sum[mid][zuo - 1] - sum[shang - 1][you] +
                      sum[shang - 1][zuo - 1];
          if (!geshu || geshu == (mid - shang + 1) * (you - zuo + 1))
            xia = mid, l = mid + 1;
          else
            r = mid - 1;
        }
        dp[shang][zuo][you][0] = xia;
        //				cout<<shang<<' '<<zuo<<' '<<you<<'
        //'<<xia<<endl;
      }
  int k;
  for (k = 1; dp[1][1][m][k - 1] < n; ++k)
    for (int kuan = 1; kuan <= m; ++kuan)
      for (int shang = 1; shang <= n; ++shang)
        for (int zuo = 1, you = zuo + kuan - 1; you <= m; ++zuo, ++you) {
          if (dp[shang][zuo][you][k - 1] == n) {
            dp[shang][zuo][you][k] = n;
            continue;
          }
          dp[shang][zuo][you][k] =
              dp[dp[shang][zuo][you][k - 1] + 1][zuo][you][k - 1];
          if (dp[shang][zuo][you][k] == n)
            continue;
          int l = zuo, r = you - 1, kotae = 0;
          while (l <= r) {
            int mid = (l + r) >> 1;
            int x = dp[shang][zuo][mid][k - 1],
                y = dp[shang][mid + 1][you][k - 1];
            kotae = max(kotae, min(x, y));
            if (x < y)
              r = mid - 1;
            else
              l = mid + 1;
          }
          dp[shang][zuo][you][k] = max(dp[shang][zuo][you][k], kotae);
        }
  write(k - 1);
  return 0;
}