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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000000
#define MAX 2000005
using ll = long long;
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<char> U(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
  }

  int ans = INF;
  // Rと他のものとの衝突
  {
    vector<pair<int, int>> a1, a2, a3;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'D') {
        a1.push_back(make_pair(X[i] - Y[i], X[i]));
      } else if (U[i] == 'L') {
        a2.push_back(make_pair(Y[i], X[i]));
      } else if (U[i] == 'U') {
        a3.push_back(make_pair(X[i] + Y[i], X[i]));
      }
    }
    if (a1.size() > 0) {
      sort(a1.begin(), a1.end());
    }
    if (a2.size() > 0) {
      sort(a2.begin(), a2.end());
    }
    if (a3.size() > 0) {
      sort(a3.begin(), a3.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'R') {
        pair<int, int> p = make_pair(X[i] - Y[i], X[i]);
        int k;
        if (a1.size() > 0) {
          k = upper_bound(a1.begin(), a1.end(), p) - a1.begin();
          if (k < a1.size()) {
            if (a1[k].first == X[i] - Y[i]) {
              ans = min(ans, 10 * (a1[k].second - X[i]));
            }
          }
        }
        p = make_pair(Y[i], X[i]);
        if (a2.size() > 0) {
          k = upper_bound(a2.begin(), a2.end(), p) - a2.begin();
          if (k < a2.size()) {
            if (a2[k].first == Y[i]) {
              ans = min(ans, 5 * (a2[k].second - X[i]));
            }
          }
        }
        p = make_pair(X[i] + Y[i], X[i]);
        if (a3.size() > 0) {
          k = upper_bound(a3.begin(), a3.end(), p) - a3.begin();
          if (k < a3.size()) {
            if (a3[k].first == X[i] + Y[i]) {
              ans = min(ans, 10 * (a3[k].second - X[i]));
            }
          }
        }
      }
    }
  }

  // Lと他のもの
  {
    vector<pair<int, int>> a1, a2, a3;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'U') {
        a1.push_back(make_pair(X[i] - Y[i], -X[i]));
      } else if (U[i] == 'R') {
        a2.push_back(make_pair(Y[i], -X[i]));
      } else if (U[i] == 'D') {
        a3.push_back(make_pair(X[i] + Y[i], -X[i]));
      }
    }
    if (a1.size() > 0) {
      sort(a1.begin(), a1.end());
    }
    if (a2.size() > 0) {
      sort(a2.begin(), a2.end());
    }
    if (a3.size() > 0) {
      sort(a3.begin(), a3.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'L') {
        int k;
        pair<int, int> p = make_pair(X[i] - Y[i], -X[i]);
        if (a1.size() > 0) {
          k = upper_bound(a1.begin(), a1.end(), p) - a1.begin();
          if (k < a1.size()) {
            if (a1[k].first == X[i] - Y[i]) {
              ans = min(ans, 10 * (X[i] + a1[k].second));
            }
          }
        }
        p = make_pair(Y[i], -X[i]);
        if (a2.size() > 0) {
          k = upper_bound(a2.begin(), a2.end(), p) - a2.begin();
          if (k < a2.size()) {
            if (a2[k].first == Y[i]) {
              ans = min(ans, 5 * (X[i] + a2[k].second));
            }
          }
        }
        p = make_pair(X[i] + Y[i], -X[i]);
        if (a3.size() > 0) {
          k = upper_bound(a3.begin(), a3.end(), p) - a3.begin();
          if (k < a3.size()) {
            if (a3[k].first == X[i] + Y[i]) {
              ans = min(ans, 10 * (X[i] + a3[k].second));
            }
          }
        }
      }
    }
  }

  // UとDの衝突
  {
    vector<pair<int, int>> a;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'D') {
        a.push_back(make_pair(X[i], Y[i]));
      }
    }
    if (a.size() > 0) {
      sort(a.begin(), a.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'U') {
        pair<int, int> p = make_pair(X[i], Y[i]);
        if (a.size() > 0) {
          int k = upper_bound(a.begin(), a.end(), p) - a.begin();
          if (k < a.size()) {
            if (a[k].first == X[i]) {
              ans = min(ans, 5 * (a[k].second - Y[i]));
            }
          }
        }
      }
    }
  }

  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000000
#define MAX 2000005
using ll = long long;
using Graph = vector<vector<int>>;

int main() {
  int N;
  cin >> N;
  vector<int> X(N), Y(N);
  vector<char> U(N);
  for (int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i] >> U[i];
  }

  int ans = INF;
  // Rと他のものとの衝突
  {
    vector<pair<int, int>> a1, a2, a3;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'D') {
        a1.push_back(make_pair(X[i] - Y[i], X[i]));
      } else if (U[i] == 'L') {
        a2.push_back(make_pair(Y[i], X[i]));
      } else if (U[i] == 'U') {
        a3.push_back(make_pair(X[i] + Y[i], X[i]));
      }
    }
    if (a1.size() > 0) {
      sort(a1.begin(), a1.end());
    }
    if (a2.size() > 0) {
      sort(a2.begin(), a2.end());
    }
    if (a3.size() > 0) {
      sort(a3.begin(), a3.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'R') {
        pair<int, int> p = make_pair(X[i] - Y[i], X[i]);
        int k;
        if (a1.size() > 0) {
          k = upper_bound(a1.begin(), a1.end(), p) - a1.begin();
          if (k < a1.size()) {
            if (a1[k].first == X[i] - Y[i]) {
              ans = min(ans, 10 * (a1[k].second - X[i]));
            }
          }
        }
        p = make_pair(Y[i], X[i]);
        if (a2.size() > 0) {
          k = upper_bound(a2.begin(), a2.end(), p) - a2.begin();
          if (k < a2.size()) {
            if (a2[k].first == Y[i]) {
              ans = min(ans, 5 * (a2[k].second - X[i]));
            }
          }
        }
        p = make_pair(X[i] + Y[i], X[i]);
        if (a3.size() > 0) {
          k = upper_bound(a3.begin(), a3.end(), p) - a3.begin();
          if (k < a3.size()) {
            if (a3[k].first == X[i] + Y[i]) {
              ans = min(ans, 10 * (a3[k].second - X[i]));
            }
          }
        }
      }
    }
  }

  // Lと他のもの
  {
    vector<pair<int, int>> a1, a2, a3;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'U') {
        a1.push_back(make_pair(X[i] - Y[i], -X[i]));
      } else if (U[i] == 'R') {
        a2.push_back(make_pair(Y[i], -X[i]));
      } else if (U[i] == 'D') {
        a3.push_back(make_pair(X[i] + Y[i], -X[i]));
      }
    }
    if (a1.size() > 0) {
      sort(a1.begin(), a1.end());
    }
    if (a2.size() > 0) {
      sort(a2.begin(), a2.end());
    }
    if (a3.size() > 0) {
      sort(a3.begin(), a3.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'L') {
        int k;
        pair<int, int> p = make_pair(X[i] - Y[i], -X[i]);
        if (a1.size() > 0) {
          k = upper_bound(a1.begin(), a1.end(), p) - a1.begin();
          if (k < a1.size()) {
            if (a1[i].first == X[i] - Y[i]) {
              ans = min(ans, 10 * (X[i] + a1[k].second));
            }
          }
        }
        p = make_pair(Y[i], -X[i]);
        if (a2.size() > 0) {
          k = upper_bound(a2.begin(), a2.end(), p) - a2.begin();
          if (k < a2.size()) {
            if (a2[i].first == Y[i]) {
              ans = min(ans, 5 * (X[i] + a2[k].second));
            }
          }
        }
        p = make_pair(X[i] + Y[i], -X[i]);
        if (a3.size() > 0) {
          k = upper_bound(a3.begin(), a3.end(), p) - a3.begin();
          if (k < a3.size()) {
            if (a3[k].first == X[i] + Y[i]) {
              ans = min(ans, 10 * (X[i] + a3[k].second));
            }
          }
        }
      }
    }
  }

  // UとDの衝突
  {
    vector<pair<int, int>> a;
    for (int i = 0; i < N; i++) {
      if (U[i] == 'D') {
        a.push_back(make_pair(X[i], Y[i]));
      }
    }
    if (a.size() > 0) {
      sort(a.begin(), a.end());
    }
    for (int i = 0; i < N; i++) {
      if (U[i] == 'U') {
        pair<int, int> p = make_pair(X[i], Y[i]);
        if (a.size() > 0) {
          int k = upper_bound(a.begin(), a.end(), p) - a.begin();
          if (k < a.size()) {
            if (a[k].first == X[i]) {
              ans = min(ans, 5 * (a[k].second - Y[i]));
            }
          }
        }
      }
    }
  }

  if (ans == INF) {
    cout << "SAFE" << endl;
  } else {
    cout << ans << endl;
  }
}