Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

// 座標圧縮
// vector<ll> rev; // 圧縮後の値から元の値への写像
vector<int> normalize(const vector<ll> v) {
  const int N = v.size();
  vector<int> res(N);
  vector<pair<ll, int>> t(N);
  // rev = vector<ll>(N);
  REP(i, N) t[i] = {v[i], i};
  sort(ALL(t));
  int r = 0;
  REP(i, N) {
    r += (i > 0 && t[i - 1].first != t[i].first);
    res[t[i].second] = r;
    // rev[r] = t[i].first;
  }
  return res;
}

vector<ll> bai(const vector<ll> &a) {
  vector<ll> res;
  REP(i, a.size()) {
    res.push_back(a[i]);
    res.push_back(a[i]);
  }
  return res;
}

bool wall[3010][3010];
bool vis[3010][3010];

int main2() {
  CLR(wall, 0);
  CLR(vis, 0);

  int N = nextLong();
  int M = nextLong();

  const ll B = 1e9 + 10;
  vector<ll> xs({-B, 0, B}), ys({-B, 0, B});
  vector<int> tate, yoko;
  REP(i, N) {
    int A = nextLong();
    int B = nextLong();
    int C = nextLong();
    tate.push_back(A);
    tate.push_back(B);
    tate.push_back(C);
    xs.push_back(A);
    xs.push_back(B);
    ys.push_back(C);
  }
  REP(i, M) {
    int D = nextLong();
    int E = nextLong();
    int F = nextLong();
    yoko.push_back(D);
    yoko.push_back(E);
    yoko.push_back(F);
    xs.push_back(D);
    ys.push_back(E);
    ys.push_back(F);
  }

  sort(ALL(xs));
  xs.erase(unique(ALL(xs)), xs.end());
  sort(ALL(ys));
  ys.erase(unique(ALL(ys)), ys.end());

  xs = bai(xs);
  ys = bai(ys);

  const int X = xs.size();
  const int Y = ys.size();

  REP(i, N) {
    int a = next(lower_bound(ALL(xs), tate[i * 3])) - xs.begin();
    int b = next(lower_bound(ALL(xs), tate[i * 3 + 1])) - xs.begin();
    int c = next(lower_bound(ALL(ys), tate[i * 3 + 2])) - ys.begin();
    for (int x = a; x <= b; x++)
      wall[x][c] = true;
  }
  REP(i, M) {
    int d = next(lower_bound(ALL(xs), yoko[i * 3])) - xs.begin();
    int e = next(lower_bound(ALL(ys), yoko[i * 3 + 1])) - ys.begin();
    int f = next(lower_bound(ALL(ys), yoko[i * 3 + 2])) - ys.begin();
    for (int y = e; y <= f; y++)
      wall[d][y] = true;
  }

  queue<pair<int, int>> q;
  {
    int x = next(lower_bound(ALL(xs), 0)) - xs.begin();
    int y = next(lower_bound(ALL(ys), 0)) - ys.begin();
    q.push({x, y});
    vis[x][y] = true;
  }

  const int dx[] = {0, 1, 0, -1};
  const int dy[] = {1, 0, -1, 0};

  bool inf = false;
  ll ans = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();

    {
      if (x == 0 || y == 0) {
        inf = true;
        break;
      }
      ans += (xs[x] - xs[x - 1]) * (ys[y] - ys[y - 1]);
    }

    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < X && 0 <= ny && ny < Y && !wall[nx][ny] &&
          !vis[nx][ny]) {
        vis[nx][ny] = true;
        q.push({nx, ny});
      }
    }
  }

  // REP(i, X) {
  //   REP(j, Y) {
  //     cout << (wall[i][j] ? 1 : 0);
  //   }
  //   cout << endl;
  // }

  // REP(i, X) {

  //   printf("%20d : ", xs[i]);
  //   REP(j, Y) {
  //     cout << (vis[i][j] ? 1 : 0);
  //   }
  //   cout << endl;
  // }

  if (inf) {
    cout << "INF" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}

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

typedef long long ll;
#define REP(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
#define ALL(v) (v).begin(), (v).end()
#define CLR(t, v) memset(t, (v), sizeof(t))
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  return os << "(" << a.first << "," << a.second << ")";
}
template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << (*i) << " ";
  cout << endl;
}
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

ll nextLong() {
  ll x;
  scanf("%lld", &x);
  return x;
}

// 座標圧縮
// vector<ll> rev; // 圧縮後の値から元の値への写像
vector<int> normalize(const vector<ll> v) {
  const int N = v.size();
  vector<int> res(N);
  vector<pair<ll, int>> t(N);
  // rev = vector<ll>(N);
  REP(i, N) t[i] = {v[i], i};
  sort(ALL(t));
  int r = 0;
  REP(i, N) {
    r += (i > 0 && t[i - 1].first != t[i].first);
    res[t[i].second] = r;
    // rev[r] = t[i].first;
  }
  return res;
}

vector<ll> bai(const vector<ll> &a) {
  vector<ll> res;
  REP(i, a.size()) {
    res.push_back(a[i]);
    res.push_back(a[i]);
  }
  return res;
}

const int SZ = 6010;
bool wall[SZ][SZ];
bool vis[SZ][SZ];

int main2() {
  CLR(wall, 0);
  CLR(vis, 0);

  int N = nextLong();
  int M = nextLong();

  const ll B = 1e9 + 10;
  vector<ll> xs({-B, 0, B}), ys({-B, 0, B});
  vector<int> tate, yoko;
  REP(i, N) {
    int A = nextLong();
    int B = nextLong();
    int C = nextLong();
    tate.push_back(A);
    tate.push_back(B);
    tate.push_back(C);
    xs.push_back(A);
    xs.push_back(B);
    ys.push_back(C);
  }
  REP(i, M) {
    int D = nextLong();
    int E = nextLong();
    int F = nextLong();
    yoko.push_back(D);
    yoko.push_back(E);
    yoko.push_back(F);
    xs.push_back(D);
    ys.push_back(E);
    ys.push_back(F);
  }

  sort(ALL(xs));
  xs.erase(unique(ALL(xs)), xs.end());
  sort(ALL(ys));
  ys.erase(unique(ALL(ys)), ys.end());

  xs = bai(xs);
  ys = bai(ys);

  const int X = xs.size();
  const int Y = ys.size();

  REP(i, N) {
    int a = next(lower_bound(ALL(xs), tate[i * 3])) - xs.begin();
    int b = next(lower_bound(ALL(xs), tate[i * 3 + 1])) - xs.begin();
    int c = next(lower_bound(ALL(ys), tate[i * 3 + 2])) - ys.begin();
    for (int x = a; x <= b; x++)
      wall[x][c] = true;
  }
  REP(i, M) {
    int d = next(lower_bound(ALL(xs), yoko[i * 3])) - xs.begin();
    int e = next(lower_bound(ALL(ys), yoko[i * 3 + 1])) - ys.begin();
    int f = next(lower_bound(ALL(ys), yoko[i * 3 + 2])) - ys.begin();
    for (int y = e; y <= f; y++)
      wall[d][y] = true;
  }

  queue<pair<int, int>> q;
  {
    int x = next(lower_bound(ALL(xs), 0)) - xs.begin();
    int y = next(lower_bound(ALL(ys), 0)) - ys.begin();
    q.push({x, y});
    vis[x][y] = true;
  }

  const int dx[] = {0, 1, 0, -1};
  const int dy[] = {1, 0, -1, 0};

  bool inf = false;
  ll ans = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();

    {
      if (x == 0 || y == 0) {
        inf = true;
        break;
      }
      ans += (xs[x] - xs[x - 1]) * (ys[y] - ys[y - 1]);
    }

    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 <= nx && nx < X && 0 <= ny && ny < Y && !wall[nx][ny] &&
          !vis[nx][ny]) {
        vis[nx][ny] = true;
        q.push({nx, ny});
      }
    }
  }

  // REP(i, X) {
  //   REP(j, Y) {
  //     cout << (wall[i][j] ? 1 : 0);
  //   }
  //   cout << endl;
  // }

  // REP(i, X) {

  //   printf("%20d : ", xs[i]);
  //   REP(j, Y) {
  //     cout << (vis[i][j] ? 1 : 0);
  //   }
  //   cout << endl;
  // }

  if (inf) {
    cout << "INF" << endl;
  } else {
    cout << ans << endl;
  }
  return 0;
}

int main() {

#ifdef LOCAL
  for (; !cin.eof(); cin >> ws)
#endif
    main2();
  return 0;
}