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

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll x[12345678], y[12345678];
signed main() {
  vector<vector<ll>> ULU(1234567), ULL(1234567), DRD(1234567), DRR(1234567);
  vector<vector<ll>> URU(1234567), URR(1234567), DLD(1234567), DLL(1234567);
  vector<vector<ll>> UDU(1234567), UDD(1234567), LRL(1234567), LRR(1234567);
  ll n, m, k;
  cin >> n;
  rep(i, n) {
    char u;
    cin >> x[i] >> y[i] >> u;
    if (u == 'U') {
      URU[x[i] + y[i]].push_back(x[i]);
      ULU[x[i] - y[i] + 200000].push_back(x[i]);
      UDU[x[i]].push_back(y[i]);
    }
    if (u == 'D') {
      DLD[x[i] + y[i]].push_back(x[i]);
      DRD[x[i] - y[i] + 200000].push_back(x[i]);
      UDD[x[i]].push_back(y[i]);
    }
    if (u == 'L') {
      DLL[x[i] + y[i]].push_back(x[i]);
      ULL[x[i] - y[i] + 200000].push_back(x[i]);
      LRL[y[i]].push_back(x[i]);
    }
    if (u == 'R') {
      URR[x[i] + y[i]].push_back(x[i]);
      DRR[x[i] - y[i] + 200000].push_back(x[i]);
      LRR[y[i]].push_back(x[i]);
    }
  }
  ll z = 0, cnt = INF;
  rep(i, 600005) { // UD
    sort(all(UDD[i]));
    rep(j, UDU[i].size()) {
      ll ok = -1, ng = UDD[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (UDD[i][mid] >= UDU[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == UDD[i].size()) {
        continue;
      }
      cnt = min((UDD[i][ng] - UDU[i][j]) * 5, cnt);
      z++;
    }
  }
  rep(i, 600005) { // LR
    sort(all(LRL[i]));
    rep(j, LRR[i].size()) {
      ll ok = -1, ng = LRL[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (LRL[i][mid] >= LRR[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == LRL[i].size()) {
        continue;
      }
      cnt = min((LRL[i][ng] - LRR[i][j]) * 5, cnt);
      z++;
    }
  }
  rep(i, 600005) { // UL
    sort(all(ULL[i]));
    rep(j, ULU[i].size()) {
      ll ok = -1, ng = ULL[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (ULL[i][mid] >= ULU[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == ULL[i].size()) {
        continue;
      }
      cnt = min((ULL[i][ng] - ULU[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 600005) { // DL
    sort(all(DLL[i]));
    rep(j, DLD[i].size()) {
      ll ok = -1, ng = DLL[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (DLL[i][mid] >= DLD[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == DLL[i].size()) {
        continue;
      }
      cnt = min((DLL[i][ng] - DLD[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 600005) { // UR
    sort(all(URU[i]));
    rep(j, URR[i].size()) {
      ll ok = -1, ng = URU[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (URU[i][mid] >= URR[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == URU[i].size()) {
        continue;
      }
      cnt = min((URU[i][ng] - URR[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 600005) { // DR
    sort(all(DRD[i]));
    rep(j, DRR[i].size()) {
      ll ok = -1, ng = DRD[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (DRD[i][mid] >= DRR[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == DRD[i].size()) {
        continue;
      }
      cnt = min((DRD[i][ng] - DRR[i][j]) * 10, cnt);
      z++;
    }
  }
  if (z == 0) {
    cout << "SAFE" << endl;
  } else {
    cout << cnt << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define rep2(i, x) for (ll i = 1; i <= x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

ll x[12345678], y[12345678];
ll usex[12345678], usey[12345678];
ll ix[12345678], ax[12345678];
ll iy[12345678], ay[12345678];
signed main() {
  rep(i, 1234567) {
    ix[i] = INF;
    iy[i] = INF;
  }
  vector<vector<ll>> ULU(12345678), ULL(12345678), DRD(12345678), DRR(12345678);
  vector<vector<ll>> URU(12345678), URR(12345678), DLD(12345678), DLL(12345678);
  ll n, m, k;
  cin >> n;
  rep(i, n) {
    char u;
    cin >> x[i] >> y[i] >> u;
    if (u == 'U') {
      URU[x[i] + y[i]].push_back(x[i]);
      ULU[x[i] - y[i] + 200000].push_back(x[i]);
      ix[x[i]] = min(ix[x[i]], y[i]);
    }
    if (u == 'D') {
      DLD[x[i] + y[i]].push_back(x[i]);
      DRD[x[i] - y[i] + 200000].push_back(x[i]);
      ax[x[i]] = max(ax[x[i]], y[i]);
    }
    if (u == 'L') {
      DLL[x[i] + y[i]].push_back(x[i]);
      ULL[x[i] - y[i] + 200000].push_back(x[i]);
      ay[y[i]] = max(ay[y[i]], x[i]);
    }
    if (u == 'R') {
      URR[x[i] + y[i]].push_back(x[i]);
      DRR[x[i] - y[i] + 200000].push_back(x[i]);
      iy[y[i]] = min(iy[y[i]], x[i]);
    }
  }
  ll z = 0, cnt = INF;
  rep(i, 6000005) {
    if (ix[i] < ax[i]) {
      cnt = min((ax[i] - ix[i]) * 5, cnt);
      z++;
    }
  }
  rep(i, 6000005) {
    if (iy[i] < ay[i]) {
      cnt = min((ay[i] - iy[i]) * 5, cnt);
      z++;
    }
  }
  rep(i, 6000005) {
    sort(all(ULL[i]));
    rep(j, ULU[i].size()) {
      ll ok = -1, ng = ULL[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (ULL[i][mid] >= ULU[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == ULL[i].size()) {
        continue;
      }
      cnt = min((ULL[i][ng] - ULU[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 6000005) {
    sort(all(DLL[i]));
    rep(j, DLD[i].size()) {
      ll ok = -1, ng = DLL[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (DLL[i][mid] >= DLD[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == DLL[i].size()) {
        continue;
      }
      cnt = min((DLL[i][ng] - DLD[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 6000005) {
    sort(all(URU[i]));
    rep(j, URR[i].size()) {
      ll ok = -1, ng = URU[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (URU[i][mid] >= URR[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == URU[i].size()) {
        continue;
      }
      cnt = min((URU[i][ng] - URR[i][j]) * 10, cnt);
      z++;
    }
  }
  rep(i, 6000005) {
    sort(all(DRD[i]));
    rep(j, DRR[i].size()) {
      ll ok = -1, ng = DRD[i].size(), mid = (ok + ng) / 2;
      while (ng - ok > 1) {
        if (DRD[i][mid] >= DRR[i][j]) {
          ng = mid;
        } else {
          ok = mid;
        }
        mid = (ok + ng) / 2;
      }
      if (ng == DRD[i].size()) {
        continue;
      }
      cnt = min((DRD[i][ng] - DRR[i][j]) * 10, cnt);
      z++;
    }
  }
  if (z == 0) {
    cout << "SAFE" << endl;
  } else {
    cout << cnt << endl;
  }
  return 0;
}