Problem p03630 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "YES"; }
  string_type do_falsename() const { return "NO"; }
};

int is_ok[2048][2048];
int h[2048][2048];

void solve(ll H, ll W, vector<string> S) {
  REP(i, H - 1)
  REP(j, W - 1) is_ok[i][j] =
      ((S[i][j] == S[i][j + 1]) == (S[i + 1][j] == S[i + 1][j + 1]));
  REP(i, H - 1) REP(j, W - 1) {
    if (i == 0)
      h[i][j] = is_ok[i][j] + 1;
    else {
      if (is_ok[i][j])
        h[i][j] = h[i - 1][j] + 1;
      else
        h[i][j] = 1;
    }
  }
  int res = 0;
  REP(i, H - 1) {
    vector<pair<int, int>> left;
    left.emplace_back(-1, -1);
    REP(j, W) {
      int pos = j;
      while (h[i][j] < left.back().second) {
        int val = left.back().second * (j - left.back().first + 1);
        chmax(res, val);
        pos = left.back().first;
        left.pop_back();
      }
      if (h[i][j] > left.back().second) {
        left.emplace_back(pos, h[i][j]);
      }
    }
  }
  assert(res >= H && res >= W);
  cout << res << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll W;
  ll H;
  scanf("%lld", &H);
  vector<string> S(H - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= H - 1; i++) {
    cin >> S[i];
  }
  solve(H, W, S);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "YES"; }
  string_type do_falsename() const { return "NO"; }
};

int is_ok[2048][2048];
int h[2048][2048];

void solve(ll H, ll W, vector<string> S) {
  REP(i, H - 1)
  REP(j, W - 1) is_ok[i][j] =
      ((S[i][j] == S[i][j + 1]) == (S[i + 1][j] == S[i + 1][j + 1]));
  REP(i, H - 1) REP(j, W - 1) {
    if (i == 0)
      h[i][j] = is_ok[i][j] + 1;
    else {
      if (is_ok[i][j])
        h[i][j] = h[i - 1][j] + 1;
      else
        h[i][j] = 1;
    }
  }
  int res = 0;
  REP(i, H - 1) {
    vector<pair<int, int>> left;
    left.emplace_back(-1, -1);
    REP(j, W) {
      int pos = j;
      while (h[i][j] < left.back().second) {
        int val = left.back().second * (j - left.back().first + 1);
        chmax(res, val);
        pos = left.back().first;
        left.pop_back();
      }
      if (h[i][j] > left.back().second) {
        left.emplace_back(pos, h[i][j]);
      }
    }
  }
  chmax(res, int(H));
  chmax(res, int(W));
  cout << res << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll W;
  ll H;
  scanf("%lld", &H);
  vector<string> S(H - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= H - 1; i++) {
    cin >> S[i];
  }
  solve(H, W, S);
  return 0;
}