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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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 <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> struct Grid {
public:
  int height, width;

  vector<vector<T>> data;
  int dh[8] = {1, 0, -1, 0, 1, 1, -1, -1};
  int dw[8] = {0, 1, 0, -1, 1, -1, 1, -1};
  int inf = numeric_limits<int>::max();

  Grid(int h, int w) : height(h), width(w), data(h, vector<T>(w)) {}
  Grid(int h, int w, T e) : height(h), width(w), data(h, vector<T>(w, e)) {}
  T &operator()(int h, int w) {
    if (isOutside(h, w)) {
      fprintf(stderr, "(h : %d, w : %d) : index out of bounds!\\n", h, w);
      exit(1);
    }
    return data[h][w];
  }

  bool isOutside(int h, int w) {
    return (h < 0 || height <= h || w < 0 || width <= w);
  }

  Grid<int> bfs(pair<int, int> start) {
    Grid<int> res(height, width, 0);
    vector<vector<int>> used(height, vector<int>(width, 0));
    queue<pi> q; // 隣接マス間の距離が１のみの場合（それ以外はpqを使う）
    // priority_queue<P, vector<P>, greater<P>> q;
    q.push(start);
    while (!q.empty()) {
      auto now = q.front();
      q.pop();
      if (isOutside(now.first, now.second)) {
        cout << "INF" << endl;
        exit(0);
      }
      if (used[now.first][now.second])
        continue;
      used[now.first][now.second] = 1;
      res(now.first, now.second) = 1;
      for (int i = 0; i < 4; i++) {
        pair<int, int> nxt = {now.first + dh[i], now.second + dw[i]};
        if (!(*this)(now.first, now.second)[i])
          continue;
        q.push(nxt);
      }
    }
    return res;
  }

  void debug(string sep = "", int n = 1) {
#ifdef LOCAL
    // cout << setw(w);
    for (int h = 0; h < height; h++) {
      for (int w = 0; w < width; w++) {
        cout << setw(n) << data[h][w] << sep;
      }
      cout << endl;
    }
#endif
  }

  void print(string sep = "") {
    for (int h = 0; h < height; h++) {
      for (int w = 0; w < width; w++) {
        cout << data[h][w] << sep;
      }
      cout << endl;
    }
  }
};

signed main() {
  INIT;
  cin >> N >> M;
  vi vx, vy;
  vx.push_back(0);
  vy.push_back(0);
  vector<pair<pi, int>> memY(N), memX(M);
  map<int, int> mpx, mpy;
  REP(i, N) {
    cin >> A >> B >> Y;
    vy.push_back(Y);
    vx.push_back(A);
    vx.push_back(B);
    memY[i] = {{A, B}, Y};
  }
  REP(i, M) {
    cin >> X >> A >> B;
    vx.push_back(X);
    vy.push_back(A);
    vy.push_back(B);
    memX[i] = {{A, B}, X};
  }
  sort(ALL(vx));
  sort(ALL(vy));
  UNIQUE(vx);
  REP(i, vx.size())
  mpx[vx[i]] = i;
  UNIQUE(vy);
  REP(i, vy.size())
  mpy[vy[i]] = i;
  Grid<vector<int>> g(vx.size() - 1, vy.size() - 1, vector<int>(4, 1));
  REP(i, N) {
    auto [A, B] = memY[i].first;
    Y = memY[i].second;
    int l = mpx[A];
    int r = mpx[B];
    int y = mpy[Y];
    FOR(j, l, r) {
      if (y < vy.size() - 1)
        g(j, y)[3] = 0;
      if (y > 0)
        g(j, y - 1)[1] = 0;
    }
  }
  REP(i, M) {
    auto [A, B] = memX[i].first;
    X = memX[i].second;
    int l = mpy[A];
    int r = mpy[B];
    int x = mpx[X];
    FOR(j, l, r) {
      if (x < vx.size() - 1)
        g(x, j)[2] = 0;
      if (x > 0)
        g(x - 1, j)[0] = 0;
    }
  }
  auto res = g.bfs({mpx[0], mpy[0]});
  res.debug();
  REP(h, res.height) {
    REP(w, res.width) {
      if (res(h, w)) {
        ans += (vx[h + 1] - vx[h]) * (vy[w + 1] - vy[w]);
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\\n'
// #define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                                                         \\
  do {                                                                         \\
    ostringstream os;                                                          \\
    os << setw(n) << setfill('0') << s;                                        \\
    s = os.str();                                                              \\
  } while (0);
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
#define INIT                                                                   \\
  std::cin.tie(nullptr);                                                       \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cout << std::fixed << std::setprecision(10);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T> using Pq = priority_queue<T>;
template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T> vector<T> make_vec(size_t a, T n) {
  return vector<T>(a, n);
}
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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 <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> struct Grid {
public:
  int height, width;

  vector<vector<T>> data;
  int dh[8] = {1, 0, -1, 0, 1, 1, -1, -1};
  int dw[8] = {0, 1, 0, -1, 1, -1, 1, -1};
  int inf = numeric_limits<int>::max();

  Grid(int h, int w) : height(h), width(w), data(h, vector<T>(w)) {}
  Grid(int h, int w, T e) : height(h), width(w), data(h, vector<T>(w, e)) {}
  T &operator()(int h, int w) {
    if (isOutside(h, w)) {
      fprintf(stderr, "(h : %d, w : %d) : index out of bounds!\\n", h, w);
      exit(1);
    }
    return data[h][w];
  }

  bool isOutside(int h, int w) {
    return (h < 0 || height <= h || w < 0 || width <= w);
  }

  Grid<int> bfs(pair<int, int> start) {
    using P = pair<int, pair<int, int>>;
    Grid<int> res(height, width, 0);
    vector<vector<int>> used(height, vector<int>(width, 0));
    queue<pi> q; // 隣接マス間の距離が１のみの場合（それ以外はpqを使う）
    // priority_queue<P, vector<P>, greater<P>> q;
    res(start.first, start.second) = 0;
    q.push(start);
    while (!q.empty()) {
      auto now = q.front();
      q.pop();
      if (isOutside(now.first, now.second)) {
        cout << "INF" << endl;
        exit(0);
      }
      if (used[now.first][now.second])
        continue;
      used[now.first][now.second] = 1;
      res(now.first, now.second) = 1;
      for (int i = 0; i < 4; i++) {
        pair<int, int> nxt = {now.first + dh[i], now.second + dw[i]};
        if (!(*this)(now.first, now.second)[i])
          continue;
        q.push(nxt);
      }
    }
    return res;
  }

  void debug(string sep = "", int n = 1) {
#ifdef LOCAL
    // cout << setw(w);
    for (int h = 0; h < height; h++) {
      for (int w = 0; w < width; w++) {
        cout << setw(n) << data[h][w] << sep;
      }
      cout << endl;
    }
#endif
  }

  void print(string sep = "") {
    for (int h = 0; h < height; h++) {
      for (int w = 0; w < width; w++) {
        cout << data[h][w] << sep;
      }
      cout << endl;
    }
  }
};

signed main() {
  INIT;
  cin >> N >> M;
  vi vx, vy;
  vx.push_back(0);
  vy.push_back(0);
  vector<pair<pi, int>> memY(N), memX(M);
  map<int, int> mpx, mpy;
  REP(i, N) {
    cin >> A >> B >> Y;
    vy.push_back(Y);
    vx.push_back(A);
    vx.push_back(B);
    memY[i] = {{A, B}, Y};
  }
  REP(i, M) {
    cin >> X >> A >> B;
    vx.push_back(X);
    vy.push_back(A);
    vy.push_back(B);
    memX[i] = {{A, B}, X};
  }
  sort(ALL(vx));
  sort(ALL(vy));
  UNIQUE(vx);
  REP(i, vx.size())
  mpx[vx[i]] = i;
  UNIQUE(vy);
  REP(i, vy.size())
  mpy[vy[i]] = i;
  Grid<vector<int>> g(vx.size() - 1, vy.size() - 1, vector<int>(4, 1));
  REP(i, N) {
    auto [A, B] = memY[i].first;
    Y = memY[i].second;
    int l = mpx[A];
    int r = mpx[B];
    int y = mpy[Y];
    FOR(j, l, r) {
      if (y < vy.size() - 1)
        g(j, y)[3] = 0;
      if (y > 0)
        g(j, y - 1)[1] = 0;
    }
  }
  REP(i, M) {
    auto [A, B] = memX[i].first;
    X = memX[i].second;
    int l = mpy[A];
    int r = mpy[B];
    int x = mpx[X];
    FOR(j, l, r) {
      if (x < vx.size() - 1)
        g(x, j)[2] = 0;
      if (x > 0)
        g(x - 1, j)[0] = 0;
    }
  }
  auto res = g.bfs({mpx[0], mpy[0]});
  res.debug();
  REP(h, res.height) {
    REP(w, res.width) {
      if (res(h, w)) {
        ans += (vx[h + 1] - vx[h]) * (vy[w + 1] - vy[w]);
      }
    }
  }
  cout << ans << endl;
}