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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = 1e18;

const int N = 2 * (int)1e5;
ll xs[N];
ll ys[N];
int dir[N];

void rotate(int n) {
  for (int i = 0; i < n; ++i) {
    dir[i] = (dir[i] + 1) % 4;
    ll x = xs[i];
    xs[i] = ys[i];
    ys[i] = -x;
  }
}

// Check for those colliding diagonally
ll check(int n) {
  vector<pair<pair<ll, ll>, int>> ord(n);
  for (int i = 0; i < n; ++i) {
    ord[i] = {{xs[i] + ys[i], xs[i]}, dir[i]};
  }
  sort(ord.begin(), ord.end());

  ll res = INF;
  for (int i = 0; i < n;) {
    ll min_ri = -INF;
    ll min_do = -INF;
    int j = i;
    while (j < n && ord[j].first.first == ord[i].first.first) {
      ll x = ord[j].first.second;
      int d = ord[j].second;
      if (d == 0)
        min_ri = x;
      else if (d == 1)
        min_do = x;
      else if (d == 2) {
        if (min_do > -INF)
          res = min(res, 10 * (x - min_do));
      } else {
        if (min_ri > -INF)
          res = min(res, 10 * (x - min_ri));
      }
      ++j;
    }
    i = j;
  }
  return res;
}

// Check for those colliding head on
ll check2(int n) {
  vector<pair<pair<ll, ll>, int>> ord(n);
  for (int i = 0; i < n; ++i) {
    ord[i] = {{xs[i], -ys[i]}, dir[i]};
  }
  sort(ord.begin(), ord.end());

  ll res = INF;
  for (int i = 0; i < n;) {
    ll max_do = INF;
    int j = i;
    while (j < n && ord[j].first.first == ord[i].first.first) {
      ll y = -ord[j].first.second;
      int d = ord[j].second;
      if (d == 1)
        max_do = y;
      else if (d == 3) {
        if (max_do < INF)
          res = min(res, 5 * (max_do - y));
      }
      ++j;
    }
    i = j;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    char d;
    cin >> xs[i] >> ys[i] >> d;

    if (d == 'R')
      dir[i] = 0;
    else if (d == 'D')
      dir[i] = 1;
    else if (d == 'L')
      dir[i] = 2;
    else
      dir[i] = 3;
  }

  // If they go RIGHT - UP, they collide IFF they are on the same diagonal c =
  // x+y, and the UP-going plane is to the right of the other
  // -||- for other pairs of directions.

  ll res = INF;
  for (int i = 0; i < 2; ++i) {
    res = min(res, check(n));
    res = min(res, check2(n));
    rotate(n);
  }

  if (res >= INF / 2)
    cout << "SAFE\\n";
  else
    cout << res << '\\n';
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const ll INF = 1e18;

const int N = 2 * (int)1e5;
ll xs[N];
ll ys[N];
int dir[N];

void rotate(int n) {
  for (int i = 0; i < n; ++i) {
    dir[i] = (dir[i] + 1) % 4;
    ll x = xs[i];
    xs[i] = ys[i];
    ys[i] = -x;
  }
}

// Check for those colliding diagonally
ll check(int n) {
  vector<pair<pair<ll, ll>, int>> ord(n);
  for (int i = 0; i < n; ++i) {
    ord[i] = {{xs[i] + ys[i], xs[i]}, dir[i]};
  }
  sort(ord.begin(), ord.end());

  ll res = INF;
  for (int i = 0; i < n; ++i) {
    ll min_ri = -INF;
    ll min_do = -INF;
    int j = i;
    while (j < n && ord[j].first.first == ord[i].first.first) {
      ll x = ord[j].first.second;
      int d = ord[j].second;
      if (d == 0)
        min_ri = x;
      else if (d == 1)
        min_do = x;
      else if (d == 2) {
        if (min_do > -INF)
          res = min(res, 10 * (x - min_do));
      } else {
        if (min_ri > -INF)
          res = min(res, 10 * (x - min_ri));
      }
      ++j;
    }
  }
  return res;
}

// Check for those colliding head on
ll check2(int n) {
  vector<pair<pair<ll, ll>, int>> ord(n);
  for (int i = 0; i < n; ++i) {
    ord[i] = {{xs[i], -ys[i]}, dir[i]};
  }
  sort(ord.begin(), ord.end());

  ll res = INF;
  for (int i = 0; i < n; ++i) {
    ll max_do = INF;
    int j = i;
    while (j < n && ord[j].first.first == ord[i].first.first) {
      ll y = -ord[j].first.second;
      int d = ord[j].second;
      if (d == 1)
        max_do = y;
      else if (d == 3) {
        if (max_do < INF)
          res = min(res, 5 * (max_do - y));
      }
      ++j;
    }
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    char d;
    cin >> xs[i] >> ys[i] >> d;

    if (d == 'R')
      dir[i] = 0;
    else if (d == 'D')
      dir[i] = 1;
    else if (d == 'L')
      dir[i] = 2;
    else
      dir[i] = 3;
  }

  // If they go RIGHT - UP, they collide IFF they are on the same diagonal c =
  // x+y, and the UP-going plane is to the right of the other
  // -||- for other pairs of directions.

  ll res = INF;
  for (int i = 0; i < 2; ++i) {
    res = min(res, check(n));
    res = min(res, check2(n));
    rotate(n);
  }

  if (res >= INF / 2)
    cout << "SAFE\\n";
  else
    cout << res << '\\n';
}