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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
int n, m;
int a[1005], b[1005], c[1005];
int d[1005], e[1005], f[1005];
int G[6005][6005];
int vis[6005][6005];
ll ans = 0;
int w[6005], h[6005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector<int> _X, _Y;
unordered_map<int, int> _mX, _mY;
queue<ii> q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &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++) {
    scanf("%d%d%d", &d[i], &e[i], &f[i]);
    _X.emplace_back(d[i]);
    _Y.emplace_back(e[i]);
    _Y.emplace_back(f[i]);
  }
  _X.emplace_back(0);
  _Y.emplace_back(0);
  sort(_X.begin(), _X.end());
  sort(_Y.begin(), _Y.end());
  _X.erase(unique(_X.begin(), _X.end()), _X.end());
  _Y.erase(unique(_Y.begin(), _Y.end()), _Y.end());
  // printf("X: "); for (auto x:  _X) printf("%d ",x); printf("\\n");
  // printf("Y: "); for (auto x:  _Y) printf("%d ",x); printf("\\n");
  for (int i = 0; i < _X.size(); i++)
    _mX[_X[i]] = i;
  for (int i = 0; i < _Y.size(); i++)
    _mY[_Y[i]] = i;
  for (int i = 0; i < _X.size() - 1; i++) {
    h[i * 2 + 2] = _X[i + 1] - _X[i];
    // printf("%d ",h[i*2+2]);
  }
  for (int i = 0; i < _Y.size() - 1; i++) {
    w[i * 2 + 2] = _Y[i + 1] - _Y[i];
    // printf("%d ",w[i*2+2]);
  }
  for (int i = 0; i < n; i++) {
    a[i] = 2 * _mX[a[i]] + 1;
    b[i] = 2 * _mX[b[i]] + 1;
    c[i] = 2 * _mY[c[i]] + 1;
    // printf("abc %d %d %d\\n",a[i],b[i],c[i]);
    for (int J = a[i]; J <= b[i]; J++) {
      G[J][c[i]] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    d[i] = 2 * _mX[d[i]] + 1;
    e[i] = 2 * _mY[e[i]] + 1;
    f[i] = 2 * _mY[f[i]] + 1;
    // printf("def %d %d %d\\n",d[i],e[i],f[i]);
    for (int J = e[i]; J <= f[i]; J++) {
      G[d[i]][J] = 1;
    }
  }
  /*for (int i = 0; i <= 2*n+1+m; i++){
      for (int j = 0; j <= 2*m+n+1; j++){
          printf("%d",G[i][j]);
      }
      printf("\\n");
  }*/
  int sx = 2 * _mX[0] + 1;
  int sy = 2 * _mY[0] + 1;
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (G[nx][ny] == 0 && vis[nx][ny] == 0) {
        vis[nx][ny] = 1;
        if (nx == 0 || ny == 0 || nx == 2 * _X.size() || ny == 2 * _Y.size()) {
          printf("INF");
          return 0;
        }
        q.push({nx, ny});
        ans += (ll)h[nx] * w[ny];
      }
    }
  }
  printf("%lld", ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
int n, m;
int a[1005], b[1005], c[1005];
int d[1005], e[1005], f[1005];
int G[3005][3005];
int vis[3005][3005];
ll ans = 0;
int w[3005], h[3005];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector<int> _X, _Y;
unordered_map<int, int> _mX, _mY;
queue<ii> q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &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++) {
    scanf("%d%d%d", &d[i], &e[i], &f[i]);
    _X.emplace_back(d[i]);
    _Y.emplace_back(e[i]);
    _Y.emplace_back(f[i]);
  }
  _X.emplace_back(0);
  _Y.emplace_back(0);
  sort(_X.begin(), _X.end());
  sort(_Y.begin(), _Y.end());
  _X.erase(unique(_X.begin(), _X.end()), _X.end());
  _Y.erase(unique(_Y.begin(), _Y.end()), _Y.end());
  // printf("X: "); for (auto x:  _X) printf("%d ",x); printf("\\n");
  // printf("Y: "); for (auto x:  _Y) printf("%d ",x); printf("\\n");
  for (int i = 0; i < _X.size(); i++)
    _mX[_X[i]] = i;
  for (int i = 0; i < _Y.size(); i++)
    _mY[_Y[i]] = i;
  for (int i = 0; i < _X.size() - 1; i++) {
    h[i * 2 + 2] = _X[i + 1] - _X[i];
    // printf("%d ",h[i*2+2]);
  }
  for (int i = 0; i < _Y.size() - 1; i++) {
    w[i * 2 + 2] = _Y[i + 1] - _Y[i];
    // printf("%d ",w[i*2+2]);
  }
  for (int i = 0; i < n; i++) {
    a[i] = 2 * _mX[a[i]] + 1;
    b[i] = 2 * _mX[b[i]] + 1;
    c[i] = 2 * _mY[c[i]] + 1;
    // printf("abc %d %d %d\\n",a[i],b[i],c[i]);
    for (int J = a[i]; J <= b[i]; J++) {
      G[J][c[i]] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    d[i] = 2 * _mX[d[i]] + 1;
    e[i] = 2 * _mY[e[i]] + 1;
    f[i] = 2 * _mY[f[i]] + 1;
    // printf("def %d %d %d\\n",d[i],e[i],f[i]);
    for (int J = e[i]; J <= f[i]; J++) {
      G[d[i]][J] = 1;
    }
  }
  /*for (int i = 0; i <= 2*n+1+m; i++){
      for (int j = 0; j <= 2*m+n+1; j++){
          printf("%d",G[i][j]);
      }
      printf("\\n");
  }*/
  int sx = 2 * _mX[0] + 1;
  int sy = 2 * _mY[0] + 1;
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (G[nx][ny] == 0 && vis[nx][ny] == 0) {
        vis[nx][ny] = 1;
        if (nx == 0 || ny == 0 || nx == 2 * _X.size() || ny == 2 * _Y.size()) {
          printf("INF");
          return 0;
        }
        q.push({nx, ny});
        ans += (ll)h[nx] * w[ny];
      }
    }
  }
  printf("%lld", ans);
}