Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int N, M;
vector<int> xx, yy;
int A[1005], B[1005], C[1005];
int D[1005], E[1005], F[1005];
bool b[2005][2005], ng[2005][2005][4];
void idx(vector<int> &v, int &x) { x = lower_bound(all(v), x) - v.begin(); }
signed main() {
  cin >> N >> M;
  xx.pb(-inf);
  xx.pb(inf);
  xx.pb(0);
  yy.pb(-inf);
  yy.pb(inf);
  yy.pb(0);
  rep(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    xx.pb(A[i]);
    xx.pb(B[i]);
    yy.pb(C[i]);
  }
  rep(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    xx.pb(D[i]);
    yy.pb(E[i]);
    yy.pb(F[i]);
  }
  sort(all(xx));
  xx.erase(unique(all(xx)), xx.end());
  sort(all(yy));
  yy.erase(unique(all(yy)), yy.end());
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  rep(i, N) {
    idx(xx, A[i]);
    idx(xx, B[i]);
    idx(yy, C[i]);
    for (int j = A[i]; j < B[i]; j++) {
      ng[j][C[i]][2] = true;
      ng[j][C[i] - 1][0] = true;
    }
  }
  rep(i, M) {
    idx(xx, D[i]);
    idx(yy, E[i]);
    idx(yy, F[i]);
    for (int j = E[i]; j < F[i]; j++) {
      ng[D[i]][j][3] = true;
      ng[D[i] - 1][j][1] = true;
    }
  }
  queue<P> que;
  int x = lower_bound(all(xx), 0ll) - xx.begin();
  int y = lower_bound(all(yy), 0ll) - yy.begin();
  int ans = 0;
  que.push({x, y});
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      if (ng[p.first][p.second][i])
        continue;
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx == 0 || nx == xx.size() - 1 || ny == 0 || ny == yy.size() - 1) {
        cout << "INF" << endl;
        return 0;
      }
      if (!b[nx][ny]) {
        ans += (xx[nx + 1] - xx[nx]) * (yy[ny + 1] - yy[ny]);
        b[nx][ny] = true;
        que.push({nx, ny});
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int N, M;
vector<int> xx, yy;
int A[1005], B[1005], C[1005];
int D[1005], E[1005], F[1005];
bool b[4005][4005], ng[4005][4005][4];
void idx(vector<int> &v, int &x) { x = lower_bound(all(v), x) - v.begin(); }
signed main() {
  cin >> N >> M;
  xx.pb(-inf);
  xx.pb(inf);
  xx.pb(0);
  yy.pb(-inf);
  yy.pb(inf);
  yy.pb(0);
  rep(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    xx.pb(A[i]);
    xx.pb(B[i]);
    yy.pb(C[i]);
  }
  rep(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    xx.pb(D[i]);
    yy.pb(E[i]);
    yy.pb(F[i]);
  }
  sort(all(xx));
  xx.erase(unique(all(xx)), xx.end());
  sort(all(yy));
  yy.erase(unique(all(yy)), yy.end());
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  rep(i, N) {
    idx(xx, A[i]);
    idx(xx, B[i]);
    idx(yy, C[i]);
    for (int j = A[i]; j < B[i]; j++) {
      ng[j][C[i]][2] = true;
      ng[j][C[i] - 1][0] = true;
    }
  }
  rep(i, M) {
    idx(xx, D[i]);
    idx(yy, E[i]);
    idx(yy, F[i]);
    for (int j = E[i]; j < F[i]; j++) {
      ng[D[i]][j][3] = true;
      ng[D[i] - 1][j][1] = true;
    }
  }
  queue<P> que;
  int x = lower_bound(all(xx), 0ll) - xx.begin();
  int y = lower_bound(all(yy), 0ll) - yy.begin();
  int ans = 0;
  que.push({x, y});
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      if (ng[p.first][p.second][i])
        continue;
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx == 0 || nx == xx.size() - 1 || ny == 0 || ny == yy.size() - 1) {
        cout << "INF" << endl;
        return 0;
      }
      if (!b[nx][ny]) {
        ans += (xx[nx + 1] - xx[nx]) * (yy[ny + 1] - yy[ny]);
        b[nx][ny] = true;
        que.push({nx, ny});
      }
    }
  }
  cout << ans << endl;
}