Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  REP(i, N) scanf("%d %d %d", &A[i], &B[i], &C[i]);
  vector<int> D(N), E(N), F(N);
  REP(i, M) scanf("%d %d %d", &D[i], &E[i], &F[i]);

  vector<int> cx, cy;
  cx.emplace_back(-INF);
  cx.emplace_back(INF);
  cy.emplace_back(-INF);
  cy.emplace_back(INF);
  REP(i, N) {
    cx.emplace_back(A[i]);
    cx.emplace_back(B[i]);
    cy.emplace_back(C[i]);
  }
  REP(i, M) {
    cx.emplace_back(D[i]);
    cy.emplace_back(E[i]);
    cy.emplace_back(F[i]);
  }

  sort(ALL(cx));
  cx.erase(unique(ALL(cx)), cx.end());
  sort(ALL(cy));
  cy.erase(unique(ALL(cy)), cy.end());

  auto compress = [](auto &x, const auto &cx) {
    for (auto &c : x)
      c = lower_bound(ALL(cx), c) - cx.begin();
  };
  compress(A, cx);
  compress(B, cx);
  compress(C, cy);
  compress(D, cx);
  compress(E, cy);
  compress(F, cy);

  vector<vector<bool>> isok[4], maps(cy.size(), vector<bool>(cx.size())),
      done = maps;
  // isok[urdl]

  REP(i, 4) isok[i] = vector<vector<bool>>(cy.size(), vector<bool>(cx.size()));

  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
  REP(dir, 4) REP(i, cy.size()) REP(j, cx.size()) {
    int ny = i + dy[dir], nx = j + dx[dir];
    isok[dir][i][j] = nx >= 0 && nx < cx.size() && ny >= 0 && ny < cy.size();
  }

  REP(i, N) {
    int y = C[i];
    FOR(x, A[i] + 1, B[i] + 1) { isok[2][y][x] = isok[0][y + 1][x] = false; }
  }
  REP(i, M) {
    int x = D[i];
    FOR(y, E[i] + 1, F[i] + 1) { isok[1][y][x] = isok[3][y][x + 1] = false; }
  }

  int stx = lower_bound(ALL(cx), 0) - cx.begin(),
      sty = lower_bound(ALL(cy), 0) - cy.begin();
  queue<pii> que;
  que.emplace(sty, stx);
  done[sty][stx] = true;
  ll ans = 0;

  while (!que.empty()) {
    int y = que.front().fi, x = que.front().se;
    que.pop();

    if (abs(cx[x]) == INF || abs(cy[y]) == INF) {
      puts("INF");
      return 0;
    }

    ans += (ll)(cy[y] - cy[y - 1]) * (cx[x] - cx[x - 1]);

    REP(dir, 4) if (isok[dir][y][x]) {
      int ny = y + dy[dir], nx = x + dx[dir];
      if (!done[ny][nx]) {
        done[ny][nx] = true;
        que.emplace(ny, nx);
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  REP(i, N) scanf("%d %d %d", &A[i], &B[i], &C[i]);
  vector<int> D(M), E(M), F(M);
  REP(i, M) scanf("%d %d %d", &D[i], &E[i], &F[i]);

  vector<int> cx, cy;
  cx.emplace_back(-INF);
  cx.emplace_back(INF);
  cy.emplace_back(-INF);
  cy.emplace_back(INF);
  REP(i, N) {
    cx.emplace_back(A[i]);
    cx.emplace_back(B[i]);
    cy.emplace_back(C[i]);
  }
  REP(i, M) {
    cx.emplace_back(D[i]);
    cy.emplace_back(E[i]);
    cy.emplace_back(F[i]);
  }

  sort(ALL(cx));
  cx.erase(unique(ALL(cx)), cx.end());
  sort(ALL(cy));
  cy.erase(unique(ALL(cy)), cy.end());

  auto compress = [](auto &x, const auto &cx) {
    for (auto &c : x)
      c = lower_bound(ALL(cx), c) - cx.begin();
  };
  compress(A, cx);
  compress(B, cx);
  compress(C, cy);
  compress(D, cx);
  compress(E, cy);
  compress(F, cy);

  vector<vector<bool>> isok[4], maps(cy.size(), vector<bool>(cx.size())),
      done = maps;
  // isok[urdl]

  REP(i, 4) isok[i] = vector<vector<bool>>(cy.size(), vector<bool>(cx.size()));

  int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
  REP(dir, 4) REP(i, cy.size()) REP(j, cx.size()) {
    int ny = i + dy[dir], nx = j + dx[dir];
    isok[dir][i][j] = nx >= 0 && nx < cx.size() && ny >= 0 && ny < cy.size();
  }

  REP(i, N) {
    int y = C[i];
    FOR(x, A[i] + 1, B[i] + 1) { isok[2][y][x] = isok[0][y + 1][x] = false; }
  }
  REP(i, M) {
    int x = D[i];
    FOR(y, E[i] + 1, F[i] + 1) { isok[1][y][x] = isok[3][y][x + 1] = false; }
  }

  int stx = lower_bound(ALL(cx), 0) - cx.begin(),
      sty = lower_bound(ALL(cy), 0) - cy.begin();
  queue<pii> que;
  que.emplace(sty, stx);
  done[sty][stx] = true;
  ll ans = 0;

  while (!que.empty()) {
    int y = que.front().fi, x = que.front().se;
    que.pop();

    if (abs(cx[x]) == INF || abs(cy[y]) == INF) {
      puts("INF");
      return 0;
    }

    ans += (ll)(cy[y] - cy[y - 1]) * (cx[x] - cx[x - 1]);

    REP(dir, 4) if (isok[dir][y][x]) {
      int ny = y + dy[dir], nx = x + dx[dir];
      if (!done[ny][nx]) {
        done[ny][nx] = true;
        que.emplace(ny, nx);
      }
    }
  }

  cout << ans << endl;
  return 0;
}