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

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

bool G[3010][3010][2];
bool checked[3010][3010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<pair<pii, pii>> XE(M), YE(N);

  rep(i, 0, N) {
    cin >> YE[i].first.second >> YE[i].second.second >> YE[i].first.first;
    YE[i].second.first = YE[i].first.first;
  }
  rep(i, 0, M) {
    cin >> XE[i].first.second >> XE[i].first.first >> XE[i].second.first;
    XE[i].second.second = XE[i].first.second;
  }

  vector<int> XV, YV;
  XV.push_back(0), YV.push_back(0);
  rep(i, 0, M) {
    XV.push_back(XE[i].first.first);
    XV.push_back(XE[i].second.first);
    YV.push_back(XE[i].first.second);
    YV.push_back(XE[i].second.second);
  }
  rep(i, 0, N) {
    XV.push_back(YE[i].first.first);
    XV.push_back(YE[i].second.first);
    YV.push_back(YE[i].first.second);
    YV.push_back(YE[i].second.second);
  }

  sort(all(XV));
  sort(all(YV));
  XV.erase(unique(all(XV)), XV.end());
  YV.erase(unique(all(YV)), YV.end());

  int H = SZ(YV) - 1, W = SZ(XV) - 1;

  rep(i, 0, M) {
    int lx = lower_bound(all(XV), XE[i].first.first) - XV.begin();
    int rx = lower_bound(all(XV), XE[i].second.first) - XV.begin();
    int y = lower_bound(all(YV), XE[i].first.second) - YV.begin();
    rep(j, lx, rx) { G[y][j][0] = true; }
  }
  rep(i, 0, N) {
    int x = lower_bound(all(XV), YE[i].first.first) - XV.begin();
    int uy = lower_bound(all(YV), YE[i].first.second) - YV.begin();
    int dy = lower_bound(all(YV), YE[i].second.second) - YV.begin();
    rep(j, uy, dy) { G[j][x][1] = true; }
  }

  queue<pii> q;
  pii sp;
  sp.first = lower_bound(all(XV), 0) - XV.begin();
  sp.second = lower_bound(all(YV), 0) - YV.begin();
  q.push(sp);
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  checked[sp.second][sp.first] = true;
  bool inf = false;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    if (!G[p.second][p.first][1]) {
      if (p.first == 0) {
        inf = true;
        break;
      } else {
        if (!checked[p.second][p.first - 1]) {
          checked[p.second][p.first - 1] = true;
          q.push(mp(p.first - 1, p.second));
        }
      }
    }

    if (!G[p.second][p.first + 1][1]) {
      if (p.first == W - 1) {
        inf = true;
        break;
      } else {
        if (!checked[p.second][p.first + 1]) {
          checked[p.second][p.first + 1] = true;
          q.push(mp(p.first + 1, p.second));
        }
      }
    }

    if (!G[p.second][p.first][0]) {
      if (p.second == 0) {
        inf = true;
        break;
      } else {
        if (!checked[p.second - 1][p.first]) {
          checked[p.second - 1][p.first] = true;
          q.push(mp(p.first, p.second - 1));
        }
      }
    }

    if (!G[p.second + 1][p.first][0]) {
      if (p.second == H - 1) {
        inf = true;
        break;
      } else {
        if (!checked[p.second + 1][p.first]) {
          checked[p.second + 1][p.first] = true;
          q.push(mp(p.first, p.second + 1));
        }
      }
    }
  }

  if (inf) {
    cout << "INF" << endl;
  } else {
    int ans = 0;
    rep(i, 0, H) rep(j, 0, W) {
      if (checked[i][j]) {
        ans += (XV[j + 1] - XV[j]) * (YV[i + 1] - YV[i]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

bool G[1010][1010][2];
bool checked[1010][1010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<pair<pii, pii>> XE(M), YE(N);

  rep(i, 0, N) {
    cin >> YE[i].first.second >> YE[i].second.second >> YE[i].first.first;
    YE[i].second.first = YE[i].first.first;
  }
  rep(i, 0, M) {
    cin >> XE[i].first.second >> XE[i].first.first >> XE[i].second.first;
    XE[i].second.second = XE[i].first.second;
  }

  vector<int> XV, YV;
  XV.push_back(0), YV.push_back(0);
  rep(i, 0, M) {
    XV.push_back(XE[i].first.first);
    XV.push_back(XE[i].second.first);
    YV.push_back(XE[i].first.second);
    YV.push_back(XE[i].second.second);
  }
  rep(i, 0, N) {
    XV.push_back(YE[i].first.first);
    XV.push_back(YE[i].second.first);
    YV.push_back(YE[i].first.second);
    YV.push_back(YE[i].second.second);
  }

  sort(all(XV));
  sort(all(YV));
  XV.erase(unique(all(XV)), XV.end());
  YV.erase(unique(all(YV)), YV.end());

  int H = SZ(YV) - 1, W = SZ(XV) - 1;

  rep(i, 0, M) {
    int lx = lower_bound(all(XV), XE[i].first.first) - XV.begin();
    int rx = lower_bound(all(XV), XE[i].second.first) - XV.begin();
    int y = lower_bound(all(YV), XE[i].first.second) - YV.begin();
    rep(j, lx, rx) { G[y][j][0] = true; }
  }
  rep(i, 0, N) {
    int x = lower_bound(all(XV), YE[i].first.first) - XV.begin();
    int uy = lower_bound(all(YV), YE[i].first.second) - YV.begin();
    int dy = lower_bound(all(YV), YE[i].second.second) - YV.begin();
    rep(j, uy, dy) { G[j][x][1] = true; }
  }

  queue<pii> q;
  pii sp;
  sp.first = lower_bound(all(XV), 0) - XV.begin();
  sp.second = lower_bound(all(YV), 0) - YV.begin();
  q.push(sp);
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  checked[sp.second][sp.first] = true;
  bool inf = false;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    if (!G[p.second][p.first][1]) {
      if (p.first == 0) {
        inf = true;
        break;
      } else {
        if (!checked[p.second][p.first - 1]) {
          checked[p.second][p.first - 1] = true;
          q.push(mp(p.first - 1, p.second));
        }
      }
    }

    if (!G[p.second][p.first + 1][1]) {
      if (p.first == W - 1) {
        inf = true;
        break;
      } else {
        if (!checked[p.second][p.first + 1]) {
          checked[p.second][p.first + 1] = true;
          q.push(mp(p.first + 1, p.second));
        }
      }
    }

    if (!G[p.second][p.first][0]) {
      if (p.second == 0) {
        inf = true;
        break;
      } else {
        if (!checked[p.second - 1][p.first]) {
          checked[p.second - 1][p.first] = true;
          q.push(mp(p.first, p.second - 1));
        }
      }
    }

    if (!G[p.second + 1][p.first][0]) {
      if (p.second == H - 1) {
        inf = true;
        break;
      } else {
        if (!checked[p.second + 1][p.first]) {
          checked[p.second + 1][p.first] = true;
          q.push(mp(p.first, p.second + 1));
        }
      }
    }
  }

  if (inf) {
    cout << "INF" << endl;
  } else {
    int ans = 0;
    rep(i, 0, H) rep(j, 0, W) {
      if (checked[i][j]) {
        ans += (XV[j + 1] - XV[j]) * (YV[i + 1] - YV[i]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}