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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // int ti = clock();
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  vector<int> D(M), E(M), F(M);
  for (int i = 0; i < M; i++) {
    cin >> D[i] >> E[i] >> F[i];
  }
  // int N=1000, M=1000;
  // vector<int> A(N, -1000000000);
  // vector<int> B(N, 1000000000);
  // vector<int> C(N, 999999999);
  // for(int i=0; i<N; i++)
  // {
  //     A[i] += i*10;
  //     B[i] -= i*10;
  //     C[i] -= i*10;
  //     if(i%2) C[i] *= -1;
  // }
  // vector<int> D(N, -999999999);
  // vector<int> E(N, -1000000000);
  // vector<int> F(N, 1000000000);
  // for(int i=0; i<N; i++)
  // {
  //     D[i] += i*10;
  //     E[i] += i*10;
  //     F[i] -= i*10;
  //     if(i%2) D[i] *= -1;
  // }
  // cout << "input: ";
  // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC
  // << endl;

  vector<ll> X, Y;
  X.push_back(0);
  Y.push_back(0);
  for (int i = 0; i < N; i++) {
    X.push_back(A[i]);
    X.push_back(B[i]);
    Y.push_back(C[i]);
  }
  for (int i = 0; i < M; i++) {
    X.push_back(D[i]);
    Y.push_back(E[i]);
    Y.push_back(F[i]);
  }

  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  // cout << "prepare X Y: ";
  // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC
  // << endl;

  // インデックスが偶数の行・列に壁(-1)を入れる。奇数マスは面積を入れる。
  vector<vector<ll>> mp(2 * X.size() - 1, vector<ll>(2 * Y.size() - 1, 0));
  // 面積埋め
  for (int i = 1; i < 2 * X.size() - 1; i += 2) {
    ll height = X[i / 2 + 1] - X[i / 2];
    for (int j = 1; j < 2 * Y.size() - 1; j += 2) {
      ll width = Y[j / 2 + 1] - Y[j / 2];
      mp[i][j] = height * width;
    }
  }
  // 壁埋め
  for (int i = 0; i < N; i++) {
    int aidx = lower_bound(X.begin(), X.end(), A[i]) - X.begin();
    int bidx = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
    int cidx = lower_bound(Y.begin(), Y.end(), C[i]) - Y.begin();
    for (int j = 2 * aidx; j <= 2 * bidx; j++) {
      mp[j][2 * cidx] = -1;
    }
  }
  for (int i = 0; i < M; i++) {
    int didx = lower_bound(X.begin(), X.end(), D[i]) - X.begin();
    int eidx = lower_bound(Y.begin(), Y.end(), E[i]) - Y.begin();
    int fidx = lower_bound(Y.begin(), Y.end(), F[i]) - Y.begin();
    for (int j = 2 * eidx; j <= 2 * fidx; j++) {
      mp[2 * didx][j] = -1;
    }
  }
  // cout << "prepare mp: ";
  // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC
  // << endl; for(auto vec : mp)
  // {
  //     for(auto num : vec)
  //     {
  //         if(num == -1) cout << "  * ";
  //         else cout << setw(3) << num << ' ';
  //     }
  //     cout << '\\n';
  // }

  // BFS
  int sx = 2 * (lower_bound(X.begin(), X.end(), 0) - X.begin());
  int sy = 2 * (lower_bound(Y.begin(), Y.end(), 0) - Y.begin());
  queue<pair<int, int>> q;
  q.emplace(sx, sy);
  ll ans = 0;
  vector<vector<bool>> is_reached(2 * X.size() - 1,
                                  vector<bool>(2 * Y.size() - 1, false));
  is_reached[sx][sy] = true;
  vector<pair<int, int>> motion;
  motion.emplace_back(1, 0);
  motion.emplace_back(-1, 0);
  motion.emplace_back(0, 1);
  motion.emplace_back(0, -1);
  bool reached_edge = false;
  // int sttime = clock();
  // cout << "start bfs: ";
  // cout << fixed << setprecision(4) << 1.0 * (clock() - ti) / CLOCKS_PER_SEC
  // << endl;
  while (!q.empty() && !reached_edge) {
    pair<int, int> cp = q.front();
    q.pop();
    for (auto m : motion) {
      int nx = cp.first + m.first;
      int ny = cp.second + m.second;
      if (nx < 0 || 2 * X.size() - 1 <= nx || ny < 0 ||
          2 * Y.size() - 1 <= ny) {
        reached_edge = true;
        break;
      }
      if (mp[nx][ny] == -1 || is_reached[nx][ny])
        continue;
      is_reached[nx][ny] = true;
      ans += mp[nx][ny];
      q.emplace(nx, ny);
    }
  }
  // cout << "end bfs: ";
  // cout << fixed << setprecision(4) << 1.0 * (clock() - sttime) /
  // CLOCKS_PER_SEC << endl;
  if (reached_edge)
    cout << "INF\\n";
  else
    cout << ans << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  vector<int> D(M), E(M), F(M);
  for (int i = 0; i < M; i++) {
    cin >> D[i] >> E[i] >> F[i];
  }

  vector<int> X, Y;
  X.push_back(0);
  Y.push_back(0);
  for (int i = 0; i < N; i++) {
    X.push_back(A[i]);
    X.push_back(B[i]);
    Y.push_back(C[i]);
  }
  for (int i = 0; i < M; i++) {
    X.push_back(D[i]);
    Y.push_back(E[i]);
    Y.push_back(F[i]);
  }

  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());

  // インデックスが偶数の行・列に壁(-1)を入れる。奇数マスは面積を入れる。
  vector<vector<int>> mp(2 * X.size() - 1, vector<int>(2 * Y.size() - 1, 0));
  // 面積埋め
  for (int i = 1; i < 2 * X.size() - 1; i += 2) {
    int height = X[i / 2 + 1] - X[i / 2];
    for (int j = 1; j < 2 * Y.size() - 1; j += 2) {
      int width = Y[i / 2 + 1] - Y[i / 2];
      mp[i][j] = height * width;
    }
  }
  // 壁埋め
  for (int i = 0; i < N; i++) {
    int aidx = lower_bound(X.begin(), X.end(), A[i]) - X.begin();
    int bidx = lower_bound(X.begin(), X.end(), B[i]) - X.begin();
    int cidx = lower_bound(Y.begin(), Y.end(), C[i]) - Y.begin();
    for (int j = 2 * aidx; j <= 2 * bidx; j++) {
      mp[j][2 * cidx] = -1;
    }
  }
  for (int i = 0; i < M; i++) {
    int didx = lower_bound(X.begin(), X.end(), D[i]) - X.begin();
    int eidx = lower_bound(Y.begin(), Y.end(), E[i]) - Y.begin();
    int fidx = lower_bound(Y.begin(), Y.end(), F[i]) - Y.begin();
    for (int j = 2 * eidx; j <= 2 * fidx; j++) {
      mp[2 * didx][j] = -1;
    }
  }

  // BFS
  int sx = 2 * (lower_bound(X.begin(), X.end(), 0) - X.begin());
  int sy = 2 * (lower_bound(Y.begin(), Y.end(), 0) - Y.begin());
  queue<pair<int, int>> q;
  q.emplace(sx, sy);
  ll ans = 0;
  vector<vector<bool>> is_reached(2 * X.size() - 1,
                                  vector<bool>(2 * Y.size() - 1, false));
  is_reached[sx][sy] = true;
  vector<pair<int, int>> motion;
  motion.emplace_back(1, 0);
  motion.emplace_back(-1, 0);
  motion.emplace_back(0, 1);
  motion.emplace_back(0, -1);
  bool reached_edge = false;
  while (!q.empty() && !reached_edge) {
    pair<int, int> cp = q.front();
    q.pop();
    for (auto m : motion) {
      int nx = cp.first + m.first;
      int ny = cp.second + m.second;
      if (nx < 0 || 2 * X.size() - 1 < nx || ny < 0 || 2 * Y.size() - 1 < ny) {
        reached_edge = true;
        break;
      }
      if (mp[nx][ny] == -1 || is_reached[nx][ny])
        continue;
      is_reached[nx][ny] = true;
      ans += mp[nx][ny];
      q.emplace(nx, ny);
    }
  }
  if (reached_edge)
    cout << "INF\\n";
  else
    cout << ans << '\\n';

  return 0;
}