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

// Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define int long long
#define double long double

int n;
int ans = 1e18;
vector<int> up[200003];
vector<int> down[200003];
vector<int> leftt[200003];
vector<int> rightt[200003];
vector<int> sum[400002];
vector<int> sum1[400002];
set<int> up1, down1, leftt1, rightt1;
int arr[200003][2];
char dir[200002];

void checkupdown() {
  for (int x = 0; x <= 200000; x++) {
    if (up1.find(x) != up1.end() && down1.find(x) != down1.end()) {
      for (auto y : up[x]) {
        int low = 0;
        int high = down[x].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (down[x][mid] < y)
            low = mid + 1;
          else
            high = mid;
        }
        if (down[x][low] < y)
          continue;
        ans = min(ans, (down[x][low] - y) * 5);
      }
    }
  }
}
void checklefttrightt() {
  for (int x = 0; x <= 200000; x++) {
    if (leftt1.find(x) != leftt1.end() && rightt1.find(x) != rightt1.end()) {
      for (auto y : rightt[x]) {
        int low = 0;
        int high = leftt[x].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (leftt[x][mid] < y)
            low = mid + 1;
          else
            high = mid;
        }
        if (leftt[x][low] < y)
          continue;
        ans = min(ans, (leftt[x][low] - y) * 5);
      }
    }
  }
}
void checkdownleft() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : down[i])
      sum[i + j].push_back(i);
    for (auto j : leftt[i])
      sum1[i + j].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (sum1[i][mid] < j)
            low = mid + 1;
          else
            high = mid;
        }
        if (sum1[i][low] < j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkdownright() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : down[i])
      sum[j - i + 200000].push_back(i);
    for (auto j : rightt[i])
      sum1[i - j + 200000].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high + 1) / 2;
          if (sum1[i][mid] > j)
            high = mid - 1;
          else
            low = mid;
        }
        if (sum1[i][low] > j)
          continue;
        ans = min(ans, (-sum1[i][low] + j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkupleft() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : up[i])
      sum[j - i + 200000].push_back(i);
    for (auto j : leftt[i])
      sum1[i - j + 200000].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (sum1[i][mid] < j)
            low = mid + 1;
          else
            high = mid;
        }
        if (sum1[i][low] < j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkupright() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : up[i])
      sum[i + j].push_back(i);
    for (auto j : rightt[i])
      sum1[i + j].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high + 1) / 2;
          if (sum1[i][mid] > j)
            high = mid - 1;
          else
            low = mid;
        }
        if (sum1[i][low] > j)
          continue;
        ans = min(ans, (-sum1[i][low] + j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
int32_t main() {
  IOS;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> dir[i];
    if (dir[i] == 'U') {
      up[arr[i][0]].push_back(arr[i][1]);
      up1.insert(arr[i][0]);
    }
    if (dir[i] == 'D') {
      down[arr[i][0]].push_back(arr[i][1]);
      down1.insert(arr[i][0]);
    }
    if (dir[i] == 'L') {
      leftt[arr[i][1]].push_back(arr[i][0]);
      leftt1.insert(arr[i][1]);
    }
    if (dir[i] == 'R') {
      rightt[arr[i][1]].push_back(arr[i][0]);
      rightt1.insert(arr[i][1]);
    }
  }
  for (int i = 0; i <= 200000; i++) {
    sort(up[i].begin(), up[i].end());
    sort(down[i].begin(), down[i].end());
    sort(leftt[i].begin(), leftt[i].end());
    sort(rightt[i].begin(), rightt[i].end());
  }
  checkupdown();
  checklefttrightt();
  checkdownleft();
  checkdownright();
  checkupleft();
  checkupright();
  if (ans == 1e18)
    print("SAFE");
  else
    print(ans);
}

Error: 0

Fixed submission:

// Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

template <typename T> void print(T t) { cout << t << endl; }
template <typename T, typename... Args> void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define int long long
#define double long double

int n;
int ans = 1e18;
vector<int> up[200002];
vector<int> down[200002];
vector<int> leftt[200002];
vector<int> rightt[200002];
vector<int> sum[400002];
vector<int> sum1[400002];
set<int> up1, down1, leftt1, rightt1;
int arr[200002][2];
char dir[200002];

void checkupdown() {
  for (int x = 0; x <= 200000; x++) {
    if (up1.find(x) != up1.end() && down1.find(x) != down1.end()) {
      for (auto y : up[x]) {
        int low = 0;
        int high = down[x].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (down[x][mid] < y)
            low = mid + 1;
          else
            high = mid;
        }
        if (down[x][low] < y)
          continue;
        ans = min(ans, (down[x][low] - y) * 5);
      }
    }
  }
}
void checklefttrightt() {
  for (int x = 0; x <= 200000; x++) {
    if (leftt1.find(x) != leftt1.end() && rightt1.find(x) != rightt1.end()) {
      for (auto y : rightt[x]) {
        int low = 0;
        int high = leftt[x].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (leftt[x][mid] < y)
            low = mid + 1;
          else
            high = mid;
        }
        if (leftt[x][low] < y)
          continue;
        ans = min(ans, (leftt[x][low] - y) * 5);
      }
    }
  }
}
void checkdownleft() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : down[i])
      sum[i + j].push_back(i);
    for (auto j : leftt[i])
      sum1[i + j].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (sum1[i][mid] < j)
            low = mid + 1;
          else
            high = mid;
        }
        if (sum1[i][low] < j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkdownright() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : down[i])
      sum[j - i + 200000].push_back(i);
    for (auto j : rightt[i])
      sum1[i - j + 200000].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high + 1) / 2;
          if (sum1[i][mid] > j)
            high = mid - 1;
          else
            low = mid;
        }
        if (sum1[i][low] > j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkupleft() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : up[i])
      sum[j - i + 200000].push_back(i);
    for (auto j : leftt[i])
      sum1[i - j + 200000].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high) / 2;
          if (sum1[i][mid] < j)
            low = mid + 1;
          else
            high = mid;
        }
        if (sum1[i][low] < j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
void checkupright() {
  for (int i = 0; i <= 200002; i++) {
    for (auto j : up[i])
      sum[i + j].push_back(i);
    for (auto j : rightt[i])
      sum1[i + j].push_back(j);
  }
  for (int i = 0; i <= 400002; i++) {
    if (sum[i].size() && sum1[i].size()) {
      sort(sum[i].begin(), sum[i].end());
      sort(sum1[i].begin(), sum1[i].end());
      for (auto j : sum[i]) {
        int low = 0;
        int high = sum1[i].size() - 1;
        while (low < high) {
          int mid = (low + high + 1) / 2;
          if (sum1[i][mid] > j)
            high = mid - 1;
          else
            low = mid;
        }
        if (sum1[i][low] > j)
          continue;
        ans = min(ans, (sum1[i][low] - j) * 10);
      }
    }
    sum[i].clear();
    sum1[i].clear();
  }
}
int32_t main() {
  IOS;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> dir[i];
    if (dir[i] == 'U') {
      up[arr[i][0]].push_back(arr[i][1]);
      up1.insert(arr[i][0]);
    }
    if (dir[i] == 'D') {
      down[arr[i][0]].push_back(arr[i][1]);
      down1.insert(arr[i][0]);
    }
    if (dir[i] == 'L') {
      leftt[arr[i][1]].push_back(arr[i][0]);
      leftt1.insert(arr[i][1]);
    }
    if (dir[i] == 'R') {
      rightt[arr[i][1]].push_back(arr[i][0]);
      rightt1.insert(arr[i][1]);
    }
  }
  for (int i = 0; i <= 200000; i++) {
    sort(up[i].begin(), up[i].end());
    sort(down[i].begin(), down[i].end());
    sort(leftt[i].begin(), leftt[i].end());
    sort(rightt[i].begin(), rightt[i].end());
  }
  checkupdown();
  checklefttrightt();
  checkdownleft();
  checkdownright();
  checkupleft();
  checkupright();
  if (ans == 1e18)
    print("SAFE");
  else
    print(ans);
}