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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;

ll a[3007], b[3007], c[3007];
ll d[3007], e[3007], f[3007];
int ngx[3007][3007];
int ngy[3007][3007];
bool visit[3007][3007];

ll ind(int key, const vector<int> &v) {
  return lower_bound(all(v), key) - v.begin();
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x, y;

  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    x.push_back(a[i]);
    x.pb(b[i]);
    y.push_back(c[i]);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    x.push_back(d[i]);
    y.pb(e[i]);
    y.push_back(f[i]);
  }

  sort(all(x));
  sort(all(y));
  x.erase(unique(all(x)), x.end());
  y.erase(unique(all(y)), y.end());

  rep(i, n) {
    int from = ind(a[i], x);
    int to = ind(b[i], x);
    int ycor = ind(c[i], y);
    FOR(j, from, to) { ngx[j][ycor] = true; }
  }

  rep(i, m) {
    int from = ind(e[i], y);
    int to = ind(f[i], y);
    int xcor = ind(d[i], x);
    FOR(j, from, to) { ngy[xcor][j] = true; }
  }

  int xs = ind(0, x) - 1;
  int ys = ind(0, y) - 1;
  queue<pair<int, int>> q;
  q.emplace(xs, ys);
  ll res = 0;

  int w = x.size();
  int h = y.size();

  while (!q.empty()) {
    int xx = q.front().first;
    int yy = q.front().second;
    q.pop();

    if (xx < 0 || yy < 0 || xx >= w - 1 || yy >= h - 1) {
      cout << "INF" << endl;
      return 0;
    }

    if (visit[xx][yy])
      continue;

    res += ((ll)x[xx + 1] - x[xx]) * ((ll)y[yy + 1] - y[yy]);

    if (!ngx[xx][yy])
      q.emplace(xx, yy - 1);
    if (!ngx[xx][yy + 1])
      q.emplace(xx, yy + 1);
    if (!ngy[xx][yy])
      q.emplace(xx - 1, yy);
    if (!ngy[xx + 1][yy])
      q.emplace(xx + 1, yy);

    visit[xx][yy] = 1;
  }

  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;

ll a[2000], b[2000], c[2000];
ll d[2000], e[2000], f[2000];
int ngx[2000][2000];
int ngy[2000][2000];
bool visit[2000][2000];

ll ind(int key, const vector<int> &v) {
  return lower_bound(all(v), key) - v.begin();
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x, y;

  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    x.push_back(a[i]);
    x.pb(b[i]);
    y.push_back(c[i]);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    x.push_back(d[i]);
    y.pb(e[i]);
    y.push_back(f[i]);
  }

  sort(all(x));
  sort(all(y));
  x.erase(unique(all(x)), x.end());
  y.erase(unique(all(y)), y.end());

  rep(i, n) {
    int from = ind(a[i], x);
    int to = ind(b[i], x);
    int ycor = ind(c[i], y);
    FOR(j, from, to) { ngx[j][ycor] = true; }
  }

  rep(i, m) {
    int from = ind(e[i], y);
    int to = ind(f[i], y);
    int xcor = ind(d[i], x);
    FOR(j, from, to) { ngy[xcor][j] = true; }
  }

  int xs = ind(0, x) - 1;
  int ys = ind(0, y) - 1;
  queue<pair<int, int>> q;
  q.emplace(xs, ys);
  ll res = 0;

  int w = x.size();
  int h = y.size();

  while (!q.empty()) {
    int xx = q.front().first;
    int yy = q.front().second;
    q.pop();

    if (xx < 0 || yy < 0 || xx >= w - 1 || yy >= h - 1) {
      cout << "INF" << endl;
      return 0;
    }

    if (visit[xx][yy])
      continue;

    res += ((ll)x[xx + 1] - x[xx]) * ((ll)y[yy + 1] - y[yy]);

    if (!ngx[xx][yy])
      q.emplace(xx, yy - 1);
    if (!ngx[xx][yy + 1])
      q.emplace(xx, yy + 1);
    if (!ngy[xx][yy])
      q.emplace(xx - 1, yy);
    if (!ngy[xx + 1][yy])
      q.emplace(xx + 1, yy);

    visit[xx][yy] = 1;
  }

  cout << res << endl;

  return 0;
}