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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

typedef complex<double> P;
typedef vector<P> G;
#define here(g, i) g[i]
#define next(g, i) g[(i + 1) % g.size()]
#define prev(g, i) g[(i - 1 + g.size()) % g.size()]
const double EPS = 1e-10;
const double INF = 1e12;

// ?????????????????????
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

struct L {
  P a, b, v;
  L() {}
  L(P _a, P _b) : a(_a), b(_b), v(b - a) {}
  L(double _ax, double _ay, double _bx, double _by)
      : L(P(_ax, _ay), P(_bx, _by)) {}
};

bool cmp_x(const P &p, const P &q) {
  if (p.real() != q.real())
    return p.real() < q.real();
  return p.imag() < q.imag();
}

double cross(P a, P b) { return imag(conj(a) * b); }

double dot(P a, P b) { return real(conj(a) * b); }

G convex_hull(vector<P> ps) {
  int n = ps.size();
  sort(all(ps), cmp_x);
  G qs(n * 2);
  int k = 0;
  rep(i, n) {
    while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = ps[i];
  }
  for (int i = n - 2, t = k; i >= 0; i--) {
    while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = ps[i];
  }
  qs.resize(k - 1);
  return qs;
}

// ????????????p, q??????????§?(q->p??????????????£)
double thetaPP(P p, P q) {
  int sgn = cross(q, p) > 0 ? +1 : -1;
  return sgn * acos(dot(p, q) / abs(p) / abs(q));
}

// l???x?????¨???????§?
double thetaL(L l) { return thetaPP(l.v, P(1, 0)); }

// q???????????????p???theta?????¢
P rotP(P p, P q, double theta) {
  p -= q;
  double x = p.real(), y = p.imag();
  p = P(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta));
  p += q;
  return p;
}

G rotG(const G &g, P p, double theta) {
  G h;
  rep(i, g.size()) { h.emplace_back(rotP(g[i], p, theta)); }
  return h;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N;
  cin >> N;
  vector<P> corners; // box???????????????
  corners.emplace_back(0, 0);
  rep(i, N - 1) {
    int j, k;
    cin >> j >> k;
    corners.emplace_back(corners[j] + P(dx[k], dy[k]));
  }

  set<pair<double, double>> sp; // ?????¢??????????????????
  rep(i, N) {
    sp.insert(make_pair(corners[i].real() + 0, corners[i].imag() + 0));
    sp.insert(make_pair(corners[i].real() + 1, corners[i].imag() + 0));
    sp.insert(make_pair(corners[i].real() + 0, corners[i].imag() + 1));
    sp.insert(make_pair(corners[i].real() + 1, corners[i].imag() + 1));
  }
  vector<P> vp;
  for (auto pii : sp) {
    vp.emplace_back(pii.first, pii.second);
  }

  G g = convex_hull(vp);

  // rep(i, g.size()) {
  //     cerr << g[i].real() << " " << g[i].imag() << endl;
  // }

  double ans = INF;
  rep(i, g.size()) { // g.size() = O(sqrt(N))
    L bottom(here(g, i), next(g, i));
    double theta = thetaL(bottom); // theta: ????????¨x?????????????§?
    // cerr << bottom.a.real() << " " << bottom.a.imag() << " " <<
    // bottom.b.real() << " " << bottom.b.imag() << " " << theta << endl;
    G g_rotated = rotG(g, here(g, i), -theta);
    double ma_x = -INF, mi_x = INF;
    double ma_y = -INF, mi_y = INF;
    rep(j, g_rotated.size()) {
      ma_x = max(ma_x, here(g_rotated, j).real());
      mi_x = min(mi_x, here(g_rotated, j).real());
      ma_y = max(ma_y, here(g_rotated, j).imag());
      mi_y = min(mi_y, here(g_rotated, j).imag());
    }
    ans = min(ans, (ma_x - mi_x) * (ma_y - mi_y));
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()

typedef complex<double> P;
typedef vector<P> G;
#define here(g, i) g[i]
#define next(g, i) g[(i + 1) % g.size()]
#define prev(g, i) g[(i - 1 + g.size()) % g.size()]
const double EPS = 1e-10;
const double INF = 1e12;

// ?????????????????????
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

struct L {
  P a, b, v;
  L() {}
  L(P _a, P _b) : a(_a), b(_b), v(b - a) {}
  L(double _ax, double _ay, double _bx, double _by)
      : L(P(_ax, _ay), P(_bx, _by)) {}
};

bool cmp_x(const P &p, const P &q) {
  if (p.real() != q.real())
    return p.real() < q.real();
  return p.imag() < q.imag();
}

double cross(P a, P b) { return imag(conj(a) * b); }

double dot(P a, P b) { return real(conj(a) * b); }

G convex_hull(vector<P> ps) {
  int n = ps.size();
  sort(all(ps), cmp_x);
  G qs(n * 2);
  int k = 0;
  rep(i, n) {
    while (k > 1 && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0)
      k--;
    qs[k++] = ps[i];
  }
  for (int i = n - 2, t = k; i >= 0; i--) {
    while (k > t && cross(qs[k - 1] - qs[k - 2], ps[i] - qs[k - 1]) < 0)
      k--;
    qs[k++] = ps[i];
  }
  qs.resize(k - 1);
  return qs;
}

// ????????????p, q??????????§?(q->p??????????????£)
double thetaPP(P p, P q) {
  int sgn = cross(q, p) > 0 ? +1 : -1;
  return sgn * acos(dot(p, q) / abs(p) / abs(q));
}

// l???x?????¨???????§?
double thetaL(L l) { return thetaPP(l.v, P(1, 0)); }

// q???????????????p???theta?????¢
P rotP(P p, P q, double theta) {
  p -= q;
  double x = p.real(), y = p.imag();
  p = P(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta));
  p += q;
  return p;
}

G rotG(const G &g, P p, double theta) {
  G h;
  rep(i, g.size()) { h.emplace_back(rotP(g[i], p, theta)); }
  return h;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N;
  cin >> N;
  vector<P> corners; // box???????????????
  corners.emplace_back(0, 0);
  rep(i, N - 1) {
    int j, k;
    cin >> j >> k;
    corners.emplace_back(corners[j] + P(dx[k], dy[k]));
  }

  set<pair<double, double>> sp; // ?????¢??????????????????
  rep(i, N) {
    sp.insert(make_pair(corners[i].real() + 0, corners[i].imag() + 0));
    sp.insert(make_pair(corners[i].real() + 1, corners[i].imag() + 0));
    sp.insert(make_pair(corners[i].real() + 0, corners[i].imag() + 1));
    sp.insert(make_pair(corners[i].real() + 1, corners[i].imag() + 1));
  }
  vector<P> vp;
  for (auto pii : sp) {
    vp.emplace_back(pii.first, pii.second);
  }

  G g = convex_hull(vp);

  // rep(i, g.size()) {
  //     cerr << g[i].real() << " " << g[i].imag() << endl;
  // }

  double ans = INF;
  rep(i, g.size()) { // g.size() = O(sqrt(N))
    L bottom(here(g, i), next(g, i));
    double theta = thetaL(bottom); // theta: ????????¨x?????????????§?
    // cerr << bottom.a.real() << " " << bottom.a.imag() << " " <<
    // bottom.b.real() << " " << bottom.b.imag() << " " << theta << endl;
    G g_rotated = rotG(g, here(g, i), -theta);
    double ma_x = -INF, mi_x = INF;
    double ma_y = -INF, mi_y = INF;
    rep(j, g_rotated.size()) {
      ma_x = max(ma_x, here(g_rotated, j).real());
      mi_x = min(mi_x, here(g_rotated, j).real());
      ma_y = max(ma_y, here(g_rotated, j).imag());
      mi_y = min(mi_y, here(g_rotated, j).imag());
    }
    ans = min(ans, (ma_x - mi_x) * (ma_y - mi_y));
  }

  cout << ans << endl;
}