Problem p02680 (C++) — buggy submission, verdict: 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 dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
vector<L> lh, lv;
int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> mpx, mpy;
  mpx[0] = 0, mpy[0] = 0;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    mpx[a] = 0, mpx[b] = 0, mpy[c] = 0;
    lh.emplace_back(a, b, c);
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    mpy[a] = 0, mpy[b] = 0, mpx[c] = 0;
    lv.emplace_back(a, b, c);
  }
  vector<int> xs, ys;
  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 h = ys.size() * 2;
  int w = xs.size() * 2;
  vector<vector<int>> d(h, vector<int>(w));
  for (auto l : lh) {
    for (int i = mpx[l.a] * 2; i <= mpx[l.b] * 2; i++)
      d[mpy[l.c] * 2][i] = -1;
  }
  for (auto l : lv) {
    for (int i = mpy[l.a] * 2; i <= mpy[l.b] * 2; i++)
      d[i][mpx[l.c] * 2] = -1;
  }
  queue<pair<int, int>> to_visit;
  int x = mpx[0] * 2, y = mpy[0] * 2;
  d[y][x] = 1;
  to_visit.push(make_pair(x, y));
  ll sum = 0;
  if (x % 2 == 1 && y % 2 == 1) {
    sum += (ll)(xs[(x + 1) / 2] - xs[(x - 1) / 2]) *
           (ll)(ys[(y + 1) / 2] - ys[(y - 1) / 2]);
  }
  bool inf = false;
  while (!to_visit.empty()) {
    x = to_visit.front().first;
    y = to_visit.front().second;
    if (x <= 0 || w - 1 <= x || y <= 0 || h - 1 <= y) {
      // cout << "INF" << endl;
      inf = true;
      break;
    }
    to_visit.pop();
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (d[yy][xx] == -1 || d[yy][xx] == 1)
        continue;
      d[yy][xx] = 1;
      if (xx % 2 == 1 && yy % 2 == 1) {
        sum += (ll)(xs[(xx + 1) / 2] - xs[(xx - 1) / 2]) *
               (ll)(ys[(yy + 1) / 2] - ys[(yy - 1) / 2]);
      }
      to_visit.push(make_pair(xx, yy));
    }
  }
  if (inf)
    cout << "INF" << endl;
  else
    cout << sum << endl;
  return 0;
}

Error: 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 dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
vector<L> lh, lv;
int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> mpx, mpy;
  mpx[0] = 0, mpy[0] = 0;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    mpx[a] = 0, mpx[b] = 0, mpy[c] = 0;
    lh.emplace_back(a, b, c);
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    mpy[a] = 0, mpy[b] = 0, mpx[c] = 0;
    lv.emplace_back(a, b, c);
  }
  vector<int> xs, ys;
  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 h = xs.size() * 2;
  int w = ys.size() * 2;
  vector<vector<int>> d(h, vector<int>(w));
  for (auto l : lh) {
    for (int i = mpx[l.a] * 2; i <= mpx[l.b] * 2; i++)
      d[mpy[l.c] * 2][i] = -1;
  }
  for (auto l : lv) {
    for (int i = mpy[l.a] * 2; i <= mpy[l.b] * 2; i++)
      d[i][mpx[l.c] * 2] = -1;
  }
  queue<pair<int, int>> to_visit;
  int x = mpx[0] * 2, y = mpy[0] * 2;
  to_visit.push(make_pair(x, y));
  ll sum = 0;
  bool inf = false;
  while (!to_visit.empty()) {
    x = to_visit.front().first;
    y = to_visit.front().second;
    if (x <= 0 || w - 1 <= x || y <= 0 || h - 1 <= y) {
      // cout << "INF" << endl;
      inf = true;
      break;
    }
    to_visit.pop();
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (d[yy][xx] == -1 || d[yy][xx] == 1)
        continue;
      d[yy][xx] = 1;
      if (xx % 2 == 1 && yy % 2 == 1) {
        sum += (xs[(xx + 1) / 2] - xs[(xx - 1) / 2]) *
               (ys[(yy + 1) / 2] - ys[(yy - 1) / 2]);
      }
      to_visit.push(make_pair(xx, yy));
    }
  }
  if (inf)
    cout << "INF" << endl;
  else
    cout << sum << endl;
  return 0;
}