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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

const int maxn = 200005;
const int shift = 200005;

int N, ans = 2147483647;
char ss[maxn][5];
int X[maxn], Y[maxn];
set<int> rx[200005], uy[200005];
set<pair<int, int>> dxay[500005], rxay[500005], rxmy[500005], uxmy[500005];

int main() {
  for (int i = 0; i <= 500000; ++i) {
    if (i <= 200000) {
      rx[i].clear();
      uy[i].clear();
    }
    dxay[i].clear();
    rxay[i].clear();
    rxmy[i].clear();
    uxmy[i].clear();
  }
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d%s", &X[i], &Y[i], ss[i]);
    if (ss[i][0] == 'U')
      rxay[X[i] + Y[i]].insert(make_pair(X[i], Y[i]));
    else if (ss[i][0] == 'D') {
      uy[X[i]].insert(Y[i]);
      rxmy[X[i] - Y[i] + shift].insert(make_pair(X[i], Y[i]));
    } else if (ss[i][0] == 'L') {
      uxmy[X[i] - Y[i] + shift].insert(make_pair(X[i], Y[i]));
      rx[Y[i]].insert(X[i]);
      dxay[X[i] + Y[i]].insert(make_pair(X[i], Y[i]));
    }
  }

  for (int i = 1; i <= N; ++i) {
    if (ss[i][0] == 'U') {
      // cerr << X[i] << " " << uy[X[i]].size() << endl;
      if (uy[X[i]].size() > 0) {
        set<int>::iterator it;
        it = uy[X[i]].lower_bound(Y[i]);
        if (it != uy[X[i]].end() && *it > Y[i])
          ans = min(ans, abs(*it - Y[i]) * 5);
      }
      if (uxmy[X[i] - Y[i] + shift].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = uxmy[X[i] - Y[i] + shift].lower_bound(make_pair(X[i], Y[i]));
        if (it != uxmy[X[i] - Y[i] + shift].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
    } else if (ss[i][0] == 'R') {
      if (rxay[X[i] + Y[i]].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = rxay[X[i] + Y[i]].upper_bound(make_pair(X[i], Y[i]));
        if (it != rxay[X[i] + Y[i]].end() && (*it).first > X[i])
          ans = min(ans, abs(Y[i] - (*it).second) * 10);
      }
      if (rxmy[X[i] - Y[i] + shift].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = rxmy[X[i] - Y[i] + shift].upper_bound(make_pair(X[i], Y[i]));
        if (it != rxmy[X[i] - Y[i] + shift].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
      if (rx[Y[i]].size() > 0) {
        set<int>::iterator it;
        it = rx[Y[i]].upper_bound(X[i]);
        if (it != uy[X[i]].end() && *it > X[i])
          ans = min(ans, abs(*it - X[i]) * 5);
      }
    } else if (ss[i][0] == 'D') {
      if (dxay[X[i] + Y[i]].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = dxay[X[i] + Y[i]].upper_bound(make_pair(X[i], Y[i]));
        if (it != dxay[X[i] + Y[i]].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
    }
  }

  if (ans == 2147483647)
    printf("SAFE\\n");
  else
    printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

const int maxn = 200005;
const int shift = 200005;

int N, ans = 2147483647;
char ss[maxn][5];
int X[maxn], Y[maxn];
set<int> rx[200005], uy[200005];
set<pair<int, int>> dxay[200005], rxay[200005], rxmy[500005], uxmy[500005];

int main() {
  for (int i = 0; i <= 500000; ++i) {
    if (i <= 200000) {
      rx[i].clear();
      uy[i].clear();
      dxay[i].clear();
      rxay[i].clear();
    }
    rxmy[i].clear();
    uxmy[i].clear();
  }
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d%s", &X[i], &Y[i], ss[i]);
    if (ss[i][0] == 'U')
      rxay[X[i] + Y[i]].insert(make_pair(X[i], Y[i]));
    else if (ss[i][0] == 'D') {
      uy[X[i]].insert(Y[i]);
      rxmy[X[i] - Y[i] + shift].insert(make_pair(X[i], Y[i]));
    } else if (ss[i][0] == 'L') {
      uxmy[X[i] - Y[i] + shift].insert(make_pair(X[i], Y[i]));
      rx[Y[i]].insert(X[i]);
      dxay[X[i] + Y[i]].insert(make_pair(X[i], Y[i]));
    }
  }

  for (int i = 1; i <= N; ++i) {
    if (ss[i][0] == 'U') {
      // cerr << X[i] << " " << uy[X[i]].size() << endl;
      if (uy[X[i]].size() > 0) {
        set<int>::iterator it;
        it = uy[X[i]].lower_bound(Y[i]);
        if (it != uy[X[i]].end() && *it > Y[i])
          ans = min(ans, abs(*it - Y[i]) * 5);
      }
      if (uxmy[X[i] - Y[i] + shift].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = uxmy[X[i] - Y[i] + shift].lower_bound(make_pair(X[i], Y[i]));
        if (it != uxmy[X[i] - Y[i] + shift].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
    } else if (ss[i][0] == 'R') {
      if (rxay[X[i] + Y[i]].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = rxay[X[i] + Y[i]].upper_bound(make_pair(X[i], Y[i]));
        if (it != rxay[X[i] + Y[i]].end() && (*it).first > X[i])
          ans = min(ans, abs(Y[i] - (*it).second) * 10);
      }
      if (rxmy[X[i] - Y[i] + shift].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = rxmy[X[i] - Y[i] + shift].upper_bound(make_pair(X[i], Y[i]));
        if (it != rxmy[X[i] - Y[i] + shift].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
      if (rx[Y[i]].size() > 0) {
        set<int>::iterator it;
        it = rx[Y[i]].upper_bound(X[i]);
        if (it != uy[X[i]].end() && *it > X[i])
          ans = min(ans, abs(*it - X[i]) * 5);
      }
    } else if (ss[i][0] == 'D') {
      if (dxay[X[i] + Y[i]].size() > 0) {
        set<pair<int, int>>::iterator it;
        it = dxay[X[i] + Y[i]].upper_bound(make_pair(X[i], Y[i]));
        if (it != dxay[X[i] + Y[i]].end() && (*it).first > X[i])
          ans = min(ans, abs((*it).second - Y[i]) * 10);
      }
    }
  }

  if (ans == 2147483647)
    printf("SAFE\\n");
  else
    printf("%d\\n", ans);

  return 0;
}