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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

signed main() {
  int N, M;
  cin >> N >> M;

  vector<int> X, Y;
  vector<int> A(N), B(N), C(N);
  REP(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    X.emplace_back(A[i]);
    X.emplace_back(B[i]);
    Y.emplace_back(C[i]);
  }
  vector<int> D(M), E(M), F(M);
  REP(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    X.emplace_back(D[i]);
    Y.emplace_back(E[i]);
    Y.emplace_back(F[i]);
  }
  X.emplace_back(0);
  Y.emplace_back(0);

  sort(ALL(X));
  sort(ALL(Y));

  X.erase(unique(ALL(X)), X.end());
  Y.erase(unique(ALL(Y)), Y.end());

  REP(i, N) {
    A[i] = lower_bound(ALL(X), A[i]) - X.begin();
    B[i] = lower_bound(ALL(X), B[i]) - X.begin();
    C[i] = lower_bound(ALL(Y), C[i]) - Y.begin();
  }
  REP(i, M) {
    D[i] = lower_bound(ALL(X), D[i]) - X.begin();
    E[i] = lower_bound(ALL(Y), E[i]) - Y.begin();
    F[i] = lower_bound(ALL(Y), F[i]) - Y.begin();
  }

  int H = X.size() * 2;
  int W = Y.size() * 2;
  vector<vector<int>> d(H, vector<int>(W));
  REP(i, N) { FOR(j, A[i] * 2, B[i] * 2 + 1) d[j][C[i] * 2] = -1; }
  REP(i, M) { FOR(j, E[i] * 2, F[i] * 2 + 1) d[D[i] * 2][j] = -1; }

  int sx = (lower_bound(ALL(X), 0) - X.begin()) * 2;
  int sy = (lower_bound(ALL(Y), 0) - Y.begin()) * 2;

  queue<pair<int, int>> q;
  q.emplace(sx, sy);

  d[sx][sy] = 1;

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

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (OUT(0, nx, H) || OUT(0, ny, W))
        continue;
      if (d[nx][ny] != 0)
        continue;
      d[nx][ny] = 1;
      q.emplace(nx, ny);
    }
  }

  int ans = 0;
  REP(i, H) REP(j, W) {
    if (d[i][j] != 1)
      continue;
    if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
      cout << "INF"
           << "\\n";
      return 0;
    }
    if (i % 2 == 0 || j % 2 == 0)
      continue;
    ans += (X[i / 2 + 1] - X[i / 2]) * (Y[j / 2 + 1] - Y[j / 2]);
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

signed main() {
  int N, M;
  cin >> N >> M;

  vector<int> X, Y;
  vector<int> A(N), B(N), C(N);
  REP(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    X.emplace_back(A[i]);
    X.emplace_back(B[i]);
    Y.emplace_back(C[i]);
  }
  vector<int> D(M), E(M), F(M);
  REP(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    X.emplace_back(D[i]);
    Y.emplace_back(E[i]);
    Y.emplace_back(F[i]);
  }
  X.emplace_back(0);
  Y.emplace_back(0);

  sort(ALL(X));
  sort(ALL(Y));

  X.erase(unique(ALL(X)), X.end());
  Y.erase(unique(ALL(Y)), Y.end());

  REP(i, N) {
    A[i] = lower_bound(ALL(X), A[i]) - X.begin();
    B[i] = lower_bound(ALL(X), B[i]) - X.begin();
    C[i] = lower_bound(ALL(X), C[i]) - X.begin();
  }
  REP(i, M) {
    D[i] = lower_bound(ALL(X), D[i]) - X.begin();
    E[i] = lower_bound(ALL(X), E[i]) - X.begin();
    F[i] = lower_bound(ALL(X), F[i]) - X.begin();
  }

  int H = X.size() * 2;
  int W = Y.size() * 2;
  vector<vector<int>> d(H, vector<int>(W));
  REP(i, N) { FOR(j, A[i] * 2, B[i] * 2 + 1) d[j][C[i] * 2] = -1; }
  REP(i, M) { FOR(j, E[i] * 2, F[i] * 2 + 1) d[D[i] * 2][j] = -1; }

  int sx = (lower_bound(ALL(X), 0) - X.begin()) * 2;
  int sy = (lower_bound(ALL(Y), 0) - Y.begin()) * 2;

  queue<pair<int, int>> q;
  q.emplace(sx, sy);

  d[sx][sy] = 1;

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

  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (OUT(0, nx, H) || OUT(0, ny, W))
        continue;
      if (d[nx][ny] != 0)
        continue;
      d[nx][ny] = 1;
      q.emplace(nx, ny);
    }
  }

  int ans = 0;
  REP(i, H) REP(j, W) {
    if (d[i][j] != 1)
      continue;
    if (i == 0 || i == H - 1 || j == 0 || j == W - 1) {
      cout << "INF"
           << "\\n";
      return 0;
    }
    if (i % 2 == 0 || j % 2 == 0)
      continue;
    ans += (X[i / 2 + 1] - X[i / 2]) * (Y[j / 2 + 1] - Y[j / 2]);
  }

  cout << ans << "\\n";

  return 0;
}