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

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int maxn = 4e5 + 5;
const int maxn1 = 2e5 + 3;
vector<pair<int, int>> su1[maxn];
vector<pair<int, int>> su2[maxn];
vector<pair<int, int>> ra1[maxn];
vector<pair<int, int>> ra2[maxn];
vector<pair<int, int>> x1[maxn];
vector<pair<int, int>> y2[maxn];
int go(vector<pair<int, int>> z) {
  if (z.size() <= 1)
    return 1e17;
  int ans = 1e17;
  vector<int> z0;
  vector<int> z1;
  int n = z.size();
  for (int i = 0; i < n; ++i) {
    if (z[i].second == 0) {
      z1.push_back(z[i].first);
    } else {
      z0.push_back(z[i].first);
    }
  }
  z1.push_back(1e17);
  sort(z1.begin(), z1.end());
  for (int i = 0; i < z0.size(); ++i) {
    ans = min(ans, (*lower_bound(z1.begin(), z1.end(), z0[i]) - z0[i]) * 10);
  }
  // if(ans<1e15) {cout<<z[0].first<<' '<<z[0].second<<endl;cout<<z[1].first<<'
  // '<<z[1].second<<endl;}
  return ans;
}
int go1(vector<pair<int, int>> z) {
  if (z.size() <= 1)
    return 1e17;
  int ans = 1e17;
  vector<int> z0;
  vector<int> z1;
  int n = z.size();
  for (int i = 0; i < n; ++i) {
    if (z[i].second == 0) {
      z0.push_back(z[i].first);
    } else {
      z1.push_back(z[i].first);
    }
  }
  z1.push_back(1e17);
  sort(z1.begin(), z1.end());
  for (int i = 0; i < z0.size(); ++i) {
    ans = min(ans, (*lower_bound(z1.begin(), z1.end(), z0[i]) - z0[i]) * 5);
  }
  // if(ans<1e15) {cout<<z[0].first<<' '<<z[0].second<<endl;cout<<z[1].first<<'
  // '<<z[1].second<<endl;}
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<pair<int, int>, char> a[n];
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    a[i].first.first = x;
    a[i].first.second = y;
    cin >> a[i].second;
  }
  for (int i = 0; i < maxn; ++i) {
    su1[i].clear();
    su2[i].clear();
    ra1[i].clear();
    ra2[i].clear();
  }
  for (int i = 0; i < n; ++i) {
    if (a[i].second == 'U') {
      su1[a[i].first.first + a[i].first.second].push_back(
          {a[i].first.first, 0});
      ra1[a[i].first.first - a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 0});
      x1[a[i].first.first].push_back({a[i].first.second, 0});
    } else if (a[i].second == 'D') {
      su2[a[i].first.first + a[i].first.second].push_back(
          {-a[i].first.first, 0});
      ra2[a[i].first.first - a[i].first.second + maxn1].push_back(
          {a[i].first.first, 0});
      x1[a[i].first.first].push_back({a[i].first.second, 1});
    } else if (a[i].second == 'L') {
      su2[a[i].first.first + a[i].first.second].push_back(
          {-a[i].first.first, 1});
      ra1[a[i].first.first - a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 1});
      y2[a[i].first.second].push_back({a[i].first.first, 1});
    } else {
      su1[a[i].first.first + a[i].first.second].push_back(
          {a[i].first.first, 1});
      ra2[a[i].first.first - a[i].first.second + maxn1].push_back(
          {a[i].first.first, 1});
      y2[a[i].first.second].push_back({a[i].first.first, 0});
    }
  }
  int ans = 1e18;
  for (int i = 0; i < maxn; ++i) {
    ans = min(ans, go(su1[i]));
    ans = min(ans, go(su2[i]));
    ans = min(ans, go(ra1[i]));
    ans = min(ans, go(ra2[i]));
    ans = min(ans, go1(x1[i]));
    ans = min(ans, go1(y2[i]));
  }
  if (ans > 1e15) {
    cout << "SAFE";
  } else {
    cout << ans;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int maxn = 4e5 + 5;
const int maxn1 = 2e5 + 3;
vector<pair<int, int>> su1[maxn];
vector<pair<int, int>> su2[maxn];
vector<pair<int, int>> ra1[maxn];
vector<pair<int, int>> ra2[maxn];
vector<pair<int, int>> x1[maxn];
vector<pair<int, int>> y2[maxn];
int go(vector<pair<int, int>> z) {
  if (z.size() <= 1)
    return 1e17;
  int ans = 1e17;
  vector<int> z0;
  vector<int> z1;
  int n = z.size();
  for (int i = 0; i < n; ++i) {
    if (z[i].second == 0) {
      z1.push_back(z[i].first);
    } else {
      z0.push_back(z[i].first);
    }
  }
  z1.push_back(1e17);
  sort(z1.begin(), z1.end());
  for (int i = 0; i < z0.size(); ++i) {
    ans = min(ans, (*lower_bound(z1.begin(), z1.end(), z0[i]) - z0[i]) * 10);
  }
  // if(ans<1e15) {cout<<z[0].first<<' '<<z[0].second<<endl;cout<<z[1].first<<'
  // '<<z[1].second<<endl;}
  return ans;
}
int go1(vector<pair<int, int>> z) {
  if (z.size() <= 1)
    return 1e17;
  int ans = 1e17;
  vector<int> z0;
  vector<int> z1;
  int n = z.size();
  for (int i = 0; i < n; ++i) {
    if (z[i].second == 0) {
      z0.push_back(z[i].first);
    } else {
      z1.push_back(z[i].first);
    }
  }
  z1.push_back(1e17);
  sort(z1.begin(), z1.end());
  for (int i = 0; i < z0.size(); ++i) {
    ans = min(ans, (*lower_bound(z1.begin(), z1.end(), z0[i]) - z0[i]) * 5);
  }
  // if(ans<1e15) {cout<<z[0].first<<' '<<z[0].second<<endl;cout<<z[1].first<<'
  // '<<z[1].second<<endl;}
  return ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<pair<int, int>, char> a[n];
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    a[i].first.first = x;
    a[i].first.second = y;
    cin >> a[i].second;
  }
  for (int i = 0; i < maxn; ++i) {
    su1[i].clear();
    su2[i].clear();
    ra1[i].clear();
    ra2[i].clear();
  }
  for (int i = 0; i < n; ++i) {
    if (a[i].second == 'U') {
      su1[a[i].first.first + a[i].first.second + maxn1].push_back(
          {a[i].first.first, 0});
      ra1[a[i].first.first - a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 0});
      x1[a[i].first.first].push_back({a[i].first.second, 0});
    } else if (a[i].second == 'D') {
      su2[a[i].first.first + a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 0});
      ra2[a[i].first.first - a[i].first.second + maxn1].push_back(
          {a[i].first.first, 0});
      x1[a[i].first.first].push_back({a[i].first.second, 1});
    } else if (a[i].second == 'L') {
      su2[a[i].first.first + a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 1});
      ra1[a[i].first.first - a[i].first.second + maxn1].push_back(
          {-a[i].first.first, 1});
      y2[a[i].first.second].push_back({a[i].first.first, 1});
    } else {
      su1[a[i].first.first + a[i].first.second + maxn1].push_back(
          {a[i].first.first, 1});
      ra2[a[i].first.first - a[i].first.second + maxn1].push_back(
          {a[i].first.first, 1});
      y2[a[i].first.second].push_back({a[i].first.first, 0});
    }
  }
  int ans = 1e18;
  for (int i = 0; i < maxn; ++i) {
    ans = min(ans, go(su1[i]));
    ans = min(ans, go(su2[i]));
    ans = min(ans, go(ra1[i]));
    ans = min(ans, go(ra2[i]));
    ans = min(ans, go1(x1[i]));
    ans = min(ans, go1(y2[i]));
  }
  if (ans > 1e15) {
    cout << "SAFE";
  } else {
    cout << ans;
  }
  return 0;
}