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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

using T = tuple<int, int, char>;
const int math = 200001;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<T> a(n);
  int x, y;
  char z;
  rep(i, n) {
    cin >> x >> y >> z;
    a[i] = T(x, y, z);
  }
  vector<set<int>> U(math), D(math), R(math), L(math);
  using T2 = tuple<int, int, int>;
  set<T2> uplus, umin, dplus, dmin;
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'U') {
      U[x].insert(y);
      uplus.insert(T2(x + y, x, y));
      umin.insert(T2(x - y, x, y));
    }
    if (z == 'D') {
      D[x].insert(y);
      dplus.insert(T2(y + x, x, y));
      dmin.insert(T2(x - y, x, y));
    }
    if (z == 'R')
      R[y].insert(x);
    if (z == 'L')
      L[y].insert(x);
  }

  int ans2 = inf;
  int ans3 = inf;
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'U' || z == 'D')
      continue;
    if (z == 'R') {
      int pl = x + y, mi = x - y;
      auto itr = dmin.lower_bound(T2(mi, x, 0));
      if (itr != dmin.end() && get<0>(*itr) == mi) {
        int ans = get<1>(*itr) - x;
        ans3 = min(ans, ans3);
      }
      itr = uplus.lower_bound(T2(pl, x, 0));
      if (itr != uplus.end() && get<0>(*itr) == pl) {
        int ans = get<1>(*itr) - x;
        ans3 = min(ans, ans3);
      }
    }
    if (z == 'L') {
      int pl = x + y, mi = x - y;
      auto itr = umin.lower_bound(T2(mi, x, 0));
      if (itr != umin.begin()) {
        itr--;
        if (get<0>(*itr) == mi) {
          int ans = get<1>(*itr) - x;
          if (ans < 0)
            ans3 = min(-ans, ans3);
        }
      }
      itr = dplus.lower_bound(T2(pl, x, 0));
      if (itr != dplus.begin()) {
        itr--;
        if (get<0>(*itr) == pl) {
          int ans = get<1>(*itr) - x;
          if (ans < 0)
            ans3 = min(-ans, ans3);
        }
      }
    }
  }
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'D' || z == 'L')
      continue;
    if (z == 'U') {
      if (D[x].upper_bound(y) != D[x].end()) {
        int dist = *D[x].upper_bound(y);
        dist -= y;
        ans2 = min(ans2, dist);
      }
    }
    if (z == 'R') {
      if (L[y].upper_bound(x) != L[y].end()) {
        int dist = *L[y].upper_bound(x);
        dist -= x;
        ans2 = min(ans2, dist);
      }
    }
  }
  if (ans2 == inf && ans3 == inf) {
    cout << "SAFE" << endl;
    return 0;
  }
  if (ans2 < inf)
    ans2 *= 5;
  if (ans3 < inf)
    ans3 *= 10;
  cout << min(ans2, ans3) << endl;
} /*
 ./problem.exe
 */

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

using T = tuple<int, int, char>;
const int math = 200001;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<T> a(n);
  int x, y;
  char z;
  rep(i, n) {
    cin >> x >> y >> z;
    a[i] = T(x, y, z);
  }
  vector<set<int>> U(math), D(math), R(math), L(math);
  using T2 = tuple<int, int, int>;
  set<T2> uplus, umin, dplus, dmin;
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'U') {
      U[x].insert(y);
      uplus.insert(T2(x + y, x, y));
      umin.insert(T2(x - y, x, y));
    }
    if (z == 'D') {
      D[x].insert(y);
      dplus.insert(T2(y + x, x, y));
      dmin.insert(T2(x - y, x, y));
    }
    if (z == 'R')
      R[y].insert(x);
    if (z == 'L')
      L[y].insert(x);
  }

  int ans2 = inf;
  int ans3 = inf;
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'U' || z == 'D')
      continue;
    if (z == 'R') {
      int pl = x + y, mi = x - y;
      auto itr = dmin.lower_bound(T2(mi, x, 0));
      if (itr != dmin.end() && get<0>(*itr) == mi) {
        int ans = get<1>(*itr) - x;
        ans3 = min(ans, ans3);
      }
      itr = uplus.lower_bound(T2(pl, x, 0));
      if (itr != uplus.end() && get<0>(*itr) == pl) {
        int ans = get<1>(*itr) - x;
        ans3 = min(ans, ans3);
      }
    }
    if (z == 'L') {
      int pl = x + y, mi = x - y;
      auto itr = umin.lower_bound(T2(mi, x, 0));
      itr--;
      if (itr != umin.end() && get<0>(*itr) == mi) {
        int ans = get<1>(*itr) - x;
        if (ans < 0)
          ans3 = min(-ans, ans3);
      }
      itr = dplus.lower_bound(T2(pl, x, 0));
      itr--;
      if (itr != dplus.end() && get<0>(*itr) == pl) {
        int ans = get<1>(*itr) - x;
        if (ans < 0)
          ans3 = min(-ans, ans3);
      }
    }
  }
  rep(i, n) {
    tie(x, y, z) = a[i];
    if (z == 'D' || z == 'L')
      continue;
    if (z == 'U') {
      if (D[x].upper_bound(y) != D[x].end()) {
        int dist = *D[x].upper_bound(y);
        dist -= y;
        ans2 = min(ans2, dist);
      }
    }
    if (z == 'R') {
      if (L[y].upper_bound(x) != L[y].end()) {
        int dist = *L[y].upper_bound(x);
        dist -= x;
        ans2 = min(ans2, dist);
      }
    }
  }
  if (ans2 == inf && ans3 == inf) {
    cout << "SAFE" << endl;
    return 0;
  }
  if (ans2 < inf)
    ans2 *= 5;
  if (ans3 < inf)
    ans3 *= 10;
  cout << min(ans2, ans3) << endl;
} /*
 ./problem.exe
 */