Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;

struct L {
  int a, b, c;
  L(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {}
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<L> lh(n), lv(m);
  map<int, int> mpx, mpy; // その座標が何番目か
  mpx[0] = 0, mpy[0] = 0;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    lh[i] = L(a, b, c);
    mpx[a] = 0, mpx[b] = 0, mpy[c] = 0;
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    L l(a, b, c);
    lv[i] = L(a, b, c);
    mpy[a] = 0, mpy[b] = 0, mpx[c] = 0;
  }
  vector<int> xs, ys; // i番目の座標
  for (auto &it : mpx) {
    it.second = xs.size();
    xs.push_back(it.first);
  }
  for (auto &it : mpy) {
    it.second = ys.size();
    ys.push_back(it.first);
  }
  int w = xs.size() * 2, h = ys.size() * 2;
  vector<vector<int>> board(h, vector<int>(w));
  for (auto l : lh) {
    for (int i = mpx[l.a] * 2; i <= mpx[l.b] * 2; i++)
      board[mpy[l.c] * 2][i] = -1;
  }
  for (auto l : lv) {
    for (int i = mpy[l.a] * 2; i <= mpy[l.b] * 2; i++)
      board[i][mpy[l.c] * 2] = -1;
  }
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  int x = mpx[0] * 2, y = mpy[0] * 2;
  board[y][x] = 1;
  queue<pair<int, int>> visit;
  visit.push(make_pair(x, y));
  bool inf = false;
  while (!visit.empty()) {
    x = visit.front().first, y = visit.front().second;
    visit.pop();
    if (x <= 0 || w - 1 <= x || y <= 0 || h - 1 <= y) {
      inf = true;
      break;
    }
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (board[yy][xx] == 1 || board[yy][xx] == -1)
        continue;
      board[yy][xx] = 1;
      visit.push(make_pair(xx, yy));
    }
  }
  if (inf) {
    cout << "INF" << endl;
    return 0;
  }
  ll sum = 0;
  for (int j = 1; j < h; j += 2) {
    for (int i = 1; i < w; i += 2) {
      if (board[j][i] == 1) {
        sum += (ll)(xs[(i + 1) / 2] - xs[(i - 1) / 2]) *
               (ll)(ys[(j + 1) / 2] - ys[(j - 1) / 2]);
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using uint = unsigned int;

struct L {
  int a, b, c;
  L(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {}
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<L> lh(n), lv(m);
  map<int, int> mpx, mpy; // その座標が何番目か
  mpx[0] = 0, mpy[0] = 0;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    lh[i] = L(a, b, c);
    mpx[a] = 0, mpx[b] = 0, mpy[c] = 0;
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    L l(a, b, c);
    lv[i] = L(a, b, c);
    mpy[a] = 0, mpy[b] = 0, mpx[c] = 0;
  }
  vector<int> xs, ys; // i番目の座標
  for (auto &it : mpx) {
    it.second = xs.size();
    xs.push_back(it.first);
  }
  for (auto &it : mpy) {
    it.second = ys.size();
    ys.push_back(it.first);
  }
  int w = xs.size() * 2, h = ys.size() * 2;
  vector<vector<int>> board(h, vector<int>(w));
  for (auto l : lh) {
    for (int i = mpx[l.a] * 2; i <= mpx[l.b] * 2; i++)
      board[mpy[l.c] * 2][i] = -1;
  }
  for (auto l : lv) {
    for (int i = mpy[l.a] * 2; i <= mpy[l.b] * 2; i++)
      board[i][mpx[l.c] * 2] = -1;
  }
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  int x = mpx[0] * 2, y = mpy[0] * 2;
  board[y][x] = 1;
  queue<pair<int, int>> visit;
  visit.push(make_pair(x, y));
  bool inf = false;
  while (!visit.empty()) {
    x = visit.front().first, y = visit.front().second;
    visit.pop();
    if (x <= 0 || w - 1 <= x || y <= 0 || h - 1 <= y) {
      inf = true;
      break;
    }
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (board[yy][xx] == 1 || board[yy][xx] == -1)
        continue;
      board[yy][xx] = 1;
      visit.push(make_pair(xx, yy));
    }
  }
  if (inf) {
    cout << "INF" << endl;
    return 0;
  }
  ll sum = 0;
  for (int j = 1; j < h; j += 2) {
    for (int i = 1; i < w; i += 2) {
      if (board[j][i] == 1) {
        sum += (ll)(xs[(i + 1) / 2] - xs[(i - 1) / 2]) *
               (ll)(ys[(j + 1) / 2] - ys[(j - 1) / 2]);
      }
    }
  }
  cout << sum << endl;
  return 0;
}