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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;

  set<int> xs, ys;
  vector<tuple<int, int, int>> hor, ver;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    xs.insert(a);
    xs.insert(b);
    ys.insert(c);
    hor.emplace_back(a, b, c);
  }
  for (int i = 0; i < M; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    xs.insert(d);
    ys.insert(e);
    ys.insert(f);
    ver.emplace_back(d, e, f);
  }
  xs.insert(0);
  ys.insert(0);

  vector<int> xi_to_x(xs.begin(), xs.end()), yi_to_y(ys.begin(), ys.end());
  int H = ys.size() + 1, W = xs.size() + 1;
  map<int, int> x_to_xi, y_to_yi;
  for (int i = 0; i < W - 1; i++) {
    x_to_xi[xi_to_x[i]] = i;
  }
  for (int i = 0; i < H - 1; i++) {
    y_to_yi[yi_to_y[i]] = i;
  }

  const int dx[] = {-1, 0, 1, 0};
  const int dy[] = {0, -1, 0, 1};
  vector<vector<set<int>>> reject(H, vector<set<int>>(W));
  for (int i = 0; i < N; i++) {
    int a, b, c;
    tie(a, b, c) = hor[i];
    int ai = x_to_xi[a], bi = x_to_xi[b], ci = y_to_yi[c];
    for (int xi = ai + 1; xi <= bi; xi++) {
      {
        int yi = ci; // reject down
        reject[yi][xi].insert(3);
      }
      {
        int yi = ci + 1; // reject up
        reject[yi][xi].insert(1);
      }
    }
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    tie(a, b, c) = ver[i];
    int ai = x_to_xi[a], bi = y_to_yi[b], ci = y_to_yi[c];
    for (int yi = bi + 1; yi <= ci; yi++) {
      {
        int xi = ai; // reject right
        reject[yi][xi].insert(2);
      }
      {
        int xi = ai + 1; // reject left
        reject[yi][xi].insert(0);
      }
    }
  }

  vector<vector<bool>> visited(H, vector<bool>(W, false));
  queue<pair<int, int>> que;
  int xi0 = x_to_xi[0], yi0 = y_to_yi[0];
  que.emplace(yi0, xi0);
  que.emplace(yi0, xi0 + 1);
  que.emplace(yi0 + 1, xi0);
  que.emplace(yi0 + 1, xi0 + 1);
  visited[yi0][xi0] = true;
  visited[yi0][xi0 + 1] = true;
  visited[yi0 + 1][xi0] = true;
  visited[yi0 + 1][xi0 + 1] = true;

  while (!que.empty()) {
    int yi, xi;
    tie(yi, xi) = que.front();
    que.pop();
    for (int d = 0; d < 4; d++) {
      if (reject[yi][xi].count(d) > 0)
        continue;
      int nyi = yi + dy[d], nxi = xi + dx[d];
      if (nyi < 0 || nyi >= H || nxi < 0 || nxi >= W)
        continue;
      if (visited[nyi][nxi])
        continue;
      visited[nyi][nxi] = true;
      que.emplace(nyi, nxi);
    }
  }

  for (int i = 0; i < H; i++) {
    if (visited[i][0] || visited[i][W - 1]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  for (int i = 0; i < W; i++) {
    if (visited[0][i] || visited[H - 1][i]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  ll ans = 0;
  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (!visited[i][j])
        continue;
      ans += ll(xi_to_x[j] - xi_to_x[j - 1]) * ll(yi_to_y[i] - yi_to_y[i - 1]);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N, M;
  cin >> N >> M;

  set<int> xs, ys;
  vector<tuple<int, int, int>> hor, ver;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    xs.insert(a);
    xs.insert(b);
    ys.insert(c);
    hor.emplace_back(a, b, c);
  }
  for (int i = 0; i < M; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    xs.insert(d);
    ys.insert(e);
    ys.insert(f);
    ver.emplace_back(d, e, f);
  }
  xs.insert(0);
  ys.insert(0);

  vector<int> xi_to_x(xs.begin(), xs.end()), yi_to_y(ys.begin(), ys.end());
  int H = ys.size() + 1, W = xs.size() + 1;
  map<int, int> x_to_xi, y_to_yi;
  for (int i = 0; i < W; i++) {
    x_to_xi[xi_to_x[i]] = i;
  }
  for (int i = 0; i < H; i++) {
    y_to_yi[yi_to_y[i]] = i;
  }

  const int dx[] = {-1, 0, 1, 0};
  const int dy[] = {0, -1, 0, 1};
  vector<vector<set<int>>> reject(H, vector<set<int>>(W));
  for (int i = 0; i < N; i++) {
    int a, b, c;
    tie(a, b, c) = hor[i];
    int ai = x_to_xi[a], bi = x_to_xi[b], ci = y_to_yi[c];
    for (int xi = ai + 1; xi <= bi; xi++) {
      {
        int yi = ci; // reject down
        reject[yi][xi].insert(3);
      }
      {
        int yi = ci + 1; // reject up
        reject[yi][xi].insert(1);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    int a, b, c;
    tie(a, b, c) = ver[i];
    int ai = x_to_xi[a], bi = y_to_yi[b], ci = y_to_yi[c];
    for (int yi = bi + 1; yi <= ci; yi++) {
      {
        int xi = ai; // reject right
        reject[yi][xi].insert(2);
      }
      {
        int xi = ai + 1; // reject left
        reject[yi][xi].insert(0);
      }
    }
  }

  vector<vector<bool>> visited(H, vector<bool>(W, false));
  queue<pair<int, int>> que;
  int xi0 = x_to_xi[0], yi0 = y_to_yi[0];
  que.emplace(yi0, xi0);
  que.emplace(yi0, xi0 + 1);
  que.emplace(yi0 + 1, xi0);
  que.emplace(yi0 + 1, xi0 + 1);
  visited[yi0][xi0] = true;
  visited[yi0][xi0 + 1] = true;
  visited[yi0 + 1][xi0] = true;
  visited[yi0 + 1][xi0 + 1] = true;

  while (!que.empty()) {
    int yi, xi;
    tie(yi, xi) = que.front();
    que.pop();
    for (int d = 0; d < 4; d++) {
      if (reject[yi][xi].count(d) > 0)
        continue;
      int nyi = yi + dy[d], nxi = xi + dx[d];
      if (nyi < 0 || nyi >= H || nxi < 0 || nxi >= W)
        continue;
      if (visited[nyi][nxi])
        continue;
      visited[nyi][nxi] = true;
      que.emplace(nyi, nxi);
    }
  }

  for (int i = 0; i < H; i++) {
    if (visited[i][0] || visited[i][W - 1]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  for (int i = 0; i < W; i++) {
    if (visited[0][i] || visited[H - 1][i]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  ll ans = 0;
  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (!visited[i][j])
        continue;
      ans += ll(xi_to_x[j] - xi_to_x[j - 1]) * ll(yi_to_y[i] - yi_to_y[i - 1]);
    }
  }
  cout << ans << endl;

  return 0;
}