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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define double long double
double EPS = 1e-12;

double g(double x, double A, double B, double C) {

  return A * x * x + B * x + C;
}
double f(double x, double A, double B) {
  double t = 2 * A * x + B;
  return sqrt(1 + t * t);
  // return 0.5*(t*sqrt(t*t+1)+log(abs(t+sqrt(1+t*t))))/(2.*A);
}
double integral(double a, double b, double A, double B) {
  double S = 1000;
  double d = (b - a) / S;
  double ans = 0;
  for (int i = 0; i < S; i++) {
    double aa = a + d * i;
    double bb = a + d * (i + 1);
    ans += d * (f(aa, A, B) + f((aa + bb) * 0.5, A, B) + f(bb, A, B)) / 3.0;
  }
  return ans;
  return f(b, A, B) - f(a, A, B);
}
vector<double> cp;
void crosspoint(double a, double b, double c) {
  if (fabs(a) < EPS) {
    cp.push_back(-c / b);
    return;
  }
  if (b * b - 4 * a * c < EPS)
    return;
  cp.push_back((-b - sqrt(b * b - 4 * a * c)) / (2 * a));
  cp.push_back((-b + sqrt(b * b - 4 * a * c)) / (2 * a));
}
double A[100] = {}, P[100] = {}, Q[100] = {};
double B[100], C[100] = {};
int main() {
  double W, H;
  int N;
  cin >> W >> H >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> P[i] >> Q[i];
    B[i] = -2.0 * P[i] * A[i];
    C[i] = A[i] * P[i] * P[i] + Q[i];
  }
  for (int i = 0; i < N; i++) {
    crosspoint(A[i], B[i], C[i]);
  }

  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      crosspoint(A[i] - A[j], B[i] - B[j], C[i] - C[j]);
  cp.push_back(0);
  cp.push_back(W);
  sort(cp.begin(), cp.end());
  double previous_x = 0;
  double ans = 0;
  for (int i = 0; i < cp.size(); i++) {
    if (cp[i] < -EPS || cp[i] > W + EPS)
      continue;
    double height = 0;
    int idx = -1;
    double a = previous_x;
    double b = cp[i];
    for (int j = 0; j < N; j++) {
      if (g(a + (b - a) * 0.1, A[j], B[j], C[j]) > height + EPS) {
        idx = j;
        height = g(a + (b - a) * 0.1, A[j], B[j], C[j]);
      }
    }
    if (idx >= 0) {
      // cout <<  integral(a,b,A[idx],B[idx]) << endl;
      ans += integral(a, b, A[idx], B[idx]);
    }
    previous_x = cp[i];
  }
  printf("%.15Lf\\n", ans);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define double long double
double EPS = 1e-12;

double g(double x, double A, double B, double C) {

  return A * x * x + B * x + C;
}
double f(double x, double A, double B) {
  double t = 2 * A * x + B;
  return sqrt(1 + t * t);
  // return 0.5*(t*sqrt(t*t+1)+log(abs(t+sqrt(1+t*t))))/(2.*A);
}
double integral(double a, double b, double A, double B) {
  double S = 100000;
  double d = (b - a) / S;
  double ans = 0;
  for (int i = 0; i < S; i++) {
    double aa = a + d * i;
    double bb = a + d * (i + 1);
    ans += d * (f(aa, A, B) + f((aa + bb) * 0.5, A, B) + f(bb, A, B)) / 3.0;
  }
  return ans;
  return f(b, A, B) - f(a, A, B);
}
vector<double> cp;
void crosspoint(double a, double b, double c) {
  if (fabs(a) < EPS) {
    cp.push_back(-c / b);
    return;
  }
  if (b * b - 4 * a * c < EPS)
    return;
  cp.push_back((-b - sqrt(b * b - 4 * a * c)) / (2 * a));
  cp.push_back((-b + sqrt(b * b - 4 * a * c)) / (2 * a));
}
double A[100] = {}, P[100] = {}, Q[100] = {};
double B[100], C[100] = {};
int main() {
  double W, H;
  int N;
  cin >> W >> H >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> P[i] >> Q[i];
    B[i] = -2.0 * P[i] * A[i];
    C[i] = A[i] * P[i] * P[i] + Q[i];
  }
  for (int i = 0; i < N; i++) {
    crosspoint(A[i], B[i], C[i]);
  }

  for (int i = 0; i < N; i++)
    for (int j = i + 1; j < N; j++)
      crosspoint(A[i] - A[j], B[i] - B[j], C[i] - C[j]);
  cp.push_back(0);
  cp.push_back(W);
  sort(cp.begin(), cp.end());
  double previous_x = 0;
  double ans = 0;
  for (int i = 0; i < cp.size(); i++) {
    if (cp[i] < -EPS || cp[i] > W + EPS)
      continue;
    double height = 0;
    int idx = -1;
    double a = previous_x;
    double b = cp[i];
    for (int j = 0; j < N; j++) {
      if (g(a + (b - a) * 0.5, A[j], B[j], C[j]) > height + EPS) {
        idx = j;
        height = g(a + (b - a) * 0.5, A[j], B[j], C[j]);
      }
    }
    if (idx >= 0) {
      // cout <<  integral(a,b,A[idx],B[idx]) << endl;
      ans += integral(a, b, A[idx], B[idx]);
    }
    previous_x = cp[i];
  }
  printf("%.15Lf\\n", ans);
}