Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n;
  scanf("%d", &n);
  std::vector<i64> x(n), y(n);
  std::vector<char> d(n);
  std::vector<i64> D, U, L, R;
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &x[i], &y[i]);
    cin >> d[i];

    if (d[i] == 'L')
      L.push_back(x[i]); // -1
    if (d[i] == 'R')
      R.push_back(x[i]); // +1
    if (d[i] == 'D')
      D.push_back(y[i]); // -1
    if (d[i] == 'U')
      U.push_back(y[i]); // +1
  }
  sort(begin(L), end(L));
  sort(begin(R), end(R));
  sort(begin(D), end(D));
  sort(begin(U), end(U));

  using ld = long double;

  auto calc = [&](ld t) {
    ld xma = -1e18, xmi = 1e18, yma = -1e18, ymi = 1e18;
    for (int i = 0; i < n; i++) {
      ld X = x[i], Y = y[i];

      if (d[i] == 'L')
        X -= t;
      if (d[i] == 'R')
        X += t;
      if (d[i] == 'D')
        Y -= t;
      if (d[i] == 'U')
        Y += t;
      xma = std::max(xma, X);
      xmi = std::min(xmi, X);
      yma = std::max(yma, Y);
      ymi = std::min(ymi, Y);
    }
    return (xma - xmi) * (yma - ymi);
  };

  auto latte = [&](i64 t, bool f) {
    i64 xma = -(1LL << 60), xmi = 1LL << 60;
    i64 yma = -(1LL << 60), ymi = 1LL << 60;
    for (int i = 0; i < n; i++) {
      i64 X = x[i], Y = y[i];

      if (d[i] == 'L')
        X -= t;
      if (d[i] == 'R')
        X += t;
      if (d[i] == 'D')
        Y -= t;
      if (d[i] == 'U')
        Y += t;
      xma = std::max(xma, X);
      xmi = std::min(xmi, X);
      yma = std::max(yma, Y);
      ymi = std::min(ymi, Y);
    }
    if (f)
      return xma - xmi;
    return yma - ymi;
  };

  auto getSegment = [&](int D, bool f) {
    i64 ok = 0, ng = 1LL << 60;
    for (int i = 0; i < 200 and std::abs(ok - ng) > 1; i++) {
      i64 mid = (ok + ng) >> 1;

      int d = latte(mid + 1, f) - latte(mid, f);
      if (d <= D)
        ok = mid;
      else
        ng = mid;
    }
    return ok + 1;
  };

  ld ans = 1e18;
  if (false and std::max(L.size(), R.size()) <= 1 or
      std::max(U.size(), D.size()) <= 1 or L.size() + R.size() == 2 or
      U.size() + D.size() == 2) {
    ld left = 0, right = 1e16;
    for (int i = 0; i < 200; i++) {
      ld ml = (left + left + right) / 3.;
      ld mr = (left + right + right) / 3.;

      if (calc(ml) <= calc(mr))
        right = mr;
      else
        left = ml;
    }
    ans = calc((left + right) / 2.);
  } else {
    std::vector<i64> vec = {0, 1LL << 60};
    for (int D = -4; D < 5; D++) {
      i64 xL = getSegment(D - 1, true), xR = getSegment(D, true);
      i64 yL = getSegment(D - 1, false), yR = getSegment(D, false);
      vec.push_back(xL);
      vec.push_back(xR);
      vec.push_back(yL);
      vec.push_back(yR);
    }
    sort(begin(vec), end(vec));
    vec.erase(unique(begin(vec), end(vec)), end(vec));

    for (int i = 0; i < (int)vec.size() - 1; i++) {
      ld left = vec[i], right = vec[i + 1];
      for (int i = 0; i < 175; i++) {
        ld ml = (left + left + right) / 3.;
        ld mr = (left + right + right) / 3.;

        ld L = calc(ml), R = calc(mr);
        ans = std::min({ans, L, R});
        if (L <= R)
          right = mr;
        else
          left = ml;
      }
    }
  }

  printf("%.12Lf\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n;
  scanf("%d", &n);
  std::vector<i64> x(n), y(n);
  std::vector<char> d(n);
  std::vector<i64> D, U, L, R;
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &x[i], &y[i]);
    cin >> d[i];

    if (d[i] == 'L')
      L.push_back(x[i]); // -1
    if (d[i] == 'R')
      R.push_back(x[i]); // +1
    if (d[i] == 'D')
      D.push_back(y[i]); // -1
    if (d[i] == 'U')
      U.push_back(y[i]); // +1
  }
  sort(begin(L), end(L));
  sort(begin(R), end(R));
  sort(begin(D), end(D));
  sort(begin(U), end(U));

  using ld = long double;

  auto calc = [&](ld t) {
    ld xma = -1e18, xmi = 1e18, yma = -1e18, ymi = 1e18;
    for (int i = 0; i < n; i++) {
      ld X = x[i], Y = y[i];

      if (d[i] == 'L')
        X -= t;
      if (d[i] == 'R')
        X += t;
      if (d[i] == 'D')
        Y -= t;
      if (d[i] == 'U')
        Y += t;
      xma = std::max(xma, X);
      xmi = std::min(xmi, X);
      yma = std::max(yma, Y);
      ymi = std::min(ymi, Y);
    }
    return (xma - xmi) * (yma - ymi);
  };

  auto latte = [&](i64 t, bool f) {
    i64 xma = -(1LL << 60), xmi = 1LL << 60;
    i64 yma = -(1LL << 60), ymi = 1LL << 60;
    for (int i = 0; i < n; i++) {
      i64 X = x[i], Y = y[i];

      if (d[i] == 'L')
        X -= t;
      if (d[i] == 'R')
        X += t;
      if (d[i] == 'D')
        Y -= t;
      if (d[i] == 'U')
        Y += t;
      xma = std::max(xma, X);
      xmi = std::min(xmi, X);
      yma = std::max(yma, Y);
      ymi = std::min(ymi, Y);
    }
    if (f)
      return xma - xmi;
    return yma - ymi;
  };

  auto getSegment = [&](int D, bool f) {
    i64 ok = 0, ng = 1LL << 60;
    for (int i = 0; i < 200 and std::abs(ok - ng) > 1; i++) {
      i64 mid = (ok + ng) >> 1;

      int d = latte(mid + 1, f) - latte(mid, f);
      if (d <= D)
        ok = mid;
      else
        ng = mid;
    }
    return ok + 1;
  };

  ld ans = 1e18;
  if (false and std::max(L.size(), R.size()) <= 1 or
      std::max(U.size(), D.size()) <= 1 or L.size() + R.size() == 2 or
      U.size() + D.size() == 2) {
    ld left = 0, right = 1e16;
    for (int i = 0; i < 200; i++) {
      ld ml = (left + left + right) / 3.;
      ld mr = (left + right + right) / 3.;

      if (calc(ml) <= calc(mr))
        right = mr;
      else
        left = ml;
    }
    ans = calc((left + right) / 2.);
  } else {
    std::vector<i64> vec = {0, 1LL << 60};
    for (int D = -3; D < 4; D++) {
      i64 xL = getSegment(D - 1, true), xR = getSegment(D, true);
      i64 yL = getSegment(D - 1, false), yR = getSegment(D, false);
      vec.push_back(xL);
      vec.push_back(xR);
      vec.push_back(yL);
      vec.push_back(yR);
    }
    sort(begin(vec), end(vec));
    vec.erase(unique(begin(vec), end(vec)), end(vec));

    for (int i = 0; i < (int)vec.size() - 1; i++) {
      ld left = vec[i], right = vec[i + 1];
      for (int i = 0; i < 175; i++) {
        ld ml = (left + left + right) / 3.;
        ld mr = (left + right + right) / 3.;

        ld L = calc(ml), R = calc(mr);
        ans = std::min({ans, L, R});
        if (L <= R)
          right = mr;
        else
          left = ml;
      }
    }
  }

  printf("%.12Lf\n", ans);
  return 0;
}