Problem p02327 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

// < "D:\\D_Download\\Visual Studio
// 2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
// Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

const int adx[] = {1, 1, 1, 0, -1, -1, -1, 0};
const int ady[] = {1, 0, -1, -1, -1, 0, 1, 1};

int add(const int base, const int plus) {
  if (!plus) {
    return base + 1;
  } else {
    return 0;
  }
}
// 0 :?????????1:??????2:?????????3:??? 4:?????????5:??? 6:?????? 7:??? ???
vector<vector<vector<int>>>
get_cumulative_sum(const vector<vector<int>> &field) {
  const int H = field.size();
  const int W = field[0].size();

  vector<vector<vector<int>>> sums(4, vector<vector<int>>(H, vector<int>(W)));

  for (int ny = H - 1; ny >= 0; --ny) {
    for (int nx = 0; nx < W; ++nx) {
      for (int k = 0; k < 3; ++k) {
        sums[k].clear();
      }
      for (int k = 3; k < 4; ++k) {
        int py = ny - ady[k];
        int px = nx - adx[k];
        if (!(px >= 0 && px < W && py >= 0 && py < H))
          continue;

        sums[k][ny][nx] = add(sums[k][py][px], field[ny][nx]);
      }
    }
  }

  return sums;
}

int getans(vector<int> hs) {
  hs.emplace_back(0);
  stack<pair<int, int>> que;
  que.emplace(-1, 0);
  int ans = 0;
  for (int x = 0; x < hs.size(); ++x) {

    if (que.empty()) {
      que.push(make_pair(x, hs[x]));
    } else {
      while (!que.empty()) {
        auto p = que.top();
        if (p.second < hs[x]) {
          que.push(make_pair(x, hs[x]));
          break;
        } else if (p.second == hs[x]) {
          break;
        } else {
          int area = (x - p.first) * p.second;
          ans = max(ans, area);
          que.pop();
          if (!que.empty()) {
            auto np = que.top();
            if (np.second < hs[x]) {
              que.push(make_pair(p.first, hs[x]));
              break;
            }
          }
        }
      }
    }
  }
  return ans;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> field(H + 2, vector<int>(W + 2, 1));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> field[i + 1][j + 1];
    }
  }
  auto vs = get_cumulative_sum(field);
  int ans = 0;

  for (int y = 1; y <= H; ++y) {
    vector<int> v;
    for (int x = 1; x <= W; ++x) {
      v.push_back(vs[3][y][x]);
    }
    ans = max(ans, getans(v));
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

// < "D:\\D_Download\\Visual Studio
// 2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
// Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

const int adx[] = {1, 1, 1, 0, -1, -1, -1, 0};
const int ady[] = {1, 0, -1, -1, -1, 0, 1, 1};

int add(const int base, const int plus) {
  if (!plus) {
    return base + 1;
  } else {
    return 0;
  }
}
// 0 :?????????1:??????2:?????????3:??? 4:?????????5:??? 6:?????? 7:??? ???
vector<vector<vector<int>>>
get_cumulative_sum(const vector<vector<int>> &field) {
  const int H = field.size();
  const int W = field[0].size();

  vector<vector<vector<int>>> sums(4, vector<vector<int>>(H, vector<int>(W)));

  for (int ny = H - 1; ny >= 0; --ny) {
    for (int nx = 0; nx < W; ++nx) {
      for (int k = 0; k < 3; ++k) {
        sums[k].clear();
      }
      for (int k = 3; k < 4; ++k) {
        int py = ny - ady[k];
        int px = nx - adx[k];
        if (!(px >= 0 && px < W && py >= 0 && py < H))
          continue;

        sums[k][ny][nx] = add(sums[k][py][px], field[ny][nx]);
      }
    }
  }

  return sums;
}

int getans(vector<int> hs) {
  hs.emplace_back(0);
  stack<pair<int, int>> que;
  que.emplace(-1, 0);
  int ans = 0;
  for (int x = 0; x < hs.size(); ++x) {

    if (que.empty()) {
      que.push(make_pair(x, hs[x]));
    } else {
      while (!que.empty()) {
        auto p = que.top();
        if (p.second < hs[x]) {
          que.push(make_pair(x, hs[x]));
          break;
        } else if (p.second == hs[x]) {
          break;
        } else {
          int area = (x - p.first) * p.second;
          ans = max(ans, area);
          que.pop();
          if (!que.empty()) {
            auto np = que.top();
            if (np.second < hs[x]) {
              que.push(make_pair(p.first, hs[x]));
              break;
            }
          }
        }
      }
    }
  }
  return ans;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> field(H + 2, vector<int>(W + 2, 1));
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> field[i + 1][j + 1];
    }
  }
  auto vs = get_cumulative_sum(field);
  int ans = 0;

  for (int y = 1; y <= H; ++y) {
    vector<int> v;
    for (int x = 1; x <= W; ++x) {
      v.push_back(vs[y][x][3]);
    }
    ans = max(ans, getans(v));
  }
  cout << ans << endl;
  return 0;
}