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

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

#define PP pair<P, char>

ll n;
vector<PP> s;
vector<ll> RtD[400500], LtD[400500], RtU[400500], LtU[400500];
ll ans = INF;
void solve1(char a, char b) {
  ll f = -1;
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    if (s[i].second == b and f != -1) {
      ans = min(ans, abs(f - y) * 5);
    }
    if (s[i].second == a) {
      f = y;
    }
    if (x != s[i + 1].first.first) {
      f = -1;
    }
  }
}

ll T1(ll a, ll b) { return 200000 + a - b; }
ll T2(ll a, ll b) { return a + b; }
int main() {
  cin >> n;
  s.resize(n);
  REP(i, n) { cin >> s[i].first.first >> s[i].first.second >> s[i].second; }
  // UD
  sort(ALL(s));
  s.push_back(PP(P(INF, INF), INF));
  solve1('U', 'D');
  s.pop_back();
  REP(i, n) { swap(s[i].first.first, s[i].first.second); }
  // RL
  sort(ALL(s));
  s.push_back(PP(P(INF, INF), INF));
  solve1('R', 'L');
  s.pop_back();
  REP(i, n) { swap(s[i].first.first, s[i].first.second); }
  // add
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    if (s[i].second == 'R') {
      RtD[T1(x, y)].push_back(-y);
      RtU[T2(x, y)].push_back(y);
    }
    if (s[i].second == 'L') {
      LtD[T2(x, y)].push_back(-y);
      LtU[T1(x, y)].push_back(y);
    }
  }
  REP(i, 400100) {
    RtD[i].push_back(INF);
    LtD[i].push_back(INF);
    RtU[i].push_back(INF);
    LtU[i].push_back(INF);
    sort(ALL(RtD[i]));
    sort(ALL(LtD[i]));
    sort(ALL(RtU[i]));
    sort(ALL(LtU[i]));
  }
  ///*
  // solve
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    // cout << x << " " << y << " " << s[i].second<< endl;
    if (s[i].second == 'D') {
      ll a1 = *upper_bound(ALL(RtD[T1(x, y)]), -y);
      // cout << a1 << "!!!!"<< endl;
      if (a1 != INF)
        ans = min(ans, abs(a1 + y) * 10);
      ll a2 = *upper_bound(ALL(LtD[T2(x, y)]), -y);
      // cout << a2 << "!"<< endl;
      if (a2 != INF)
        ans = min(ans, abs(a2 + y) * 10);
    }
    if (s[i].second == 'U') {
      ll a1 = *upper_bound(ALL(RtU[T2(x, y)]), y);
      // cout << a1 << "!!!"<< endl;
      if (a1 != INF)
        ans = min(ans, abs(a1 - y) * 10);
      ll a2 = *upper_bound(ALL(LtU[T1(x, y)]), y);
      // cout << a2 << "!!"<< endl;
      if (a2 != INF)
        ans = min(ans, abs(a2 - y) * 10);
    }
  }
  //*/
  if (ans == INF) {
    cout << "SAFE" << endl;
    return 0;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

#define PP pair<P, char>

ll n;
vector<PP> s;
vector<ll> RtD[400100], LtD[400100], RtU[400100], LtU[400100];
ll ans = INF;
void solve1(char a, char b) {
  ll f = -1;
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    if (s[i].second == b and f != -1) {
      ans = min(ans, abs(f - y) * 5);
    }
    if (s[i].second == a) {
      f = y;
    }
    if (x != s[i + 1].first.first) {
      f = -1;
    }
  }
}

ll T1(ll a, ll b) { return 200000 + a - b; }
ll T2(ll a, ll b) { return a + b; }
int main() {
  cin >> n;
  s.resize(n);
  REP(i, n) { cin >> s[i].first.first >> s[i].first.second >> s[i].second; }
  // UD
  sort(ALL(s));
  s.push_back(PP(P(INF, INF), INF));
  solve1('U', 'D');
  s.pop_back();
  REP(i, n) { swap(s[i].first.first, s[i].first.second); }
  // RL
  sort(ALL(s));
  s.push_back(PP(P(INF, INF), INF));
  solve1('R', 'L');
  s.pop_back();
  REP(i, n) { swap(s[i].first.first, s[i].first.second); }
  // add
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    if (s[i].second == 'R') {
      RtD[T1(x, y)].push_back(-y);
      RtU[T2(x, y)].push_back(y);
    }
    if (s[i].second == 'L') {
      LtD[T2(x, y)].push_back(-y);
      LtU[T1(x, y)].push_back(y);
    }
  }
  REP(i, 400000) {
    RtD[i].push_back(INF);
    LtD[i].push_back(INF);
    RtU[i].push_back(INF);
    LtU[i].push_back(INF);
    sort(ALL(RtD[i]));
    sort(ALL(LtD[i]));
    sort(ALL(RtU[i]));
    sort(ALL(LtU[i]));
  }
  ///*
  // solve
  REP(i, n) {
    ll x = s[i].first.first, y = s[i].first.second;
    // cout << x << " " << y << " " << s[i].second<< endl;
    if (s[i].second == 'D') {
      ll a1 = *upper_bound(ALL(RtD[T1(x, y)]), -y);
      // cout << a1 << "!!!!"<< endl;
      if (a1 != INF)
        ans = min(ans, abs(a1 + y) * 10);
      ll a2 = *upper_bound(ALL(LtD[T2(x, y)]), -y);
      // cout << a2 << "!"<< endl;
      if (a2 != INF)
        ans = min(ans, abs(a2 + y) * 10);
    }
    if (s[i].second == 'U') {
      ll a1 = *upper_bound(ALL(RtU[T2(x, y)]), y);
      // cout << a1 << "!!!"<< endl;
      if (a1 != INF)
        ans = min(ans, abs(a1 - y) * 10);
      ll a2 = *upper_bound(ALL(LtU[T1(x, y)]), y);
      // cout << a2 << "!!"<< endl;
      if (a2 != INF)
        ans = min(ans, abs(a2 - y) * 10);
    }
  }
  //*/
  if (ans == INF) {
    cout << "SAFE" << endl;
    return 0;
  }
  cout << ans << endl;
}