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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const long double PI = 3.14159265358979323846l;
const long double Moon_T = 41666.666666666667l;
const long double Moon_S = 1800.0000000000000l;
const long double Kaguya_S = 1900.00000000000l;
const long double Kaguya_T = 120.000000000000l;
const long double Dist = 380000.0000000000000l;
long double start, times;

struct Point {
  long double x, y, z;
};
long double SIN(long double r) { return sin(r * PI / 180.0l); }
long double COS(long double r) { return cos(r * PI / 180.0l); }
Point getpoint(long double r, long double t) {
  Point mn = Point{Dist * COS(r), Dist * SIN(r), 0.0l};
  Point Kgy = {mn.x - Kaguya_S * SIN(t / Kaguya_T * 360.0l), mn.y,
               mn.z + Kaguya_S * COS(t / Kaguya_T * 360.0l)};
  return Kgy;
}
long double dist(Point a, Point b) {
  long double a1 = (a.x - b.x) * (a.x - b.x);
  long double a2 = (a.y - b.y) * (a.y - b.y);
  long double a3 = (a.z - b.z) * (a.z - b.z);
  return sqrtl(a1 + a2 + a3);
}
long double dst(Point a, Point b) {
  long double L1 = 375000, R1 = 385000, c1, c2;
  long double A1 = dist(Point{0, 0, 0}, b);
  R1 = A1;
  long double Ax1 = b.x / A1;
  long double Ay1 = b.y / A1;
  long double Az1 = b.z / A1;
  long double minx = 1000000000.0l;
  for (int i = 0; i < 60; i++) {
    c1 = (L1 + L1 + R1) / 3.0l;
    c2 = (L1 + R1 + R1) / 3.0l;
    Point RR1 = {Ax1 * c1, Ay1 * c1, Az1 * c1};
    Point RR2 = {Ax1 * c2, Ay1 * c2, Az1 * c2};
    long double d1 = dist(a, RR1);
    long double d2 = dist(a, RR2);
    if (d1 < d2)
      R1 = c2;
    if (d1 > d2)
      L1 = c1;
    minx = min(minx, min(d1, d2));
  }
  return minx;
}
long double solve(long double t) {
  long double rr = (start / 360.0l + (t / Moon_T)) * 360.0l;
  Point PP1 = getpoint(rr, t);
  Point PP2 = Point{Dist * COS(rr), Dist * SIN(rr), 0.0l};
  long double ans = dst(PP2, PP1);
  return ans;
}
int main() {
  cin >> start >> times;
  long double cnt = 0.0l;
  for (long double i = 0.0l; i < times; i += 0.005l) {
    if (solve(i) < Moon_S)
      cnt += 0.005l;
  }
  printf("%.12Lf\\n", cnt);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const long double PI = 3.14159265358979323846l;
const long double Moon_T = 41666.666666666667l;
const long double Moon_S = 1800.0000000000000l;
const long double Kaguya_S = 1900.00000000000l;
const long double Kaguya_T = 120.000000000000l;
const long double Dist = 380000.0000000000000l;
long double start, times;

struct Point {
  long double x, y, z;
};
long double SIN(long double r) { return sin(r * PI / 180.0l); }
long double COS(long double r) { return cos(r * PI / 180.0l); }
Point getpoint(long double r, long double t) {
  Point mn = Point{Dist * COS(r), Dist * SIN(r), 0.0l};
  Point Kgy = {mn.x - Kaguya_S * SIN(t / Kaguya_T * 360.0l), mn.y,
               mn.z + Kaguya_S * COS(t / Kaguya_T * 360.0l)};
  return Kgy;
}
long double dist(Point a, Point b) {
  long double a1 = (a.x - b.x) * (a.x - b.x);
  long double a2 = (a.y - b.y) * (a.y - b.y);
  long double a3 = (a.z - b.z) * (a.z - b.z);
  return sqrtl(a1 + a2 + a3);
}
long double dst(Point a, Point b) {
  long double L1 = 375000, R1 = 385000, c1, c2;
  long double A1 = dist(Point{0, 0, 0}, b);
  R1 = A1;
  long double Ax1 = b.x / A1;
  long double Ay1 = b.y / A1;
  long double Az1 = b.z / A1;
  long double minx = 1000000000.0l;
  for (int i = 0; i < 60; i++) {
    c1 = (L1 + L1 + R1) / 3.0l;
    c2 = (L1 + R1 + R1) / 3.0l;
    Point RR1 = {Ax1 * c1, Ay1 * c1, Az1 * c1};
    Point RR2 = {Ax1 * c2, Ay1 * c2, Az1 * c2};
    long double d1 = dist(a, RR1);
    long double d2 = dist(a, RR2);
    if (d1 < d2)
      R1 = c2;
    if (d1 > d2)
      L1 = c1;
    minx = min(minx, min(d1, d2));
  }
  return minx;
}
long double solve(long double t) {
  long double rr = (start / 360.0l + (t / Moon_T)) * 360.0l;
  Point PP1 = getpoint(rr, t);
  Point PP2 = Point{Dist * COS(rr), Dist * SIN(rr), 0.0l};
  long double ans = dst(PP2, PP1);
  return ans;
}
int main() {
  cin >> start >> times;
  long double cnt = 0.0l;
  for (long double i = 0.0l; i < times; i += 0.001l) {
    if (solve(i) < Moon_S)
      cnt += 0.001l;
  }
  printf("%.12Lf\\n", cnt);
  return 0;
}