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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
std::vector<ll> xs, ys;
ll A[1010], B[1010], C[1010];
ll D[1010], E[1010], F[1010];

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

bool used[5020][5020][4];

std::map<ll, ll> ixs, iys;
ll dist[5020][5020];

int main() {
  std::cin >> N >> M;

  rep(i, N) {
    std::cin >> A[i] >> B[i] >> C[i];

    xs.emplace_back(A[i]);
    xs.emplace_back(B[i]);
    ys.emplace_back(C[i]);
  }

  rep(i, M) {
    std::cin >> D[i] >> E[i] >> F[i];

    xs.emplace_back(D[i]);
    ys.emplace_back(E[i]);
    ys.emplace_back(F[i]);
  }

  xs.emplace_back(0);
  xs.emplace_back(2e9);
  xs.emplace_back(-2e9);

  ys.emplace_back(0);
  ys.emplace_back(2e9);
  ys.emplace_back(-2e9);

  std::sort(all(xs));
  std::sort(all(ys));
  xs.erase(std::unique(all(xs)), xs.end());
  ys.erase(std::unique(all(ys)), ys.end());

  rep(i, xs.size()) ixs[xs[i]] = i;

  rep(i, ys.size()) iys[ys[i]] = i;

  rep(i, N) {
    repi(j, ixs[A[i]], ixs[B[i]]) {
      used[j][iys[C[i]]][3] = true;
      used[j][iys[C[i]] - 1][1] = true;
    }
  }

  rep(i, M) {
    repi(j, iys[E[i]], iys[F[i]]) {
      used[ixs[D[i]]][j][2] = true;
      used[ixs[D[i]] - 1][j][0] = true;
    }
  }

  std::queue<P> que;
  que.emplace(ixs[0], iys[0]);

  rep(i, xs.size() + 5) rep(j, ys.size() + 5) dist[i][j] = INF;

  dist[ixs[0]][iys[0]] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    ll i = p.first, j = p.second;

    // std::cout << p << std::endl;

    rep(d, 4) if (!used[i][j][d]) {
      ll ni = i + dx[d], nj = j + dy[d];

      if (ni >= 0 && ni < xs.size() && nj >= 0 && nj < ys.size() &&
          dist[ni][nj] == INF) {
        dist[ni][nj] = dist[i][j] + 1;
        que.emplace(ni, nj);
      }
    }
  }

  ll w = xs.size() - 1;
  ll h = ys.size() - 1;

  ll ans = 0;

  rep(i, w) rep(j, h) if (dist[i][j] != INF) {
    if (!i || i == w - 1 || !j || j == h - 1) {
      std::cout << "INF" << std::endl;

      return 0;
    }

    ans += (xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
  }

  std::cout << ans << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N, M;
std::vector<ll> xs, ys;
ll A[1010], B[1010], C[1010];
ll D[1010], E[1010], F[1010];

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

bool used[2020][2020][4];

std::map<ll, ll> ixs, iys;
ll dist[2020][2020];

int main() {
  std::cin >> N >> M;

  rep(i, N) {
    std::cin >> A[i] >> B[i] >> C[i];

    xs.emplace_back(A[i]);
    xs.emplace_back(B[i]);
    ys.emplace_back(C[i]);
  }

  rep(i, M) {
    std::cin >> D[i] >> E[i] >> F[i];

    xs.emplace_back(D[i]);
    ys.emplace_back(E[i]);
    ys.emplace_back(F[i]);
  }

  xs.emplace_back(0);
  xs.emplace_back(2e9);
  xs.emplace_back(-2e9);

  ys.emplace_back(0);
  ys.emplace_back(2e9);
  ys.emplace_back(-2e9);

  std::sort(all(xs));
  std::sort(all(ys));
  xs.erase(std::unique(all(xs)), xs.end());
  ys.erase(std::unique(all(ys)), ys.end());

  rep(i, xs.size()) ixs[xs[i]] = i;

  rep(i, ys.size()) iys[ys[i]] = i;

  rep(i, N) {
    repi(j, ixs[A[i]], ixs[B[i]]) {
      used[j][iys[C[i]]][3] = true;
      used[j][iys[C[i]] - 1][1] = true;
    }
  }

  rep(i, M) {
    repi(j, iys[E[i]], iys[F[i]]) {
      used[ixs[D[i]]][j][2] = true;
      used[ixs[D[i]] - 1][j][0] = true;
    }
  }

  std::queue<P> que;
  que.emplace(ixs[0], iys[0]);

  rep(i, xs.size() + 5) rep(j, ys.size() + 5) dist[i][j] = INF;

  dist[ixs[0]][iys[0]] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    ll i = p.first, j = p.second;

    // std::cout << p << std::endl;

    rep(d, 4) if (!used[i][j][d]) {
      ll ni = i + dx[d], nj = j + dy[d];

      if (ni >= 0 && ni < xs.size() && nj >= 0 && nj < ys.size() &&
          dist[ni][nj] == INF) {
        dist[ni][nj] = dist[i][j] + 1;
        que.emplace(ni, nj);
      }
    }
  }

  ll w = xs.size() - 1;
  ll h = ys.size() - 1;

  ll ans = 0;

  rep(i, w) rep(j, h) if (dist[i + 1][j + 1] != INF) {
    if (!i || i == w - 1 || !j || j == h - 1) {
      std::cout << "INF" << std::endl;

      return 0;
    }

    ans += (xs[i + 1] - xs[i]) * (ys[i + 1] - ys[i]);
  }

  std::cout << ans << std::endl;

  return 0;
}