Problem p02327 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1000000000 + 7;
const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int h, w;
vector<vector<int>> grid(1005, vector<int>(1005));

struct Rectangle {
  int height, pos;
};

int largest_hist(int size, vector<int> &hist) {
  stack<Rectangle> s;
  int maxv = 0;
  hist[size] = 0;

  for (int i = 0; i <= size; i++) {
    Rectangle rect;
    rect.height = hist[i];
    rect.pos = i;

    if (s.empty()) {
      s.push(rect);
    } else {
      if (s.top().height < rect.height)
        s.push(rect);
      else if (s.top().height > rect.height) {
        int target = i;
        while (!s.empty() && s.top().height >= rect.height) {
          Rectangle pre = s.top();
          s.pop();
          int area = pre.height * (i - pre.pos);
          maxv = max(maxv, area);
          target = pre.pos;
        }
        rect.pos = target;
        s.push(rect);
      }
    }
  }
  return maxv;
}

int largest_rect(int h, int w, vector<vector<int>> &grid) {
  vector<vector<int>> hist(h, vector<int>(w + 1));
  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      if (grid[i][j] == 1) {
        hist[i][j] = 0;
      } else {
        hist[i][j] = (i > 0) ? hist[i - 1][j] + 1 : 1;
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < h; i++) {
    ret = max(ret, largest_hist(w, hist[i]));
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> grid[i][j];
  cout << largest_rect(h, w, grid) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<double, int> pdbi;
typedef pair<int, pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define rrep2(i, a, b) for (int i = (a); i > b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()

const ll mod = 1000000000 + 7;
const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1 << 30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int h, w;
vector<vector<int>> grid(2005, vector<int>(2005));

struct Rectangle {
  int height, pos;
};

int largest_hist(int size, vector<int> &hist) {
  stack<Rectangle> s;
  int maxv = 0;
  hist[size] = 0;

  for (int i = 0; i <= size; i++) {
    Rectangle rect;
    rect.height = hist[i];
    rect.pos = i;

    if (s.empty()) {
      s.push(rect);
    } else {
      if (s.top().height < rect.height)
        s.push(rect);
      else if (s.top().height > rect.height) {
        int target = i;
        while (!s.empty() && s.top().height >= rect.height) {
          Rectangle pre = s.top();
          s.pop();
          int area = pre.height * (i - pre.pos);
          maxv = max(maxv, area);
          target = pre.pos;
        }
        rect.pos = target;
        s.push(rect);
      }
    }
  }
  return maxv;
}

int largest_rect(int h, int w, vector<vector<int>> &grid) {
  vector<vector<int>> hist(h, vector<int>(w + 1));
  for (int j = 0; j < w; j++) {
    for (int i = 0; i < h; i++) {
      if (grid[i][j] == 1) {
        hist[i][j] = 0;
      } else {
        hist[i][j] = (i > 0) ? hist[i - 1][j] + 1 : 1;
      }
    }
  }

  int ret = 0;
  for (int i = 0; i < h; i++) {
    ret = max(ret, largest_hist(w, hist[i]));
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> grid[i][j];
  cout << largest_rect(h, w, grid) << endl;
}