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

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

// Does coordinate compression.
// Arguments:
// - raw_xs: Values to compress.
// Returns:
// - to_index: A mapping from a raw value to a compressed value.
// - to_raw: A mapping from a compressed value to a raw value.
// Verified: CF573Div1D, ARC076D
tuple<unordered_map<int64, int>, vector<int64>>
Compress(const vector<int64> &raw_xs) {
  unordered_set<int64> raw_x_set(raw_xs.begin(), raw_xs.end());
  vector<int64> to_raw(raw_x_set.begin(), raw_x_set.end());
  sort(to_raw.begin(), to_raw.end());
  unordered_map<int64, int> to_index;
  for (int i = 0; i < to_raw.size(); i++) {
    to_index[to_raw[i]] = i;
  }
  return make_tuple(to_index, to_raw);
}

template <typename T>
vector<vector<T>> MakeVector2D(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<int64> xs, ys;

  vector<tuple<int64, int64, int64>> abcs;
  for (int i = 0; i < n; i++) {
    int64 a, b, c;
    cin >> a >> b >> c;
    abcs.push_back({a, b, c});
    xs.push_back(a);
    xs.push_back(b);
    ys.push_back(c);
  }
  vector<tuple<int64, int64, int64>> defs;
  for (int i = 0; i < m; i++) {
    int64 d, e, f;
    cin >> d >> e >> f;
    defs.push_back({d, e, f});
    xs.push_back(d);
    ys.push_back(e);
    ys.push_back(f);
  }

  xs.push_back(0);
  ys.push_back(0);

  xs.push_back(-2E9);
  xs.push_back(2E9);
  ys.push_back(-2E9);
  ys.push_back(2E9);
  auto [x_to_index, x_to_raw] = Compress(xs);
  auto [y_to_index, y_to_raw] = Compress(ys);

  if (DEBUG)
    cout << "DEBUG 1" << endl;

  int x_size = x_to_raw.size();
  int y_size = y_to_raw.size();
  auto reached = MakeVector2D(y_size - 1, x_size - 1, 0);
  auto hbar = MakeVector2D(y_size, x_size - 1, 0);
  auto vbar = MakeVector2D(y_size - 1, x_size, 0);
  for (auto [raw_a, raw_b, raw_c] : abcs) {
    int a = x_to_index[raw_a];
    int b = x_to_index[raw_b];
    int c = y_to_index[raw_c];
    for (int x = a; x < b; x++) {
      hbar[c][x] = 1;
    }
  }
  for (auto [raw_d, raw_e, raw_f] : defs) {
    int d = x_to_index[raw_d];
    int e = y_to_index[raw_e];
    int f = y_to_index[raw_f];
    for (int y = e; y < f; y++) {
      vbar[y][d] = 1;
    }
  }

  if (DEBUG)
    cout << "DEBUG 2" << endl;

  // function<void(int, int)> dfs_fn = [&](int x, int y) {
  //   if (reached[y][x]) return;
  //   reached[y][x] = 1;
  //   if (y > 0 && hbar[y][x] == 0) {
  //     dfs_fn(x, y - 1);
  //   }
  //   if (y + 1 < y_size - 1 && hbar[y + 1][x] == 0) {
  //     dfs_fn(x, y + 1);
  //   }
  //   if (x > 0 && vbar[y][x] == 0) {
  //     dfs_fn(x - 1, y);
  //   }
  //   if (x + 1 < x_size - 1 && vbar[y][x + 1] == 0) {
  //     dfs_fn(x + 1, y);
  //   }
  // };
  // dfs_fn(x_to_index[0], y_to_index[0]);
  stack<tuple<int, int>> stack;
  stack.push({x_to_index[0], y_to_index[0]});
  while (!stack.empty()) {
    auto [x, y] = stack.top();
    stack.pop();
    reached[y][x] = 1;
    if (y > 0 && hbar[y][x] == 0) {
      if (!reached[y - 1][x]) {
        stack.push({x, y - 1});
      }
    }
    if (y + 1 < y_size - 1 && hbar[y + 1][x] == 0) {
      if (!reached[y + 1][x]) {
        stack.push({x, y + 1});
      }
    }
    if (x > 0 && vbar[y][x] == 0) {
      if (!reached[y][x - 1]) {
        stack.push({x - 1, y});
      }
    }
    if (x + 1 < x_size - 1 && vbar[y][x + 1] == 0) {
      if (!reached[y][x + 1]) {
        stack.push({x + 1, y});
      }
    }
  }

  for (int y = 0; y < y_size - 1; y++) {
    for (int x = 0; x < x_size - 1; x++) {
      if (reached[y][x]) {
        if (x == 0 || x == x_size - 1 || y == 0 || y == y_size - 1) {
          cout << "INF" << endl;
          return 0;
        }
      }
    }
  }

  int64 ans = 0;
  for (int y = 0; y < y_size - 1; y++) {
    for (int x = 0; x < x_size - 1; x++) {
      if (reached[y][x]) {
        ans +=
            (x_to_raw[x + 1] - x_to_raw[x]) * (y_to_raw[y + 1] - y_to_raw[y]);
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

// Does coordinate compression.
// Arguments:
// - raw_xs: Values to compress.
// Returns:
// - to_index: A mapping from a raw value to a compressed value.
// - to_raw: A mapping from a compressed value to a raw value.
// Verified: CF573Div1D, ARC076D
tuple<unordered_map<int64, int>, vector<int64>>
Compress(const vector<int64> &raw_xs) {
  unordered_set<int64> raw_x_set(raw_xs.begin(), raw_xs.end());
  vector<int64> to_raw(raw_x_set.begin(), raw_x_set.end());
  sort(to_raw.begin(), to_raw.end());
  unordered_map<int64, int> to_index;
  for (int i = 0; i < to_raw.size(); i++) {
    to_index[to_raw[i]] = i;
  }
  return make_tuple(to_index, to_raw);
}

template <typename T>
vector<vector<T>> MakeVector2D(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<int64> xs, ys;

  vector<tuple<int64, int64, int64>> abcs;
  for (int i = 0; i < n; i++) {
    int64 a, b, c;
    cin >> a >> b >> c;
    abcs.push_back({a, b, c});
    xs.push_back(a);
    xs.push_back(b);
    ys.push_back(c);
  }
  vector<tuple<int64, int64, int64>> defs;
  for (int i = 0; i < m; i++) {
    int64 d, e, f;
    cin >> d >> e >> f;
    defs.push_back({d, e, f});
    xs.push_back(d);
    ys.push_back(e);
    ys.push_back(f);
  }

  xs.push_back(0);
  ys.push_back(0);

  xs.push_back(-2E9);
  xs.push_back(2E9);
  ys.push_back(-2E9);
  ys.push_back(2E9);
  auto [x_to_index, x_to_raw] = Compress(xs);
  auto [y_to_index, y_to_raw] = Compress(ys);

  if (DEBUG)
    cout << "DEBUG 1" << endl;

  int x_size = x_to_raw.size();
  int y_size = y_to_raw.size();
  auto reached = MakeVector2D(y_size - 1, x_size - 1, 0);
  auto hbar = MakeVector2D(y_size, x_size - 1, 0);
  auto vbar = MakeVector2D(y_size - 1, x_size, 0);
  for (auto [raw_a, raw_b, raw_c] : abcs) {
    int a = x_to_index[raw_a];
    int b = x_to_index[raw_b];
    int c = y_to_index[raw_c];
    for (int x = a; x < b; x++) {
      hbar[c][x] = 1;
    }
  }
  for (auto [raw_d, raw_e, raw_f] : defs) {
    int d = x_to_index[raw_d];
    int e = y_to_index[raw_e];
    int f = y_to_index[raw_f];
    for (int y = e; y < f; y++) {
      vbar[y][d] = 1;
    }
  }

  if (DEBUG)
    cout << "DEBUG 2" << endl;

  function<void(int, int)> dfs_fn = [&](int x, int y) {
    if (reached[y][x])
      return;
    reached[y][x] = 1;
    if (y > 0 && hbar[y][x] == 0) {
      dfs_fn(x, y - 1);
    }
    if (y + 1 < y_size - 1 && hbar[y + 1][x] == 0) {
      dfs_fn(x, y + 1);
    }
    if (x > 0 && vbar[y][x] == 0) {
      dfs_fn(x - 1, y);
    }
    if (x + 1 < x_size - 1 && vbar[y][x + 1] == 0) {
      dfs_fn(x + 1, y);
    }
  };
  dfs_fn(x_to_index[0], y_to_index[0]);

  for (int y = 0; y < y_size - 1; y++) {
    for (int x = 0; x < x_size - 1; x++) {
      if (reached[y][x]) {
        if (x == 0 || x == x_size - 1 || y == 0 || y == y_size - 1) {
          cout << "INF" << endl;
          return 0;
        }
      }
    }
  }

  int64 ans = 0;
  for (int y = 0; y < y_size - 1; y++) {
    for (int x = 0; x < x_size - 1; x++) {
      if (reached[y][x]) {
        ans +=
            (x_to_raw[x + 1] - x_to_raw[x]) * (y_to_raw[y + 1] - y_to_raw[y]);
      }
    }
  }
  cout << ans << endl;
}