Language: C++

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())

template <class F>
double integrate(F f, double a, double b, double eps = 1e-8) { 
  const double C = asin(1.0), T = 10.0; 
  function<double (double)> x, g, H; // g = dx/dt
  if (!isinf(a) && !isinf(b)) { // [a, b]
    x = [&](double t) { return (b+a)/2 + (b-a)/2*tanh(C*sinh(t)); };
    g = [&](double t) { return C*(b-a)/2*cosh(t)/pow(cosh(C*sinh(t)), 2); };
  } else if (!isinf(a)) {      // [a, infty]
    x = [&](double t) { return a + exp(C*sinh(t)); };
    g = [&](double t) { return C*cosh(t)*exp(C*sinh(t)); };
  } else if (!isinf(b)) {      // [-infty, b]
    x = [&](double t) { return b - exp(C*sinh(-t)); };
    g = [&](double t) { return C*cosh(t)*exp(C*sinh(-t)); };
  } else {                     // [-infty, infty]
    x = [&](double t) { return sinh(C*sinh(t)); };
    g = [&](double t) { return C*cosh(t)*cosh(C*sinh(t)); };
  }
  for (double S = 0, I, h = 0.01/C; ; S = I, h /= 2) {
    I = 0;
    for (double t = 0; t < T; t > 0 ? t = -t : t = -t+h) {
      double dI = f(x(t)) * g(t) * h;
      if (!isnan(dI)) I += dI;
    }
    if (fabs(I - S) <= fabs(I) * eps) return I;
  }
}

int main() {
  for (int n; scanf("%d", &n); ) {
    if (n == 0) break;
    double r; 
    scanf("%lf\n", &r);

    vector<pair<double,double>> p(n);
    for (int i = 0; i < n; ++i) 
      scanf("%lf %lf", &p[i].fst, &p[i].snd);
    function<double (double)> f = [&](double x) {
      if (x < p[0].fst) return 0.0;
      if (x >= p.back().fst) return 0.0;
      for (int i = 0; i+1 < n; ++i) 
        if (x < p[i+1].fst) 
          return p[i].snd + (p[i+1].snd-p[i].snd)/(p[i+1].fst-p[i].fst)*(x-p[i].fst);
    };
    function<double (double)> g = [&](double x) {
      //return exp(-x*x);
      return f(x) * f(x + r);
    };
    vector<double> pts;
    pts.push_back(-1.0/0.0);
    for (int i = 0; i < p.size(); ++i) {
      pts.push_back(p[i].fst);
      pts.push_back(p[i].fst + r);
    }
    pts.push_back(1.0/0.0);
    sort(all(pts));
    pts.erase(unique(all(pts)), pts.end());
    double I = 0;
    for (int i = 0; i+1 < pts.size(); ++i) 
      I += integrate(g, pts[i], pts[i+1], 1e-7);
    printf("%.12f\n", I);
  }
}

Compiler output:
a.cc: In function 'double integrate(F, double, double, double)':
a.cc:15:20: error: there are no arguments to 'asin' that depend on a template parameter, so a declaration of 'asin' must be available [-fpermissive]
   15 |   const double C = asin(1.0), T = 10.0;
      |                    ^~~~
a.cc:15:20: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:17:8: error: there are no arguments to 'isinf' that depend on a template parameter, so a declaration of 'isinf' must be available [-fpermissive]
   17 |   if (!isinf(a) && !isinf(b)) { // [a, b]
      |        ^~~~~
a.cc:17:21: error: there are no arguments to 'isinf' that depend on a template parameter, so a declaration of 'isinf' must be available [-fpermissive]
   17 |   if (!isinf(a) && !isinf(b)) { // [a, b]
      |                     ^~~~~
a.cc: In lambda function:
a.cc:18:57: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   18 |     x = [&](double t) { return (b+a)/2 + (b-a)/2*tanh(C*sinh(t)); };
      |                                                         ^~~~
a.cc: In lambda function:
a.cc:19:42: error: there are no arguments to 'cosh' that depend on a template parameter, so a declaration of 'cosh' must be available [-fpermissive]
   19 |     g = [&](double t) { return C*(b-a)/2*cosh(t)/pow(cosh(C*sinh(t)), 2); };
      |                                          ^~~~
a.cc:19:61: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   19 |     g = [&](double t) { return C*(b-a)/2*cosh(t)/pow(cosh(C*sinh(t)), 2); };
      |                                                             ^~~~
a.cc: In function 'double integrate(F, double, double, double)':
a.cc:20:15: error: there are no arguments to 'isinf' that depend on a template parameter, so a declaration of 'isinf' must be available [-fpermissive]
   20 |   } else if (!isinf(a)) {      // [a, infty]
      |               ^~~~~
a.cc: In lambda function:
a.cc:21:42: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   21 |     x = [&](double t) { return a + exp(C*sinh(t)); };
      |                                          ^~~~
a.cc: In lambda function:
a.cc:22:34: error: there are no arguments to 'cosh' that depend on a template parameter, so a declaration of 'cosh' must be available [-fpermissive]
   22 |     g = [&](double t) { return C*cosh(t)*exp(C*sinh(t)); };
      |                                  ^~~~
a.cc:22:48: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   22 |     g = [&](double t) { return C*cosh(t)*exp(C*sinh(t)); };
      |                                                ^~~~
a.cc: In function 'double integrate(F, double, double, double)':
a.cc:23:15: error: there are no arguments to 'isinf' that depend on a template parameter, so a declaration of 'isinf' must be available [-fpermissive]
   23 |   } else if (!isinf(b)) {      // [-infty, b]
      |               ^~~~~
a.cc: In lambda function:
a.cc:24:42: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   24 |     x = [&](double t) { return b - exp(C*sinh(-t)); };
      |                                          ^~~~
a.cc: In lambda function:
a.cc:25:34: error: there are no arguments to 'cosh' that depend on a template parameter, so a declaration of 'cosh' must be available [-fpermissive]
   25 |     g = [&](double t) { return C*cosh(t)*exp(C*sinh(-t)); };
      |                                  ^~~~
a.cc:25:48: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   25 |     g = [&](double t) { return C*cosh(t)*exp(C*sinh(-t)); };
      |                                                ^~~~
a.cc: In lambda function:
a.cc:27:39: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   27 |     x = [&](double t) { return sinh(C*sinh(t)); };
      |                                       ^~~~
a.cc: In lambda function:
a.cc:28:34: error: there are no arguments to 'cosh' that depend on a template parameter, so a declaration of 'cosh' must be available [-fpermissive]
   28 |     g = [&](double t) { return C*cosh(t)*cosh(C*sinh(t)); };
      |                                  ^~~~
a.cc:28:49: error: there are no arguments to 'sinh' that depend on a template parameter, so a declaration of 'sinh' must be available [-fpermissive]
   28 |     g = [&](double t) { return C*cosh(t)*cosh(C*sinh(t)); };
      |                                                 ^~~~
a.cc: In function 'double integrate(F, double, double, double)':
a.cc:34:12: error: there are no arguments to 'isnan' that depend on a template parameter, so a declaration of 'isnan' must be available [-fpermissive]
   34 |       if (!isnan(dI)) I += dI;
      |            ^~~~~
a.cc:36:9: error: there are no arguments to 'fabs' that depend on a template parameter, so a declaration of 'fabs' must be available [-fpermissive]
   36 |     if (fabs(I - S) <= fabs(I) * eps) return I;
      |         ^~~~
a.cc:36:24: error: there are no arguments to 'fabs' that depend on a template parameter, so a declaration of 'fabs' must be available [-fpermissive]
   36 |     if (fabs(I - S) <= fabs(I) * eps) return I;
      |                        ^~~~
a.cc: In instantiation of 'double integrate(F, double, double, double) [with F = std::function<double(double)>]':
a.cc:71:21:   required from here
   71 |       I += integrate(g, pts[i], pts[i+1], 1e-7);
      |            ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:15:24: error: 'asin' was not declared in this scope
   15 |   const double C = asin(1.0), T = 10.0;
      |                    ~~~~^~~~~
a.cc:17:13: error: 'isinf' was not declared in this scope
   17 |   if (!isinf(a) && !isinf(b)) { // [a, b]
      |        ~~~~~^~~
a.cc:17:26: error: 'isinf' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
   17 |   if (!isinf(a) && !isinf(b)) { // [a, b]
      |                     ~~~~~^~~
a.cc:17:13: note: 'isinf' declared here, later in the translation unit
   17 |   if (!isinf(a) && !isinf(b)) { // [a, b]
      |        ~~~~~^~~
a.cc:18:61: error: 'sinh' was not declared in this scope
   18 |     x = [&](double t) { return (b+a)/2 + (b-a)/2*tanh(C*sinh(t)); };
      |                                                         ~~~~^~~
a.cc:18:54: error: 'tanh' was not declared in this scope
   18 |     x = [&](double t) { return (b+a)/2 + (b-a)/2*tanh(C*sinh(t)); };
      |                                                  ~~~~^~~~~~~~~~~
a.cc:18:7: error: no match for 'operator=' (operand types are 'std::function<double(double)>' and 'integrate<std::function<double(double)> >(std::function<double(double)>, double, double, double)::<lambda(double)>')
   18 |     x = [&](double t) { return (b+a)/2 + (b-a)/2*tanh(C*sinh(t)); };
      |     ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/functional:59,
                 from a.cc:5:
/usr/include/c++/14/bits/std_function.h:531:9: note: candidate: 'template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_Callable<_Functor>, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Res = double; _ArgTypes = {double}]'
  531 |         operator=(_Functor&& __f)
      |         ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:531:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits: In substitution of 'template<bool _Cond, class _Tp> using std::__enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = std::function<double(double)>&]':
/usr/include/c++/14/bits/std_function.h:353:8:   required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using std::function<_Res(_ArgTypes ...)>::_Requires = std::__enable_if_t<((bool)_Cond::value), _Tp> [with _Cond = std::function<double(double)>::_Callable<integrate<std::function<double(double)> >(std::function<double(double)>, double, double, double)::<lambda(double)>, integrate<std::function<double(double)> >(std::function<double(double)>, double, double, double)::<lambda(double)>, std::__invoke_result<integrate<std::function<double(double)> >(std::function<double(double)>, double, double, double)::<lambda(double)>&, double> >; _Tp = std::function<double(double)>&; _Res = double; _ArgTypes = {double}]'
  353 |         using _Requires = __enable_if_t<_Cond::value, _Tp>;
      |               ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:531:2:   required by substitution of 'template<class _Functor> std::function<double(double)>::_Requires<std::function<double(double)>::_Callable<_Functor, typename std::enable_if<(!(bool)std::is_same<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type, std::function<double(double)> >::value), std::decay<_Ex> >::type::type, std::__invoke_result<typename std::enable_if<(!(bool)std::is_same<typename std::remove_cv<typename std::remove_reference<_Tp>::type>::type, std::function<double(double)> >::value), std::decay<_Ex> >::type::type&, double> >, std::function<double(double)>&> std::function<double(double)>::operator=(_Functor&&) [with _Functor = integrate<std::function<double(double)> >(std::function<double(