Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using PII = pair<int, int>;

const int inf = 2e9;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(n), b(n), c(n);
  vector<int> d(m), e(m), f(m);
  set<int> sx, sy;
  sx = sy = {0, inf, -inf};
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    sx.insert(a[i]);
    sx.insert(b[i]);
    sy.insert(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    sx.insert(d[i]);
    sy.insert(e[i]);
    sy.insert(f[i]);
  }

  vector<int> vx(sx.begin(), sx.end());
  vector<int> vy(sy.begin(), sy.end());

  int w = 2 * sx.size() - 1;
  int h = 2 * sy.size() - 1;
  vector<string> board(h, string(w, '.'));

  // printf("hw %d %d\n", w, h);
  // for (int i = 0; i < max(vx.size(), vy.size()); i++) {
  // 	if (i < min(vx.size(), vy.size())) {
  // 		printf("i %2d, x %4d, y %4d\n", i, vx[i], vy[i]);
  // 	} else if (i >= vx.size()) {
  // 		printf("i %2d, x ----, y %4d\n", i, vy[i]);
  // 	} else {
  // 		printf("i %2d, x %4d, y ----\n", i, vx[i]);
  // 	}
  // }

  for (int i = 0; i < n; i++) {
    int ix1 = lower_bound(vx.begin(), vx.end(), a[i]) - vx.begin();
    int ix2 = lower_bound(vx.begin(), vx.end(), b[i]) - vx.begin();
    int iy = lower_bound(vy.begin(), vy.end(), c[i]) - vy.begin();

    ix1 = 2 * ix1;
    ix2 = 2 * ix2;
    iy = 2 * iy;

    for (int i = ix1; i <= ix2; i++) {
      board[iy][i] = '#';
    }
  }
  for (int i = 0; i < m; i++) {
    int ix = lower_bound(vx.begin(), vx.end(), d[i]) - vx.begin();
    int iy1 = lower_bound(vy.begin(), vy.end(), e[i]) - vy.begin();
    int iy2 = lower_bound(vy.begin(), vy.end(), f[i]) - vy.begin();

    ix = 2 * ix;
    iy1 = 2 * iy1;
    iy2 = 2 * iy2;

    for (int i = iy1; i <= iy2; i++) {
      board[i][ix] = '#';
    }
  }

  long long ans = 0;
  queue<PII> que;

  int ix = lower_bound(vx.begin(), vx.end(), 0) - vx.begin();
  int iy = lower_bound(vy.begin(), vy.end(), 0) - vy.begin();
  int bx = 2 * ix;
  int by = 2 * iy;
  que.emplace(bx, by);

  while (!que.empty()) {
    tie(bx, by) = que.front();
    que.pop();
    if (board[by][bx] != '.') {
      continue;
    }
    // printf("bx %d, by %d, %c, %lu\n", bx, by, board[by][bx], que.size());

    if (bx == 0 || bx == w - 1 || by == 0 || by == h) {
      cout << "INF" << endl;
      return 0;
    }

    board[by][bx] = 'O';
    if (bx % 2 == 1 && by % 2 == 1) {
      ix = bx / 2;
      iy = by / 2;
      ans += 1LL * (vx[ix + 1] - vx[ix]) * (vy[iy + 1] - vy[iy]);
    }

    for (int i = 0; i < 4; i++) {
      que.emplace(bx + dx[i], by + dy[i]);
    }
  }

  for (auto s : board) {
    cerr << s << endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using PII = pair<int, int>;

const int inf = 2e9;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;

  vector<int> a(n), b(n), c(n);
  vector<int> d(m), e(m), f(m);
  set<int> sx, sy;
  sx = sy = {0, inf, -inf};
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    sx.insert(a[i]);
    sx.insert(b[i]);
    sy.insert(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    sx.insert(d[i]);
    sy.insert(e[i]);
    sy.insert(f[i]);
  }

  vector<int> vx(sx.begin(), sx.end());
  vector<int> vy(sy.begin(), sy.end());

  int w = 2 * sx.size() - 1;
  int h = 2 * sy.size() - 1;
  vector<string> board(h, string(w, '.'));

  // printf("hw %d %d\n", w, h);
  // for (int i = 0; i < max(vx.size(), vy.size()); i++) {
  // 	if (i < min(vx.size(), vy.size())) {
  // 		printf("i %2d, x %4d, y %4d\n", i, vx[i], vy[i]);
  // 	} else if (i >= vx.size()) {
  // 		printf("i %2d, x ----, y %4d\n", i, vy[i]);
  // 	} else {
  // 		printf("i %2d, x %4d, y ----\n", i, vx[i]);
  // 	}
  // }

  for (int i = 0; i < n; i++) {
    int ix1 = lower_bound(vx.begin(), vx.end(), a[i]) - vx.begin();
    int ix2 = lower_bound(vx.begin(), vx.end(), b[i]) - vx.begin();
    int iy = lower_bound(vy.begin(), vy.end(), c[i]) - vy.begin();

    ix1 = 2 * ix1;
    ix2 = 2 * ix2;
    iy = 2 * iy;

    for (int i = ix1; i <= ix2; i++) {
      board[iy][i] = '#';
    }
  }
  for (int i = 0; i < m; i++) {
    int ix = lower_bound(vx.begin(), vx.end(), d[i]) - vx.begin();
    int iy1 = lower_bound(vy.begin(), vy.end(), e[i]) - vy.begin();
    int iy2 = lower_bound(vy.begin(), vy.end(), f[i]) - vy.begin();

    ix = 2 * ix;
    iy1 = 2 * iy1;
    iy2 = 2 * iy2;

    for (int i = iy1; i <= iy2; i++) {
      board[i][ix] = '#';
    }
  }

  long long ans = 0;
  queue<PII> que;

  int ix = lower_bound(vx.begin(), vx.end(), 0) - vx.begin();
  int iy = lower_bound(vy.begin(), vy.end(), 0) - vy.begin();
  int bx = 2 * ix;
  int by = 2 * iy;
  que.emplace(bx, by);

  while (!que.empty()) {
    tie(bx, by) = que.front();
    que.pop();
    if (board[by][bx] != '.') {
      continue;
    }
    // printf("bx %d, by %d, %c, %lu\n", bx, by, board[by][bx], que.size());

    if (bx == 0 || bx == w - 1 || by == 0 || by == h - 1) {
      cout << "INF" << endl;
      return 0;
    }

    board[by][bx] = 'O';
    if (bx % 2 == 1 && by % 2 == 1) {
      ix = bx / 2;
      iy = by / 2;
      ans += 1LL * (vx[ix + 1] - vx[ix]) * (vy[iy + 1] - vy[iy]);
    }

    for (int i = 0; i < 4; i++) {
      que.emplace(bx + dx[i], by + dy[i]);
    }
  }

  for (auto s : board) {
    cerr << s << endl;
  }
  cout << ans << endl;
  return 0;
}