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


#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

#ifndef ONLINE_JUDGE
#include <iomanip>
#include <iostream>

void dbg_impl() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void dbg_impl(const Head &head, const Tail &...tail) {
  std::cerr << ' ' << head;
  dbg_impl(tail...);
}

struct dbg_setup {
  dbg_setup() {
    std::cerr << std::boolalpha;
    std::cerr << std::fixed << std::setprecision(5);
  }
} debug_setup;

#define debug(...)                                                             \\
  do {                                                                         \\
    std::cerr << std::setw(5) << __LINE__ << "  | ";                           \\
    std::cerr << "[" << #__VA_ARGS__ << "]:";                                  \\
    dbg_impl(__VA_ARGS__);                                                     \\
  } while (false)

#define affirm(condition, ...)                                                 \\
  do {                                                                         \\
    if (!(condition)) {                                                        \\
      std::cerr << "Affirmation failed: (" << #condition << "), ";             \\
      if (std::string(#__VA_ARGS__) != "") {                                   \\
        std::cerr << "message: " << #__VA_ARGS__ << ", ";                      \\
      }                                                                        \\
      std::cerr << "function " << __func__ << ", ";                            \\
      std::cerr << "file " << __FILE__ << ", ";                                \\
      std::cerr << "line " << __LINE__ << ".\\n";                               \\
      std::exit(EXIT_FAILURE);                                                 \\
    }                                                                          \\
  } while (false)

#else
#define debug(...) ((void)0)
#define affirm(...) ((void)0)
#endif

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> cmpx(1, 0);
  std::vector<int> cmpy(1, 0);
  std::vector<int> A(N), B(N), C(N);
  std::vector<int> D(M), E(M), F(M);
  for (int i : range(0, N)) {
    std::cin >> A[i] >> B[i] >> C[i];
    cmpx.push_back(A[i]);
    cmpx.push_back(B[i]);
    cmpy.push_back(C[i]);
  }
  for (int i : range(0, M)) {
    std::cin >> D[i] >> E[i] >> F[i];
    cmpx.push_back(D[i]);
    cmpy.push_back(E[i]);
    cmpy.push_back(F[i]);
  }
  auto cmprs = [&](std::vector<int> &vec) {
    std::sort(vec.begin(), vec.end());
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  };
  auto fix = [&](const std::vector<int> &vec, int &v) {
    v = std::lower_bound(vec.cbegin(), vec.cend(), v) - vec.cbegin();
  };
  cmprs(cmpx);
  cmprs(cmpy);
  for (int i : range(0, N)) {
    fix(cmpx, A[i]);
    fix(cmpx, B[i]);
    fix(cmpy, C[i]);
  }
  for (int i : range(0, M)) {
    fix(cmpx, D[i]);
    fix(cmpy, E[i]);
    fix(cmpy, F[i]);
  }
  int sx = 0, sy = 0;
  fix(cmpx, sx);
  fix(cmpy, sy);
  if (sx + 1 == cmpx.size() || sx == 0) {
    std::cout << "INF\\n";
    return 0;
  }
  if (sy + 1 == cmpy.size() || sy == 0) {
    std::cout << "INF\\n";
    return 0;
  }
  int X = cmpx.size() - 1;
  int Y = cmpy.size() - 1;
  std::vector<std::vector<bool>> reach(X, std::vector<bool>(Y));
  std::vector<std::vector<bool>> side(Y + 1, std::vector<bool>(X)); // y -> x
  std::vector<std::vector<bool>> vert(X + 1, std::vector<bool>(Y)); // x -> y
  for (int i : range(0, N)) {
    for (int j : range(A[i], B[i])) {
      side[C[i]][j] = true;
    }
  }
  for (int i : range(0, M)) {
    for (int j : range(E[i], F[i])) {
      vert[D[i]][j] = true;
    }
  }
  std::queue<std::pair<int, int>> que;
  auto push = [&](int x, int y) {
    if (x < 0 || y < 0 || x >= X || y >= Y) {
      std::cout << "INF\\n";
      std::exit(EXIT_SUCCESS);
    }
    if (!reach[x][y]) {
      reach[x][y] = true;
      que.emplace(x, y);
    }
  };
  push(sx, sy);
  push(sx - 1, sy);
  push(sx, sy - 1);
  push(sx - 1, sy - 1);
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    if (!side[y + 1][x])
      push(x, y + 1);
    if (!side[y][x])
      push(x, y - 1);
    if (!vert[x + 1][y])
      push(x + 1, y);
    if (!vert[x][y])
      push(x - 1, y);
  }
  int64_t ans = 0;
  for (int i : range(0, X)) {
    for (int j : range(0, Y)) {
      if (reach[i][j]) {
        ans +=
            (int64_t)(cmpx[i + 1] - cmpx[i]) * (int64_t)(cmpy[j + 1] - cmpy[j]);
      }
    }
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { ++i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_) : l(l_), r(std::max(l_, r_)) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_) : i(i_) {}
    constexpr void operator++() { --i; }
    constexpr int operator*() const { return i; }
    constexpr bool operator!=(itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_) : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

#ifndef ONLINE_JUDGE
#include <iomanip>
#include <iostream>

void dbg_impl() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void dbg_impl(const Head &head, const Tail &...tail) {
  std::cerr << ' ' << head;
  dbg_impl(tail...);
}

struct dbg_setup {
  dbg_setup() {
    std::cerr << std::boolalpha;
    std::cerr << std::fixed << std::setprecision(5);
  }
} debug_setup;

#define debug(...)                                                             \\
  do {                                                                         \\
    std::cerr << std::setw(5) << __LINE__ << "  | ";                           \\
    std::cerr << "[" << #__VA_ARGS__ << "]:";                                  \\
    dbg_impl(__VA_ARGS__);                                                     \\
  } while (false)

#define affirm(condition, ...)                                                 \\
  do {                                                                         \\
    if (!(condition)) {                                                        \\
      std::cerr << "Affirmation failed: (" << #condition << "), ";             \\
      if (std::string(#__VA_ARGS__) != "") {                                   \\
        std::cerr << "message: " << #__VA_ARGS__ << ", ";                      \\
      }                                                                        \\
      std::cerr << "function " << __func__ << ", ";                            \\
      std::cerr << "file " << __FILE__ << ", ";                                \\
      std::cerr << "line " << __LINE__ << ".\\n";                               \\
      std::exit(EXIT_FAILURE);                                                 \\
    }                                                                          \\
  } while (false)

#else
#define debug(...) ((void)0)
#define affirm(...) ((void)0)
#endif

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> cmpx(1, 0);
  std::vector<int> cmpy(1, 0);
  std::vector<int> A(N), B(N), C(N);
  std::vector<int> D(M), E(M), F(M);
  for (int i : range(0, N)) {
    std::cin >> A[i] >> B[i] >> C[i];
    cmpx.push_back(A[i]);
    cmpx.push_back(B[i]);
    cmpy.push_back(C[i]);
  }
  for (int i : range(0, M)) {
    std::cin >> D[i] >> E[i] >> F[i];
    cmpx.push_back(D[i]);
    cmpy.push_back(E[i]);
    cmpy.push_back(F[i]);
  }
  auto cmprs = [&](std::vector<int> &vec) {
    std::sort(vec.begin(), vec.end());
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  };
  auto fix = [&](const std::vector<int> &vec, int &v) {
    v = std::lower_bound(vec.cbegin(), vec.cend(), v) - vec.cbegin();
  };
  cmprs(cmpx);
  cmprs(cmpy);
  for (int i : range(0, N)) {
    fix(cmpx, A[i]);
    fix(cmpx, B[i]);
    fix(cmpy, C[i]);
  }
  for (int i : range(0, M)) {
    fix(cmpx, D[i]);
    fix(cmpy, E[i]);
    fix(cmpy, F[i]);
  }
  int sx = 0, sy = 0;
  fix(cmpx, sx);
  fix(cmpy, sy);
  if (sx + 1 == cmpx.size() || sx == 0) {
    std::cout << "INF\\n";
    return 1;
  }
  if (sy + 1 == cmpy.size() || sy == 0) {
    std::cout << "INF\\n";
    return 1;
  }
  int X = cmpx.size() - 1;
  int Y = cmpy.size() - 1;
  std::vector<std::vector<bool>> reach(X, std::vector<bool>(Y));
  std::vector<std::vector<bool>> side(Y + 1, std::vector<bool>(X)); // y -> x
  std::vector<std::vector<bool>> vert(X + 1, std::vector<bool>(Y)); // x -> y
  for (int i : range(0, N)) {
    for (int j : range(A[i], B[i])) {
      side[C[i]][j] = true;
    }
  }
  for (int i : range(0, M)) {
    for (int j : range(E[i], F[i])) {
      vert[D[i]][j] = true;
    }
  }
  std::queue<std::pair<int, int>> que;
  auto push = [&](int x, int y) {
    if (x < 0 || y < 0 || x >= X || y >= Y) {
      std::cout << "INF\\n";
      std::exit(EXIT_SUCCESS);
    }
    if (!reach[x][y]) {
      reach[x][y] = true;
      que.emplace(x, y);
    }
  };
  push(sx, sy);
  push(sx - 1, sy);
  push(sx, sy - 1);
  push(sx - 1, sy - 1);
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    if (!side[y + 1][x])
      push(x, y + 1);
    if (!side[y][x])
      push(x, y - 1);
    if (!vert[x + 1][y])
      push(x + 1, y);
    if (!vert[x][y])
      push(x - 1, y);
  }
  int64_t ans = 0;
  for (int i : range(0, X)) {
    for (int j : range(0, Y)) {
      if (reach[i][j]) {
        ans +=
            (int64_t)(cmpx[i + 1] - cmpx[i]) * (int64_t)(cmpy[j + 1] - cmpy[i]);
      }
    }
  }
  std::cout << ans << '\\n';
  return 0;
}