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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define YNUM(X) (X < 0 ? X + 3 : X)

typedef std::pair<int, int> WH;
// typedef std::vector<WH> whvec;

int h, w;
bool map[502][502];
std::string in;
// std::vector<std::vector<whvec> > data(3, std::vector<whvec>(502));
int data[3][502][502];
int data_max = 0;
int height_sum[502][502], width_sum[502][502];
int data_pt[3][502];
WH wh;

int main() {
  while (std::cin >> h >> w, w + h != 0) {
    data_max = 0;

    memset(data_pt, 0, sizeof(data_pt));
    memset(height_sum, 0, sizeof(height_sum));
    memset(width_sum, 0, sizeof(width_sum));

    for (int i = 1; i <= h; ++i) {
      std::cin >> in;
      for (int l = 1; l <= w; ++l) {
        map[i][l] = (in[l - 1] == '.');
        if (map[i][l]) {
          width_sum[i][l] = width_sum[i][l - 1] + 1;
          height_sum[i][l] = height_sum[i - 1][l] + 1;
        }
      }
    }

    for (int i = 1, n = 1; n <= h; n++, i = n % 3) {
      for (int l = 1; l <= w; l++) {
        // data[i][l] = 0;
        memset(data[i][l], 0, sizeof(int) * 502);
        if (map[n][l]) {
          if (map[n - 1][l] && map[n][l - 1]) {
            int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l];

            for (int m = 0; m < 502; ++m) {
              if (data[YNUM(i - 1)][l][m]) {
                data[i][l][std::min(m, x_cnt)] = data[YNUM(i - 1)][l][m] + 1;
                data_max = std::max(data_max, wh.first * wh.second);
              }
            }
            for (int m = 0; m < 502; ++m) {
              if (data[i][l - 1][m]) {
                data[i][l][m + 1] = std::min(data[i][l - 1][m], y_cnt);
                data_max = std::max(data_max, (m + 1) * data[i][l][m + 1]);
              }
            }

          } else if (map[n - 1][l]) {
            for (int m = 0; m < 502; ++m) {
              if (data[YNUM(i - 1)][l]) {
                data[i][l][1] =
                    std::max(data[i][l][1], data[YNUM(i - 1)][l][m] + 1);
                data_max = std::max(data_max, wh.first * wh.second);
              }
            }

          } else if (map[n][l - 1]) {
            for (int m = 1; m < 502; ++m) {
              if (data[i][l - 1][m]) {
                data[i][l][m + 1] = std::max(data[i][l][m + 1], 1);
                data_max = std::max(data_max, (m + 1) * data[i][l][1]);
              }
            }

          } else
            data[i][l][1] = std::max(data[i][l][1], 1),
            data_max = std::max(data_max, data[i][l][1]);
        }
      }
    }

    std::cout << data_max << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define YNUM(X) (X < 0 ? X + 3 : X)

typedef std::pair<int, int> WH;
// typedef std::vector<WH> whvec;

int h, w;
bool map[502][502];
std::string in;
// std::vector<std::vector<whvec> > data(3, std::vector<whvec>(502));
WH data[3][502][500];
int data_max = 0;
int height_sum[502][502], width_sum[502][502];
int data_pt[3][502];
WH wh;

int main() {
  while (std::cin >> h >> w, w + h != 0) {
    data_max = 0;

    memset(data_pt, 0, sizeof(data_pt));
    memset(height_sum, 0, sizeof(height_sum));
    memset(width_sum, 0, sizeof(width_sum));

    for (int i = 1; i <= h; ++i) {
      std::cin >> in;
      for (int l = 1; l <= w; ++l) {
        map[i][l] = (in[l - 1] == '.');
        if (map[i][l]) {
          width_sum[i][l] = width_sum[i][l - 1] + 1;
          height_sum[i][l] = height_sum[i - 1][l] + 1;
        }
      }
    }

    for (int i = 1, n = 1; n <= h; n++, i = n % 3) {
      for (int l = 1; l <= w; l++) {
        data_pt[i][l] = 0;

        if (map[n][l]) {
          if (map[n - 1][l] && map[n][l - 1]) {
            int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l];

            for (int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m) {
              wh = WH(std::min(data[YNUM(i - 1)][l][m].first, x_cnt),
                      data[YNUM(i - 1)][l][m].second + 1);
              data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++;
              data_max = std::max(data_max, wh.first * wh.second);
            }
            for (int m = 0; m < data_pt[i][l - 1]; ++m) {
              wh = WH(data[i][l - 1][m].first + 1,
                      std::min(data[i][l - 1][m].second, y_cnt));

              if (std::find(data[i][l], data[i][l] + data_pt[i][l], wh) ==
                  data[i][l] + data_pt[i][l]) {
                data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++;
                data_max = std::max(data_max, wh.first * wh.second);
              }
            }

          } else if (map[n - 1][l]) {
            for (int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m) {
              data[i][l][data_pt[i][l]] =
                  (wh = WH(1, data[YNUM(i - 1)][l][m].second + 1)),
              data_pt[i][l]++;
              data_max = std::max(data_max, wh.first * wh.second);
            }

          } else if (map[n][l - 1]) {
            for (int m = 0; m < data_pt[i][l - 1]; ++m) {
              data[i][l][data_pt[i][l]] =
                  (wh = WH(data[i][l - 1][m].first + 1, 1)),
              data_pt[i][l]++;
              data_max = std::max(data_max, wh.first * wh.second);
            }

          } else
            data[i][l][data_pt[i][l]] = (wh = WH(1, 1)), data_pt[i][l]++,
            data_max = std::max(data_max, wh.first * wh.second);
        }
      }
    }

    std::cout << data_max << std::endl;
  }
  return 0;
}