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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+18 + 10;

int h, w, ans = 0;
int map[1405][1405];

int main() {
  std::cin >> h >> w;
  rep(i, h) {
    rep(j, w) std::cin >> map[i][j], map[i][j] = (1 - map[i][j]);
    map[i][w] = 0;
  }

  rep(i, w) {
    REP(j, 1, h) map[j][i] = (map[j][i] == 0 ? 0 : map[j - 1][i] + 1);
  }

  rep(i, h) {
    int max = 0;
    std::stack<P> s;
    rep(j, w + 1) {
      if (s.empty() || s.top().first < map[i][j])
        s.push(P(map[i][j], j));
      else if (s.top().first > map[i][j]) {
        int pos = j;
        while (s.size() && s.top().first >= map[i][j]) {
          P p = s.top();
          s.pop();
          max = std::max(max, p.first * (j - p.second));
          pos = p.second;
        }
        s.push(P(map[i][j], pos));
      }
    }
    ans = std::max(max, ans);
  }

  std::cout << ans << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a) RREP(i, a, 0)
#define RREP(i, a, b) for (int i = a; i >= b; --i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for (ll i = a; i < b; ++i)
#define rrepll(i, a) RREPLL(i, a, 0)
#define RREPLL(i, a, b) for (ll i = a; i >= b; --i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<int, P> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

int h, w;
int map[1401][1401];

int main() {
  std::cin >> h >> w;
  rep(i, h) {
    rep(j, w) {
      std::cin >> map[i][j];
      map[i][j] = (1 - map[i][j]);
    }
  }

  rep(i, h) {
    rrep(j, w - 2) {
      if (map[i][j])
        map[i][j] += map[i][j + 1];
    }
  }

  int ans = 0;

  rep(i, h) {
    rep(j, w) {
      int minEdge = infi;
      REP(k, i, h) {
        if (map[k][j] == 0)
          break;
        minEdge = std::min(minEdge, map[k][j]);
        ans = std::max(ans, (k - i + 1) * minEdge);
      }
    }
  }

  std::cout << ans << std::endl;

  return 0;
}