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

#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

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

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 1000000010;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

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

void remove_duplication(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

ll ans = 0;
int H, W;
bool wall[3030][3030][4], vis[3030][3030];
VI h{0, INF, -INF}, w{0, INF, -INF};

void Main() {
  int N, M;
  cin >> N >> M;
  VI A(N), B(N), C(N), D(M), E(M), F(M);
  REP(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    h.push_back(A[i]);
    h.push_back(B[i]);
    w.push_back(C[i]);
  }
  REP(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    h.push_back(D[i]);
    w.push_back(E[i]);
    w.push_back(F[i]);
  }

  remove_duplication(h);
  remove_duplication(w);
  H = h.size();
  W = w.size();
  map<int, int> mh, mw;
  REP(i, H) mh[h[i]] = i;
  REP(i, W) mw[w[i]] = i;

  REP(i, N) {
    int x1 = mh[A[i]], x2 = mh[B[i]], y = mw[C[i]];
    FOR(j, x1, x2) wall[j][y][2] = true;
    FOR(j, x1, x2) wall[j][y - 1][3] = true;
  }
  REP(i, M) {
    int x = mh[D[i]], y1 = mw[E[i]], y2 = mw[F[i]];
    FOR(j, y1, y2) wall[x][j][0] = true;
    FOR(j, y1, y2) wall[x - 1][j][1] = true;
  }

  stack<P> s;
  s.emplace(mh[0], mw[0]);
  while (!s.empty()) {
    P p = s.top();
    s.pop();
    int i = p.first, j = p.second;
    if (i < 0 || j < 0 || i >= H || j >= W)
      continue;
    if (vis[i][j])
      continue;
    vis[i][j] = true;

    ll mass_h, mass_w;
    if (i == H - 1)
      mass_h = 1;
    else
      mass_h = h[i + 1] - h[i];
    if (j == W - 1)
      mass_w = 1;
    else
      mass_w = w[j + 1] - w[j];
    ans += mass_h * mass_w;

    if (wall[i][j][0] == false)
      s.emplace(i - 1, j);
    if (wall[i][j][1] == false)
      s.emplace(i + 1, j);
    if (wall[i][j][2] == false)
      s.emplace(i, j - 1);
    if (wall[i][j][3] == false)
      s.emplace(i, j + 1);
  }

  bool flag = false;
  REP(i, H) if (vis[i][0] || vis[i][W - 1]) flag = true;
  REP(i, W) if (vis[0][i] || vis[H - 1][i]) flag = true;

  if (flag)
    cout << "INF" << en;
  else
    cout << ans << en;
  return;
}

int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

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

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 1000000010;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

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

void remove_duplication(vector<int> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

ll ans = 0;
int H, W;
bool wall[3030][3030][4], vis[3030][3030];
VI h{0, INF, -INF}, w{0, INF, -INF};

void dfs(int i, int j) {
  if (i < 0 || j < 0 || i >= H || j >= W)
    return;
  if (vis[i][j])
    return;
  vis[i][j] = true;

  ans += (h[i + 1] - h[i]) * (w[j + 1] - w[j]);

  if (wall[i][j][0] == false)
    dfs(i - 1, j);
  if (wall[i][j][1] == false)
    dfs(i + 1, j);
  if (wall[i][j][2] == false)
    dfs(i, j - 1);
  if (wall[i][j][3] == false)
    dfs(i, j + 1);

  return;
}

void Main() {
  int N, M;
  cin >> N >> M;
  VI A(N), B(N), C(N), D(M), E(M), F(M);
  REP(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    h.push_back(A[i]);
    h.push_back(B[i]);
    w.push_back(C[i]);
  }
  REP(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    h.push_back(D[i]);
    w.push_back(E[i]);
    w.push_back(F[i]);
  }

  remove_duplication(h);
  remove_duplication(w);
  H = h.size();
  W = w.size();
  h.push_back(h.back() + 5);
  w.push_back(w.back() + 5);
  map<int, int> mh, mw;
  REP(i, H) mh[h[i]] = i;
  REP(i, W) mw[w[i]] = i;

  REP(i, N) {
    int x1 = mh[A[i]], x2 = mh[B[i]], y = mw[C[i]];
    FOR(j, x1, x2) wall[j][y][2] = true;
    FOR(j, x1, x2) wall[j][y - 1][3] = true;
  }
  REP(i, M) {
    int x = mh[D[i]], y1 = mw[E[i]], y2 = mw[F[i]];
    FOR(j, y1, y2) wall[x][j][0] = true;
    FOR(j, y1, y2) wall[x - 1][j][1] = true;
  }

  dfs(mh[0], mw[0]);

  bool flag = false;
  REP(i, H) if (vis[i][0] || vis[i][W - 1]) flag = true;
  REP(i, W) if (vis[0][i] || vis[H - 1][i]) flag = true;

  if (flag)
    cout << "INF" << en;
  else
    cout << ans << en;
  return;
}

int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}