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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v)                                                               \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]" << endl;
#define STOP assert(false);
#else
#define dump(x) ;
#define LINE ;
#define dumpV(v) ;
#define STOP ;
#endif

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.fi << ", " << a.se << ')';
  return out;
}
} // namespace std

int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

int H, W;
int d[1010][1010];
auto isOutOfRange = [](int h, int w, int H, int W) {
  return h < 0 || h >= H || w < 0 || w >= W;
};

vi memo; //-1:non-used, -2:ok, else:ikisaki

vector<int> es[1010 * 1010];

int dfs(int now) {
  if (memo[now] != -1)
    return memo[now];
  int ret = -1;
  for (auto &to : es[now]) {
    int tmp = dfs(to);
    if (ret == -1)
      ret = tmp;
    else if (ret != tmp) {
      return memo[now] = -2;
    }
  }
  return memo[now] = (ret == -1 ? now : ret);
}

int main() {
  cin >> H >> W;
  rep(i, H) rep(j, W) cin >> d[i][j];

  memo.clear();
  memo.resize(H * W, -1);

  rep(y, H) rep(x, W) {
    int now = y * W + x;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (isOutOfRange(ny, nx, H, W))
        continue;
      int nxt = ny * W + nx;
      if (d[y][x] > d[ny][nx])
        es[now].pb(nxt);
    }
  }
  dump(H);
  dump(W);
  dump(es[1].size());
  LINE;
  // rep(i, H) rep(j, W) {
  // 	dump(i*W+j);
  // 	dfs(i*W+j);
  // }
  for (int i = H - 1; i >= 0; i--) {
    for (int j = W - 1; j >= 0; j--) {
      dump(i * W + j);
      dfs(i * W + j);
    }
  }
  LINE;

  int ans = 0;
  rep(i, memo.size()) if (memo[i] == -2) ans++;

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v)                                                               \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]" << endl;
#define STOP assert(false);
#else
#define dump(x) ;
#define LINE ;
#define dumpV(v) ;
#define STOP ;
#endif

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.fi << ", " << a.se << ')';
  return out;
}
} // namespace std

int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

int H, W;
int d[1010][1010];
auto isOutOfRange = [](int h, int w, int H, int W) {
  return h < 0 || h >= H || w < 0 || w >= W;
};

vi memo; //-1:non-used, -2:ok, else:ikisaki

vector<int> es[1010 * 1010];

int dfs(int now) {
  if (memo[now] != -1)
    return memo[now];
  int ret = -1;
  for (auto &to : es[now]) {
    int tmp = dfs(to);
    if (ret == -1)
      ret = tmp;
    else if (ret != tmp)
      ret = -2;
  }
  return memo[now] = (ret == -1 ? now : ret);
}

int main() {
  cin >> H >> W;
  rep(i, H) rep(j, W) cin >> d[i][j];

  memo.clear();
  memo.resize(H * W, -1);

  rep(y, H) rep(x, W) {
    int now = y * W + x;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (isOutOfRange(ny, nx, H, W))
        continue;
      int nxt = ny * W + nx;
      if (d[y][x] > d[ny][nx])
        es[now].pb(nxt);
    }
  }

  rep(i, H) rep(j, W) dfs(i * W + j);

  int ans = 0;
  rep(i, memo.size()) if (memo[i] == -2) ans++;

  cout << ans << endl;
#ifdef DEBUG_MODE
  // DEBUG_2D
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      printf("%3d", memo[i * W + j]);
    printf("\\n");
  }
#endif
  dump(es[3].size());
  return 0;
}