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

#include <bits/stdc++.h>
#define ref(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define INF 1111111111

int n, m;
int a[1001], b[1001], c[1000], d[1000], e[1000], f[1000];
int U[4000][4000], L[4000][4000];
typedef pair<int, int> P;
vector<vector<bool>> vis;
typedef long long ll;

int id(vector<ll> h, ll a) {
  return lower_bound(h.begin(), h.end(), a) - h.begin();
}

int main() {

  cin >> n >> m;
  vector<ll> h, w;
  ref(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    h.push_back(a[i]);
    h.push_back(b[i]);
    w.push_back(c[i]);
  }
  ref(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    h.push_back(d[i]);
    w.push_back(f[i]);
    w.push_back(e[i]);
  }
  h.push_back(INF);
  h.push_back(-INF);
  w.push_back(INF);
  w.push_back(-INF);
  h.push_back(0);
  w.push_back(0);
  sort(h.begin(), h.end());
  sort(w.begin(), w.end());
  h.erase(unique(h.begin(), h.end()), h.end());
  w.erase(unique(w.begin(), w.end()), w.end());

  vis = vector<vector<bool>>(h.size(), vector<bool>(w.size(), false));

  ref(i, n) {
    L[id(h, a[i])][id(w, c[i])]++;
    L[id(h, b[i])][id(w, c[i])]--;
  }
  ref(i, m) {
    U[id(h, d[i])][id(w, e[i])]++;
    U[id(h, d[i])][id(w, f[i])]--;
  }
  for (int i = 1; i < h.size(); i++) {
    for (int j = 1; j < w.size(); j++) {
      L[i][j] += L[i - 1][j];
      U[i][j] += U[i][j - 1];
    }
  }

  queue<P> q;
  q.push({id(h, 0), id(w, 0)});
  ll ans = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (!x || !y || x == h.size() - 1 || y == w.size() - 1) {
      cout << "INF";
      return 0;
    }
    if (!vis[x][y]) {
      vis[x][y] = 1;
      ans += (h[x + 1] - h[x]) * (w[y + 1] - w[y]);
      if (!U[x][y])
        q.push({x - 1, y});
      if (!L[x][y])
        q.push({x, y - 1});
      if (!U[x + 1][y])
        q.push({x + 1, y});
      if (!L[x][y + 1])
        q.push({x, y + 1});
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ref(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define INF 1111111111

int n, m;
int a[1001], b[1001], c[1000], d[1000], e[1000], f[1000];
int U[3000][3000], L[3000][3000];
typedef pair<int, int> P;
vector<vector<bool>> vis;
typedef long long ll;

int id(vector<ll> h, ll a) {
  return lower_bound(h.begin(), h.end(), a) - h.begin();
}

int main() {

  cin >> n >> m;
  vector<ll> h, w;
  ref(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    h.push_back(a[i]);
    h.push_back(b[i]);
    w.push_back(c[i]);
  }
  ref(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    h.push_back(d[i]);
    w.push_back(f[i]);
    w.push_back(e[i]);
  }
  h.push_back(INF);
  h.push_back(-INF);
  w.push_back(INF);
  w.push_back(-INF);
  h.push_back(0);
  w.push_back(0);
  sort(h.begin(), h.end());
  sort(w.begin(), w.end());
  h.erase(unique(h.begin(), h.end()), h.end());
  w.erase(unique(w.begin(), w.end()), w.end());

  vis = vector<vector<bool>>(h.size(), vector<bool>(w.size(), false));

  ref(i, n) {
    L[id(h, a[i])][id(w, c[i])]++;
    L[id(h, b[i])][id(w, c[i])]--;
  }
  ref(i, m) {
    U[id(h, d[i])][id(w, e[i])]++;
    U[id(h, d[i])][id(w, f[i])]--;
  }
  for (int i = 1; i < h.size(); i++) {
    for (int j = 1; j < w.size(); j++) {
      L[i][j] += L[i - 1][j];
      U[i][j] += U[i][j - 1];
    }
  }

  queue<P> q;
  q.push({id(h, 0), id(w, 0)});
  ll ans = 0;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (!x || !y || x == h.size() - 1 || y == w.size() - 1) {
      cout << "INF";
      return 0;
    }
    if (!vis[x][y]) {
      vis[x][y] = 1;
      ans += (h[x + 1] - h[x]) * (w[y + 1] - w[y]);
      if (!U[x][y])
        q.push({x - 1, y});
      if (!L[x][y])
        q.push({x, y - 1});
      if (!U[x + 1][y])
        q.push({x + 1, y});
      if (!L[x][y + 1])
        q.push({x, y + 1});
    }
  }
  cout << ans;
  return 0;
}