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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll INF = 1e9 + 10;

void compress_coordinate(VL x, map<ll, int> &zip, VL &unzip) {
  int n = x.size();
  REP(i, n) unzip.push_back(x[i]);
  sort(ALL(unzip));
  unzip.erase(unique(ALL(unzip)), unzip.end());
  REP(i, unzip.size()) zip[unzip[i]] = i;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  VL a(n), b(n), c(n), d(m), e(m), f(m), xs, ys;
  REP(i, n) {
    a[i] = in();
    b[i] = in();
    c[i] = in();
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }
  REP(i, m) {
    d[i] = in();
    e[i] = in();
    f[i] = in();
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }
  xs.push_back(0);
  xs.push_back(-INF);
  xs.push_back(INF);
  ys.push_back(0);
  ys.push_back(-INF);
  ys.push_back(INF);

  map<ll, int> zipx, zipy;
  VL unzipx, unzipy;
  compress_coordinate(xs, zipx, unzipx);
  compress_coordinate(ys, zipy, unzipy);

  int szx = unzipx.size() - 1, szy = unzipy.size() - 1;
  VVI wallx(szx + 1, VI(szy + 1)), wally(wallx);
  REP(i, m) {
    d[i] = zipx[d[i]];
    e[i] = zipy[e[i]];
    f[i] = zipy[f[i]];
    FOR(p, e[i], f[i] - 1) wallx[d[i] - 1][p] = 1;
  }
  REP(i, n) {
    a[i] = zipx[a[i]];
    b[i] = zipx[b[i]];
    c[i] = zipy[c[i]];
    FOR(p, a[i], b[i] - 1) wally[p][c[i] - 1] = 1;
  }
  VVI visit(szx + 1, VI(szy + 1));
  queue<P> que;
  int sx = zipx[0], sy = zipy[0];
  visit[sx][sy] = 1;
  que.push(P(sx, sy));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    vector<P> next;
    if (!wallx[x][y])
      next.push_back(P(x + 1, y));
    if (x > 0 && !wallx[x - 1][y])
      next.push_back(P(x - 1, y));
    if (!wally[x][y])
      next.push_back(P(x, y + 1));
    if (y > 0 && !wally[x][y - 1])
      next.push_back(P(x, y - 1));
    for (P q : next) {
      int xx = q.first, yy = q.second;
      if (xx == 0 || xx == szx - 1 || yy == 0 || yy == szy - 1) {
        cout << "INF" << endl;
        return 0;
      }
      if (visit[xx][yy])
        continue;
      visit[xx][yy] = 1;
      que.push(q);
    }
  }
  ll ans = 0;
  REP(i, szx) REP(j, szy) {
    if (!visit[i][j])
      continue;
    ll dx = unzipx[i + 1] - unzipx[i];
    ll dy = unzipy[j + 1] - unzipy[j];
    ans += dx * dy;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll INF = 1e9 + 10;

void compress_coordinate(VL x, map<ll, int> &zip, VL &unzip) {
  int n = x.size();
  REP(i, n) unzip.push_back(x[i]);
  sort(ALL(unzip));
  unzip.erase(unique(ALL(unzip)), unzip.end());
  REP(i, unzip.size()) zip[unzip[i]] = i;
}

int main(void) {
  int n, m;
  cin >> n >> m;
  VL a(n), b(n), c(n), d(m), e(m), f(m), xs, ys;
  REP(i, n) {
    a[i] = in();
    b[i] = in();
    c[i] = in();
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }
  REP(i, m) {
    d[i] = in();
    e[i] = in();
    f[i] = in();
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }
  xs.push_back(0);
  xs.push_back(-INF);
  xs.push_back(INF);
  ys.push_back(0);
  ys.push_back(-INF);
  ys.push_back(INF);

  map<ll, int> zipx, zipy;
  VL unzipx, unzipy;
  compress_coordinate(xs, zipx, unzipx);
  compress_coordinate(ys, zipy, unzipy);

  int szx = unzipx.size() - 1, szy = unzipy.size() - 1;
  VVI wallx(szx, VI(szy)), wally(wallx);
  REP(i, m) {
    d[i] = zipx[d[i]];
    e[i] = zipy[e[i]];
    f[i] = zipy[f[i]];
    FOR(p, e[i], f[i] - 1) wallx[d[i] - 1][p] = 1;
  }
  REP(i, n) {
    a[i] = zipx[a[i]];
    b[i] = zipx[b[i]];
    c[i] = zipy[c[i]];
    FOR(p, a[i], b[i] - 1) wally[p][c[i] - 1] = 1;
  }
  VVI visit(szx, VI(szy));
  queue<P> que;
  int sx = zipx[0], sy = zipy[0];
  visit[sx][sy] = 1;
  que.push(P(sx, sy));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    vector<P> next;
    if (!wallx[x][y])
      next.push_back(P(x + 1, y));
    if (x > 0 && !wallx[x - 1][y])
      next.push_back(P(x - 1, y));
    if (!wally[x][y])
      next.push_back(P(x, y + 1));
    if (y > 0 && !wally[x][y - 1])
      next.push_back(P(x, y - 1));
    for (P q : next) {
      int xx = q.first, yy = q.second;
      if (xx == 0 || xx == szx - 1 || yy == 0 || yy == szy - 1) {
        cout << "INF" << endl;
        return 0;
      }
      if (visit[xx][yy])
        continue;
      visit[xx][yy] = 1;
      que.push(q);
    }
  }
  ll ans = 0;
  REP(i, szx) REP(j, szy) {
    if (!visit[i][j])
      continue;
    ll dx = unzipx[i + 1] - unzipx[i];
    ll dy = unzipy[j + 1] - unzipy[j];
    ans += dx * dy;
  }
  cout << ans << endl;

  return 0;
}