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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<ll> x(1), y(1);
  vector<ll> a(n), b(n), c(n), d(m), e(m), f(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.emplace_back(a[i]);
    x.emplace_back(b[i]);
    y.emplace_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    x.emplace_back(d[i]);
    y.emplace_back(e[i]);
    y.emplace_back(f[i]);
  }
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  sort(y.begin(), y.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  int h = x.size(), w = y.size();
  vector<vector<int>> g(2 * h + 1, vector<int>(2 * w + 1));
  for (int i = 0; i < n; i++) {
    int ax = lower_bound(x.begin(), x.end(), a[i]) - x.begin();
    int bx = lower_bound(x.begin(), x.end(), b[i]) - x.begin();
    int cy = lower_bound(y.begin(), y.end(), c[i]) - y.begin();
    ax = ax * 2 + 1;
    bx = bx * 2 + 1;
    cy = cy * 2 + 1;
    for (int j = ax; j <= bx; j++) {
      g[j][cy] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int dx = lower_bound(x.begin(), x.end(), d[i]) - x.begin();
    int ey = lower_bound(y.begin(), y.end(), e[i]) - y.begin();
    int fy = lower_bound(y.begin(), y.end(), f[i]) - y.begin();
    dx = dx * 2 + 1;
    ey = ey * 2 + 1;
    fy = fy * 2 + 1;
    for (int j = ey; j <= fy; j++) {
      g[dx][j] = 1;
    }
  }
  ll ans = 0;
  int px = lower_bound(x.begin(), x.end(), 0) - x.begin();
  int py = lower_bound(y.begin(), y.end(), 0) - y.begin();
  px = px * 2 + 1;
  py = py * 2 + 1;
  queue<pair<int, int>> que;
  que.emplace(px, py);
  while (!que.empty()) {
    for (int id = que.size(); id > 0; id--) {
      int i, j;
      tie(i, j) = que.front();
      que.pop();
      if (g[i][j] == 1) {
        continue;
      }
      if (i == 0 || j == 0 || i == 2 * h || j == 2 * w) {
        cout << "INF" << '\\n';
        exit(0);
      }
      g[i][j] = 1;
      if (i % 2 == 0 && j % 2 == 0) {
        ans += (x[i / 2] - x[i / 2 - 1]) * (y[j / 2] - y[j / 2 - 1]);
      }
      for (int ni = i - 1; ni <= i + 1; ni++) {
        for (int nj = j - 1; nj <= j + 1; nj++) {
          if (g[ni][nj] == 0) {
            que.emplace(ni, nj);
          }
        }
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<ll> x(1), y(1);
  vector<ll> a(n), b(n), c(n), d(m), e(m), f(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.emplace_back(a[i]);
    x.emplace_back(b[i]);
    y.emplace_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    x.emplace_back(d[i]);
    y.emplace_back(e[i]);
    y.emplace_back(f[i]);
  }
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  sort(y.begin(), y.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  int h = x.size(), w = y.size();
  vector<vector<int>> g(2 * h + 1, vector<int>(2 * w + 1));
  for (int i = 0; i < n; i++) {
    int ax = lower_bound(x.begin(), x.end(), a[i]) - x.begin();
    int bx = lower_bound(x.begin(), x.end(), b[i]) - x.begin();
    int cy = lower_bound(y.begin(), y.end(), c[i]) - y.begin();
    ax = ax * 2 + 1;
    bx = bx * 2 + 1;
    cy = cy * 2 + 1;
    for (int j = ax; j <= bx; j++) {
      g[j][cy] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int dx = lower_bound(x.begin(), x.end(), d[i]) - x.begin();
    int ey = lower_bound(y.begin(), y.end(), e[i]) - y.begin();
    int fy = lower_bound(y.begin(), y.end(), f[i]) - y.begin();
    dx = dx * 2 + 1;
    ey = ey * 2 + 1;
    fy = fy * 2 + 1;
    for (int j = ey; j <= fy; j++) {
      g[dx][j] = 1;
    }
  }
  ll ans = 0;
  int px = lower_bound(x.begin(), x.end(), 0) - x.begin();
  int py = lower_bound(y.begin(), y.end(), 0) - y.begin();
  px = px * 2 + 1;
  py = py * 2 + 1;
  function<void(int, int)> dfs = [&](int i, int j) {
    if (i == 0 || j == 0 || i == 2 * h || j == 2 * w) {
      cout << "INF" << '\\n';
      exit(0);
    }
    g[i][j] = 1;
    if (i % 2 == 0 && j % 2 == 0) {
      ans += (x[i / 2] - x[i / 2 - 1]) * (y[j / 2] - y[j / 2 - 1]);
    }
    for (int ni = i - 1; ni <= i + 1; ni++) {
      for (int nj = j - 1; nj <= j + 1; nj++) {
        if (g[ni][nj] == 0) {
          dfs(ni, nj);
        }
      }
    }
  };
  dfs(px, py);
  cout << ans << '\\n';
  return 0;
}