Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

void compress(vl &i2x, map<ll, ll> &x2i) {
  sort(all(i2x));
  i2x.erase(unique(all(i2x)), end(i2x));
  FOR(i, 0, sz(i2x)) x2i[i2x[i]] = i;
}

const ll N = 1010;
bool wallns[N][N], wallew[N][N], reach[N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll m, n;
  cin >> m >> n;

  vl i2x = {-oo, oo}, j2y = {-oo, oo};

  vector<tuple<ll, ll, ll>> ns, ew;
  while (m--) {
    ll a, b, c;
    cin >> a >> b >> c;
    i2x.pb(a), i2x.pb(b), j2y.pb(c);
    ns.pb(make_tuple(a, b, c));
  }
  while (n--) {
    ll d, e, f;
    cin >> d >> e >> f;
    i2x.pb(d), j2y.pb(e), j2y.pb(f);
    ew.pb(make_tuple(d, e, f));
  }

  map<ll, ll> x2i, y2j;
  compress(i2x, x2i), compress(j2y, y2j);

  for (auto tpl : ns) {
    ll a, b, c;
    tie(a, b, c) = tpl;
    a = x2i[a], b = x2i[b], c = y2j[c];
    FOR(i, a, b)
    wallns[i][c - 1] = 1; //, cerr << "NS " << i << " " << c-1 << endl;
  }
  for (auto tpl : ew) {
    ll d, e, f;
    tie(d, e, f) = tpl;
    d = x2i[d], e = y2j[e], f = y2j[f];
    FOR(j, e, f)
    wallew[d - 1][j] = 1; //, cerr << "EW " << d-1 << " " << j << endl;
  }

  queue<pll> q;

  FOR(i, 0, sz(i2x) - 1) if (i2x[i] <= 0 && i2x[i + 1] >= 0) {
    FOR(j, 0, sz(j2y) - 1) if (j2y[j] <= 0 && j2y[j + 1] >= 0) {
      q.emplace(i, j), reach[i][j] = 1;
    }
  }

  while (sz(q)) {
    ll i, j;
    tie(i, j) = q.front();
    q.pop();
    //		cerr << i << " " << j << endl;

    if (i > 0 && !wallew[i - 1][j] && !reach[i - 1][j]) {
      q.emplace(i - 1, j), reach[i - 1][j] = 1;
    }
    if (j > 0 && !wallns[i][j - 1] && !reach[i][j - 1]) {
      q.emplace(i, j - 1), reach[i][j - 1] = 1;
    }
    if (i + 2 < sz(i2x) && !wallew[i][j] && !reach[i + 1][j]) {
      q.emplace(i + 1, j), reach[i + 1][j] = 1;
    }
    if (j + 2 < sz(j2y) && !wallns[i][j] && !reach[i][j + 1]) {
      q.emplace(i, j + 1), reach[i][j + 1] = 1;
    }
  }

  ll res = 0;
  FOR(i, 0, sz(i2x) - 1) FOR(j, 0, sz(j2y) - 1) if (reach[i][j]) {
    if (i == 0 || j == 0 || i == sz(i2x) - 2 || j == sz(j2y) - 2) {
      cout << "INF" << endl;
      return 0;
    }
    res += (i2x[i + 1] - i2x[i]) * (j2y[j + 1] - j2y[j]);
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

void compress(vl &i2x, map<ll, ll> &x2i) {
  sort(all(i2x));
  i2x.erase(unique(all(i2x)), end(i2x));
  FOR(i, 0, sz(i2x)) x2i[i2x[i]] = i;
}

const ll N = 3010;
bool wallns[N][N], wallew[N][N], reach[N][N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll m, n;
  cin >> m >> n;

  vl i2x = {-oo, oo}, j2y = {-oo, oo};

  vector<tuple<ll, ll, ll>> ns, ew;
  while (m--) {
    ll a, b, c;
    cin >> a >> b >> c;
    i2x.pb(a), i2x.pb(b), j2y.pb(c);
    ns.pb(make_tuple(a, b, c));
  }
  while (n--) {
    ll d, e, f;
    cin >> d >> e >> f;
    i2x.pb(d), j2y.pb(e), j2y.pb(f);
    ew.pb(make_tuple(d, e, f));
  }

  map<ll, ll> x2i, y2j;
  compress(i2x, x2i), compress(j2y, y2j);

  for (auto tpl : ns) {
    ll a, b, c;
    tie(a, b, c) = tpl;
    a = x2i[a], b = x2i[b], c = y2j[c];
    FOR(i, a, b)
    wallns[i][c - 1] = 1; //, cerr << "NS " << i << " " << c-1 << endl;
  }
  for (auto tpl : ew) {
    ll d, e, f;
    tie(d, e, f) = tpl;
    d = x2i[d], e = y2j[e], f = y2j[f];
    FOR(j, e, f)
    wallew[d - 1][j] = 1; //, cerr << "EW " << d-1 << " " << j << endl;
  }

  queue<pll> q;

  FOR(i, 0, sz(i2x) - 1) if (i2x[i] <= 0 && i2x[i + 1] >= 0) {
    FOR(j, 0, sz(j2y) - 1) if (j2y[j] <= 0 && j2y[j + 1] >= 0) {
      q.emplace(i, j), reach[i][j] = 1;
    }
  }

  while (sz(q)) {
    ll i, j;
    tie(i, j) = q.front();
    q.pop();
    //		cerr << i << " " << j << endl;

    if (i > 0 && !wallew[i - 1][j] && !reach[i - 1][j]) {
      q.emplace(i - 1, j), reach[i - 1][j] = 1;
    }
    if (j > 0 && !wallns[i][j - 1] && !reach[i][j - 1]) {
      q.emplace(i, j - 1), reach[i][j - 1] = 1;
    }
    if (i + 2 < sz(i2x) && !wallew[i][j] && !reach[i + 1][j]) {
      q.emplace(i + 1, j), reach[i + 1][j] = 1;
    }
    if (j + 2 < sz(j2y) && !wallns[i][j] && !reach[i][j + 1]) {
      q.emplace(i, j + 1), reach[i][j + 1] = 1;
    }
  }

  ll res = 0;
  FOR(i, 0, sz(i2x) - 1) FOR(j, 0, sz(j2y) - 1) if (reach[i][j]) {
    if (i == 0 || j == 0 || i == sz(i2x) - 2 || j == sz(j2y) - 2) {
      cout << "INF" << endl;
      return 0;
    }
    res += (i2x[i + 1] - i2x[i]) * (j2y[j + 1] - j2y[j]);
  }
  cout << res << endl;
}