Problem p02605 (C++) — buggy submission, verdict: Memory Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, ans = inf;
  cin >> n;
  map<char, int> dir;
  dir['U'] = 0;
  dir['R'] = 1;
  dir['D'] = 2;
  dir['L'] = 3;
  vector<int> X(n, 0), Y(n, 0);
  vector<char> U(n);
  vector<vector<set<int>>> st(8, vector<set<int>>(400001));
  vector<vector<set<int>>> dr(4, vector<set<int>>(400001));
  rep(i, 8) rep(j, 400001) {
    st[i][j].insert(-inf);
    st[i][j].insert(inf);
  }
  rep(i, 4) rep(j, 400001) {
    dr[i][j].insert(-inf);
    dr[i][j].insert(inf);
  }
  rep(i, n) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    X[i] = x;
    Y[i] = y;
    U[i] = u;
    st[dir[u]][x + y].insert(x);
    st[dir[u] + 4][200000 + x - y].insert(x);
    if (dir[u] % 2)
      dr[dir[u]][y].insert(x);
    else
      dr[dir[u]][x].insert(y);
  }
  rep(i, n) {
    int p, p1, p2;
    if (U[i] == 'U') {
      p = *dr[2][X[i]].upper_bound(Y[i]);
      p1 = *(--st[1][X[i] + Y[i]].upper_bound(X[i]));
      p2 = *st[7][200000 + X[i] - Y[i]].upper_bound(X[i]);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(Y[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    if (U[i] == 'R') {
      p = *dr[3][Y[i]].upper_bound(X[i]);
      p1 = *st[6][200000 + X[i] - Y[i]].upper_bound(X[i]);
      p2 = *st[0][X[i] + Y[i]].upper_bound(X[i]);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(X[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    if (U[i] == 'D') {
      p = *(--dr[0][X[i]].upper_bound(Y[i]));
      p1 = *st[3][X[i] + Y[i]].upper_bound(X[i]);
      p2 = *(--st[5][200000 + X[i] - Y[i]].upper_bound(X[i]));
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(Y[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    if (U[i] == 'L') {
      p = *(--dr[1][Y[i]].upper_bound(X[i]));
      p1 = *(--st[2][X[i] + Y[i]].upper_bound(X[i]));
      p2 = *(--st[4][200000 + X[i] - Y[i]].upper_bound(X[i]));
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(X[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    // cout<<p<<" "<<p1<<" "<<p2<<endl;
  }
  if (ans == inf)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
  /*
  set<int> ww;
  rep(i,10) ww.insert(i*10);
  cout<<*(--ww.upper_bound(76))<<endl;
  */
}


Error: MLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, ans = inf;
  cin >> n;
  map<char, int> dir;
  dir['U'] = 0;
  dir['R'] = 1;
  dir['D'] = 2;
  dir['L'] = 3;
  vector<int> X(n, 0), Y(n, 0);
  vector<char> U(n);
  vector<vector<set<int>>> st(16, vector<set<int>>(400001));
  vector<vector<set<int>>> dr(4, vector<set<int>>(400001));
  rep(i, 16) rep(j, 400001) {
    st[i][j].insert(-inf);
    st[i][j].insert(inf);
  }
  rep(i, 4) rep(j, 400001) {
    dr[i][j].insert(-inf);
    dr[i][j].insert(inf);
  }
  rep(i, n) {
    int x, y;
    char u;
    cin >> x >> y >> u;
    X[i] = x;
    Y[i] = y;
    U[i] = u;
    st[dir[u]][x + y].insert(x);
    st[dir[u] + 4][200000 + x - y].insert(x);
    st[dir[u] + 8][x + y].insert(y);
    st[dir[u] + 12][200000 + x - y].insert(y);
    if (dir[u] % 2)
      dr[dir[u]][y].insert(x);
    else
      dr[dir[u]][x].insert(y);
  }
  rep(i, n) {
    int p, p1, p2;
    if (U[i] == 'U') {
      p = *dr[2][X[i]].upper_bound(Y[i]);
      p1 = *(st[1][X[i] + Y[i]].upper_bound(X[i])--);
      p2 = *st[7][200000 + X[i] - Y[i]].upper_bound(X[i]);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(Y[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    if (U[i] == 'R') {
      p = *dr[3][Y[i]].upper_bound(X[i]);
      p1 = *st[14][200000 + X[i] - Y[i]].upper_bound(Y[i]);
      p2 = *(st[8][X[i] + Y[i]].upper_bound(Y[i])--);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(X[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(Y[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(Y[i] - p2));
    }
    if (U[i] == 'D') {
      p = *(dr[0][X[i]].upper_bound(Y[i])--);
      p1 = *st[3][X[i] + Y[i]].upper_bound(X[i]);
      p2 = *(st[5][200000 + X[i] - Y[i]].upper_bound(X[i])--);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(Y[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(X[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(X[i] - p2));
    }
    if (U[i] == 'L') {
      p = *(dr[1][Y[i]].upper_bound(X[i])--);
      p1 = *st[10][X[i] + Y[i]].upper_bound(Y[i]);
      p2 = *(st[12][200000 + X[i] - Y[i]].upper_bound(Y[i])--);
      if (abs(p) != inf)
        ans = min(ans, 5 * abs(X[i] - p));
      if (abs(p1) != inf)
        ans = min(ans, 10 * abs(Y[i] - p1));
      if (abs(p2) != inf)
        ans = min(ans, 10 * abs(Y[i] - p2));
    }
  }
  if (ans == inf)
    cout << "SAFE" << endl;
  else
    cout << ans << endl;
}