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

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P; // Point
typedef pair<P, P> L;      // Line, Segment

const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
}; // namespace std
double dot(P a, P b) { return a.real() * b.real() + a.imag() * b.imag(); }
double cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }
double area(vector<P> v) {
  double sum = 0.0;
  int n = v.size();
  for (int i = 0; i < n; i++)
    sum += (real(v[i]) - real(v[(i + 1) % n])) *
           (imag(v[i]) + imag(v[(i + 1) % n]));
  return fabs(sum) / 2;
};
P rotate(P p, double theta) {
  theta = theta * M_PI / 180.0;
  double x = real(p) * cos(theta) - imag(p) * sin(theta);
  double y = real(p) * sin(theta) + imag(p) * cos(theta);
  return P(x, y);
}
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // テ・ツ渉催ヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if (cross(b, c) < -EPS)
    return -1; // テヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if (dot(b, c) < -EPS)
    return 2; // c -- a -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  if (norm(b) < norm(c))
    return -2; // a -- b -- c テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  return 0; // a -- c -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
}
P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  else if (fabs(A) >= EPS)
    return m.first + B / A * (m.second - m.first);
}
int N;
typedef pair<bool, double> PR;
double getA(double pi) {
  if (pi + EPS > M_PI)
    return -M_PI;
  return pi;
}
int main() {
  while (~scanf("%d", &N) && N) {
    P v = P(0, 1.0 / sqrt(3));
    set<P> sp;
    vector<pair<PR, L>> ss;
    for (int i = 0; i < N; i++) {
      P v1 = v;
      for (int i = 0; i < 3; i++) {
        P v2 = rotate(v1, 120.0);
        // cout << " " << v1 << " "<< v2 << endl;
        ss.push_back(make_pair(PR(0, getA(arg(v1))), L(v1, v2)));
        ss.push_back(make_pair(PR(1, getA(arg(v2))), L(v2, v1)));
        v1 = v2;
      }
      v = rotate(v, 360.0 / (double)N);
    }
    sort(ss.begin(), ss.end());
    int cnt = 0;
    for (int i = 0; i < (int)ss.size() - 1 - cnt; i++) {
      // cout << ss[i].first.second << endl;
      if (ss[i].first.first == ss[i + 1].first.first &&
          abs(ss[i].first.second - ss[i + 1].first.second) < EPS) {
        cnt++;
        for (int j = i + 1; j < (int)ss.size() - 1; j++)
          ss[j] = ss[j + 1];
        i--;
      }
    }
    ss.erase(ss.end() - cnt, ss.end());
    int n = ss.size();
    vector<P> p;
    for (int i = 0; i < n / 2; i++) {
      L s1 = ss[i].second;
      L s2 = ss[i + 1].second;
      L s3 = ss[(i + 1 + n / 2 == n) ? (n / 2) : (i + 1 + n / 2)].second;
      //  cout <<" x" << s1.first << " " << s1.second << endl;
      // cout << " y"<< s3.first <<" "<<s3.second << endl;
      p.push_back(s1.first);
      P mp = crossPoint(s1, s3);
      if (abs(mp - s2.first) > EPS)
        p.push_back(mp);
    }
    printf("%.9lf\\n", area(p));
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P; // Point
typedef pair<P, P> L;      // Line, Segment

const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);

namespace std {
bool operator<(const P &a, const P &b) {
  return fabs(real(a) - real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
}
}; // namespace std
double dot(P a, P b) { return a.real() * b.real() + a.imag() * b.imag(); }
double cross(P a, P b) { return a.real() * b.imag() - a.imag() * b.real(); }
double area(vector<P> v) {
  double sum = 0.0;
  int n = v.size();
  for (int i = 0; i < n; i++)
    sum += (real(v[i]) - real(v[(i + 1) % n])) *
           (imag(v[i]) + imag(v[(i + 1) % n]));
  return fabs(sum) / 2;
};
P rotate(P p, double theta) {
  theta = theta * M_PI / 180.0;
  double x = real(p) * cos(theta) - imag(p) * sin(theta);
  double y = real(p) * sin(theta) + imag(p) * cos(theta);
  return P(x, y);
}
int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1; // テ・ツ渉催ヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if (cross(b, c) < -EPS)
    return -1; // テヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if (dot(b, c) < -EPS)
    return 2; // c -- a -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  if (norm(b) < norm(c))
    return -2; // a -- b -- c テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  return 0; // a -- c -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
}
P crossPoint(L l, L m) {
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if (fabs(A) < EPS && fabs(B) < EPS)
    return m.first;
  else if (fabs(A) >= EPS)
    return m.first + B / A * (m.second - m.first);
}
int N;
double getA(P v) { return atan2(v.imag(), v.real()); }
int main() {
  while (~scanf("%d", &N) && N) {
    P v = P(0, 1.0 / sqrt(3));
    set<P> sp;
    vector<pair<double, L>> ss;
    for (int i = 0; i < N; i++) {
      P v1 = v;
      for (int i = 0; i < 3; i++) {
        P v2 = rotate(v1, 120.0);
        // cout << " " << v1 << " "<< v2 << endl;
        ss.push_back(make_pair(arg(v1), L(v1, v2)));
        v1 = v2;
      }
      v = rotate(v, 360.0 / (double)N);
    }
    sort(ss.begin(), ss.end());
    int cnt = 0;
    for (int i = 0; i < (int)ss.size() - 1 - cnt; i++) {
      if (abs(ss[i].first - ss[i + 1].first) < EPS) {
        cnt++;
        for (int j = i + 1; j < (int)ss.size() - 1; j++)
          ss[j] = ss[j + 1];
        i--;
      }
    }
    ss.erase(ss.end() - cnt, ss.end());
    int n = ss.size();
    vector<P> p;
    for (int i = 0; i < n; i++) {
      L s1 = ss[i].second;
      L s2 = ss[(i + 1) % n].second;
      //      cout <<" x" << s1.first<< endl;
      p.push_back(s1.first);
      P mp = P(1e9, 1e9);
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        P x = crossPoint(s1, ss[j].second);
        if (abs(x - s1.first) > EPS && abs(s1.first - mp) > abs(s1.first - x))
          mp = x;
      }
      if (abs(mp - s2.first) > EPS)
        p.push_back(mp);
    }
    printf("%.9lf\\n", area(p));
  }
}