Problem p03004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<double> x, y, dx, dy;
  Solver(LL n) : n(n), x(n), y(n), dx(n), dy(n){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      char c;
      cin >> x[i] >> y[i] >> c;
      if (c == 'R')
        dx[i] = 1;
      if (c == 'L')
        dx[i] = -1;
      if (c == 'U')
        dy[i] = 1;
      if (c == 'D')
        dy[i] = -1;
    }
    double LB = 0;
    double UB = 1e9;
    auto xminmax = [&](double t) -> double {
      double xmin = LINF;
      double xmax = -LINF;
      for (int i = 0; i < n; ++i) {
        double xtmp = x[i] + dx[i] * t;
        xmin = min(xmin, xtmp);
        xmax = max(xmax, xtmp);
      }
      return xmax - xmin;
    };
    auto yminmax = [&](double t) -> double {
      double ymin = LINF;
      double ymax = -LINF;
      for (int i = 0; i < n; ++i) {
        double ytmp = y[i] + dy[i] * t;
        ymin = min(ymin, ytmp);
        ymax = max(ymax, ytmp);
      }
      return ymax - ymin;
    };
    auto area = [&](double t) -> double {
      double xmin = LINF;
      double xmax = -LINF;
      double ymin = LINF;
      double ymax = -LINF;
      for (int i = 0; i < n; ++i) {
        double xtmp = x[i] + dx[i] * t;
        xmin = min(xmin, xtmp);
        xmax = max(xmax, xtmp);
        double ytmp = y[i] + dy[i] * t;
        ymin = min(ymin, ytmp);
        ymax = max(ymax, ytmp);
      }
      return (xmax - xmin) * (ymax - ymin);
    };
    double t1 = ternary_search(LB, UB, xminmax);
    double t2 = ternary_search(LB, UB, yminmax);
    double ans = ternary_search(max(LB, min(t1, t2) - 10ll),
                                min(UB, max(t1, t2) + 10ll), area);
    DBG(t1, t2, ans);
    cout << area(ans) << endl;
  }

  double ternary_search(double lb, double ub, function<double(double)> f) {
    while (ub - lb > 1e-10) {
      vector<double> res(4);
      double m1 = lb + (ub - lb) / 3;
      double m2 = m1 + (ub - m1) / 2;
      res[0] = f(lb);
      res[1] = f(m1);
      res[2] = f(m2);
      res[3] = f(ub);
      int p = distance(res.begin(), min_element(res.begin(), res.end()));
      if (p == 0) {
        ub = m1;
      } else if (p == 1) {
        ub = m2;
      } else if (p == 2) {
        lb = m1;
      } else {
        lb = m2;
      }
    }
    return ub;
  }
};

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  std::cerr << std::fixed << std::setprecision(12);
  std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<double> x, y, dx, dy;
  Solver(LL n) : n(n), x(n), y(n), dx(n), dy(n){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      char c;
      cin >> x[i] >> y[i] >> c;
      if (c == 'R')
        dx[i] = 1;
      if (c == 'L')
        dx[i] = -1;
      if (c == 'U')
        dy[i] = 1;
      if (c == 'D')
        dy[i] = -1;
    }
    double LB = 0;
    double UB = 1e9;
    auto xminmax = [&](double t) -> double {
      double xmin = LINF;
      double xmax = -LINF;
      for (int i = 0; i < n; ++i) {
        double xtmp = x[i] + dx[i] * t;
        xmin = min(xmin, xtmp);
        xmax = max(xmax, xtmp);
      }
      return xmax - xmin;
    };
    auto yminmax = [&](double t) -> double {
      double ymin = LINF;
      double ymax = -LINF;
      for (int i = 0; i < n; ++i) {
        double ytmp = y[i] + dy[i] * t;
        ymin = min(ymin, ytmp);
        ymax = max(ymax, ytmp);
      }
      return ymax - ymin;
    };
    auto area = [&](double t) -> double {
      double xmin = LINF;
      double xmax = -LINF;
      double ymin = LINF;
      double ymax = -LINF;
      for (int i = 0; i < n; ++i) {
        double xtmp = x[i] + dx[i] * t;
        xmin = min(xmin, xtmp);
        xmax = max(xmax, xtmp);
        double ytmp = y[i] + dy[i] * t;
        ymin = min(ymin, ytmp);
        ymax = max(ymax, ytmp);
      }
      return (xmax - xmin) * (ymax - ymin);
    };
    double t1 = ternary_search(LB, UB, xminmax);
    double t2 = ternary_search(LB, UB, yminmax);
    double ans = ternary_search(max(LB, min(t1, t2) - 10ll),
                                min(UB, max(t1, t2) + 10ll), area);
    DBG(t1, t2, ans);
    cout << area(ans) << endl;
  }

  double ternary_search(double lb, double ub, function<double(double)> f) {
    for (int _ = 0; _ < 100; ++_) {
      vector<double> res(4);
      double m1 = lb + (ub - lb) / 3;
      double m2 = m1 + (ub - m1) / 2;
      res[0] = f(lb);
      res[1] = f(m1);
      res[2] = f(m2);
      res[3] = f(ub);
      int p = distance(res.begin(), min_element(res.begin(), res.end()));
      if (p == 0) {
        ub = m1;
      } else if (p == 1) {
        ub = m2;
      } else if (p == 2) {
        lb = m1;
      } else {
        lb = m2;
      }
    }
    return ub;
  }
};

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  std::cerr << std::fixed << std::setprecision(12);
  std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}