Problem p00116 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-10;

int dp[510][510];
int main() {
  int H, W;
  while (~scanf("%d %d", &H, &W)) {
    if (H == 0 && W == 0)
      break;
    memset(dp, 0, sizeof(dp));

    for (int y = 0; y < H; y++) {
      char line[505];
      scanf("%s", line);
      for (int x = 0; x < W; x++) {
        dp[y + 1][x + 1] = dp[y][x + 1];
        if (line[x] == '.') {
          dp[y + 1][x + 1]++;
        } else {
          dp[y + 1][x + 1] = 0;
        }
      }
    }

    int res = 0;
    for (int y = 0; y < H; y++) {
      priority_queue<P> que;
      for (int x = 0; x <= W; x++) {
        int next = dp[y + 1][x + 1];
        int len = 0;
        while (!que.empty() && que.top().first >= next) {
          len += que.top().second;
          res = max(res, len * que.top().first);
          que.pop();
        }
        len++;
        que.push(P(next, len));
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-10;

int dp[510][510];
int main() {
  int H, W;
  while (~scanf("%d %d", &H, &W)) {
    if (H == 0 && W == 0)
      break;
    char stage[505][505];
    memset(dp, 0, sizeof(dp));

    vector<P> candidates;
    for (int y = 0; y < H; y++) {
      char line[505];
      scanf("%s", line);
      for (int x = 0; x < W; x++) {
        stage[y][x] = line[x];
        dp[y + 1][x + 1] = dp[y][x + 1] + dp[y + 1][x] - dp[y][x];
        if (line[x] == '.') {
          dp[y + 1][x + 1]++;
          candidates.push_back(P(x, y));
        }
      }
    }

    int res = 0;
    for (int i = 0; i < candidates.size(); i++) {
      int lx = candidates[i].first + 1;
      int ly = candidates[i].second + 1;
      for (int ry = ly; ry <= H; ry++) {
        int lhs = lx - 1;
        int rhs = W + 1;
        bool isok = false;
        for (int round = 0; round < 10; round++) {
          int mid = (lhs + rhs) / 2;
          int count = dp[ry][mid] - dp[ly - 1][mid] - dp[ry][lx - 1] +
                      dp[ly - 1][lx - 1];
          if (count == (ry - ly + 1) * (mid - lx + 1)) {
            lhs = mid;
            isok = true;
          } else {
            rhs = mid;
          }
        }

        if (isok) {
          res = max(dp[ry][rhs - 1] - dp[ly - 1][rhs - 1] - dp[ry][lx - 1] +
                        dp[ly - 1][lx - 1],
                    res);
        }
      }
    }
    printf("%d\\n", res);
  }
}