Problem:
Problem E:
Autocorrelation Function
Nathan O. Davis is taking a class of signal processing as a student in engineering. Today’s topic of the
class was autocorrelation. It is a mathematical tool for analysis of signals represented by functions or
series of values. Autocorrelation gives correlation of a signal with itself. For a continuous real function
f
(
x
), the autocorrelation function
R
f
(
r
) is given by
where
r
is a real number.
The professor teaching in the class presented an assignment today. This assignment requires students
to make plots of the autocorrelation functions for given functions. Each function is piecewise linear and
continuous for all real numbers. The figure below depicts one of those functions.
Figure 1: An Example of Given Functions
The professor suggested use of computer programs, but unfortunately Nathan hates programming. So he
calls you for help, as he knows you are a great programmer. You are requested to write a program that
computes the value of the autocorrelation function where a function
f
(
x
) and a parameter
r
are given as
the input. Since he is good at utilization of existing software, he could finish his assignment with your
program.
Input
The input consists of a series of data sets.
The first line of each data set contains an integer
n
(3 ≤
n
≤ 100) and a real number
r
(-100 ≤
r
≤ 100),
where n denotes the number of endpoints forming sub-intervals in the function
f
(
x
), and
r
gives the
parameter of the autocorrelation function
R
f
(
r
). The
i
-th of the following
n
lines contains two integers
x
i
(-100 ≤
x
i
≤ 100) and
y
i
(-50 ≤
y
i
≤ 50), where (
x
i
,
y
i
) is the coordinates of the
i
-th endpoint.
The endpoints are given in increasing order of their
x
-coordinates, and no couple of endpoints shares the
same
x
-coordinate value. The
y
-coordinates of the first and last endpoints are always zero.
The end of input is indicated by
n
=
r
= 0. This is not part of data sets, and hence should not be
processed.
Output
For each data set, your program should print the value of the autocorrelation function in a line. Each
value may be printed with an arbitrary number of digits after the decimal point, but should not contain
an error greater than 10
-4
.
Sample Input
3 1
0 0
1 1
2 0
11 1.5
0 0
2 7
5 3
7 8
10 -5
13 4
15 -1
17 3
20 -7
23 9
24 0
0 0
Output for the Sample Input
0.166666666666667
165.213541666667

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cout.setf(ios::fixed);
    cout << setprecision(15);

    int n;
    long double r;
    while ( (cin >> n >> r) ) {
        if (n == 0 && fabsl(r) == 0.0L) break;

        vector<long double> x(n), y(n);
        for (int i = 0; i < n; ++i) {
            long double xi, yi;
            cin >> xi >> yi;
            x[i] = xi; y[i] = yi;
        }

        // Предполагается: f(x)=0 вне [x0, x_{n-1}] (y на концах равно 0).
        // Рассматриваем только область перекрытия x и x+r.
        const long double EPS = 1e-12L;

        long double L = max(x.front(), x.front() - r);
        long double R = min(x.back(),  x.back()  - r);

        if (L >= R - EPS) {
            cout << 0.0 << "\n";
            continue;
        }

        // Считаем наклоны и свободные члены на каждом сегменте: f(x)=m[i]*x + c[i] на [x[i], x[i+1]]
        int segs = n - 1;
        vector<long double> m(segs), c(segs);
        for (int i = 0; i < segs; ++i) {
            m[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]);
            c[i] = y[i] - m[i] * x[i];
        }

        // Функции для поиска сегмента, которому принадлежит точка t (x[i] <= t <= x[i+1]).
        auto find_segment = [&](long double t) -> int {
            // upper_bound даст первый x[k] > t, отнимаем 1
            int idx = int(upper_bound(x.begin(), x.end(), t) - x.begin()) - 1;
            if (idx < 0) idx = 0;
            if (idx >= segs) idx = segs - 1; // на правой границе попадём сюда, но дальше мы ограничиваемся R
            return idx;
        };

        int i = find_segment(L);           // сегмент f(x) при x=L
        int j = find_segment(L + r);       // сегмент f(x+r) при x=L

        long double cur = L;
        long double ans = 0.0L;

        while (cur < R - EPS) {
            // следующий излом для x или для (x+r)
            long double next_i = x[i+1];          // граница для f(x)
            long double next_j = x[j+1] - r;      // граница для f(x+r)
            long double nxt = min({next_i, next_j, R});

            long double a = cur, b = nxt;
            if (b > a + EPS) {
                // На [a,b] имеем f(x)=m[i]*x + c[i], f(x+r)=m[j]*(x+r)+c[j] = m[j]*x + (m[j]*r + c[j]).
                long double A = m[i] * m[j];
                long double B = m[i] * (m[j]*r + c[j]) + c[i] * m[j];
                long double C = c[i] * (m[j]*r + c[j]);
                // Интеграл A x^2 + B x + C
                long double I = A * ( (b*b*b - a*a*a) / 3.0L )
                              + B * ( (b*b   - a*a  ) / 2.0L )
                              + C * (  b - a );
                ans += I;
            }

            cur = b;

            // Перейти на следующий сегмент(ы) при необходимости
            if (fabsl(cur - next_i) <= EPS && i+1 < segs) ++i;
            if (fabsl(cur - next_j) <= EPS && j+1 < segs) ++j;
            // защита от возможных застреваний из-за численных ошибок
            if (nxt == cur) {
                // подвинем на микро-шаг
                cur += 1e-13L;
            }
        }

        cout << ans << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3604
final_rank: 13

code_snippet: #include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;
using R=long double;
const R eps=1e-11; // [-1000:1000]->EPS=1e-8 [-10000:10000]->EPS=1e-7
const R pi=acos(-1);
const R inf=1e40;
inline int sgn(const R& r){ return (r > eps) - (r < -eps);}
inline int sgn(const R& a, const R &b){ return sgn(a-b); }
inline R sq(R x){return sqrt(max<R>(x,0.0));}

int n;
R vx[110],vy[110],r;

R val(R x){
	R ret=0.0;
	rep(i,n-1){
		if(vx[i]<=x&&x<vx[i+1]){
			R rate=(x-vx[i])/(vx[i+1]-vx[i]);
			ret=vy[i]+rate*(vy[i+1]-vy[i]);
			return ret;
		}
	}
	return ret;
}

R func(R x){ return 1.0*val(x)*val(x+r); }

int main(void){
	while(cin >> n >> r,n){	
		vector<R> ary;
		rep(i,n) cin >> vx[i] >> vy[i];
		rep(i,n) ary.push_back(vx[i]),ary.push_back(vx[i]+r),ary.push_back(vx[i]-r);
		
		uniq(ary);
		int m=ary.size();

		R ans=0.0;
		rep(i,m-1){
			R a=ary[i],b=ary[i+1],c=(a+b)/2.0;
			R fa=func(a),fb=func(b),fc=func(c);
			R cur=(fa+4.0*fc+fb)*(b-a)/6.0;
			ans+=cur;
		}
		cout.precision(20);
		cout << fixed << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3160
final_rank: 12

code_snippet: //
// Numerical Integration (Adaptive Gauss--Lobatto--Kronrod)
//
// Description:
//   Gauss--Lobatto formula is a numerical integrator
//   that is accurate for polynomials of degree <= 2n+1.
//   Adaptive Gauss--Lobatto recursively divides the domain
//   and computes integral by using G-L formula.
//
// Algorithm:
//   Above.
//
// Complexity:
//   O(#pieces) for a piecewise polynomials.
//   In general, it converges in O(1/n^6) for smooth functions.
//
// Verified: 
//   AOJ 2034
//
// References:
//   R. T. Trimbitas, M. G. Trimbitas (2008):
//   Gauss-Lobatto-Kronrod formulae and adaptive numerical integration.
//   in Proceedings of the 10th International Symposium on 
//   Symbolic and Numeric Algorithms for Scientific Computing. pp. 183--186.
//
#include <iostream>
#include <vector>
#include <limits>
#include <cstdio>
#include <algorithm>
#include <functional>

using namespace std;

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

// adaptive gauss-lobatto
template <class F>
double integrate(F f, double lo, double hi, double eps = 1e-8) {
  const double th = eps / 1e-14; // (= eps / machine_epsilon)
  function<double (double,double,double,double,int)> rec =
    [&](double x0, double x6, double y0, double y6, int d) {
    const double a = sqrt(2.0/3.0), b = 1.0 / sqrt(5.0);
    double x3 = (x0 + x6)/2, y3 = f(x3), h = (x6 - x0)/2;
    double x1 = x3-a*h, x2 = x3-b*h, x4 = x3+b*h, x5 = x3+a*h;
    double y1 = f(x1),  y2 = f(x2),  y4 = f(x4),  y5 = f(x5);
    double I1 = (y0+y6 + 5*(y2+y4)) * (h/6);
    double I2 = (77*(y0+y6) + 432*(y1+y5) + 625*(y2+y4) + 672*y3) * (h/1470);
    if (x3 + h == x3 || d > 50) return 0.0; 
    if (d > 4 && th + (I1-I2) == th) return I2;
    return (double)(rec(x0, x1, y0, y1, d+1) + rec(x1, x2, y1, y2, d+1) 
                  + rec(x2, x3, y2, y3, d+1) + rec(x3, x4, y3, y4, d+1) 
                  + rec(x4, x5, y4, y5, d+1) + rec(x5, x6, y5, y6, d+1));
  };
  return rec(lo, hi, f(lo), f(hi), 0);
}

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 f(x) * f(x + r);
    };
    printf("%.12f\n", integrate(g, p[0].fst-100, p.back().fst+100));
  }
}
accuracy: 1
time_ms: 1690
memory_kb: 1244
final_rank: 14

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const double DELTA = 1e-10;

/* typedef */

struct tri {
  double x, y0, y1;
  tri() {}
  tri(double _x, double _y0, double _y1) { x = _x, y0 = _y0, y1 = _y1; }
  bool operator<(const tri& t0) const { return x < t0.x; }
};

/* global variables */

int n, cn;
double r, minx, maxx;
double xs[MAX_N], ys[MAX_N];
tri cxys[MAX_N * 2];

/* subroutines */

// (x0,y0)-(x1-y1)
// dx=x1-x0,dy=y1-y0
// y = dy/dx * (x-x0) + y0 = dy/dx * x + (dx*y0-dy*x0)/dx

inline void line_eq(double x0, double y0, double x1, double y1,
		    double& a, double& b) {
  double dx = x1 - x0;
  double dy = y1 - y0;
  a = dy / dx;
  b = (dx * y0 - dy * x0) / dx;
}

double f(double x) {
  if (x <= minx || x >= maxx) return 0.0;
  int k = upper_bound(xs, xs + n, x) - xs - 1;
  double a, b;
  line_eq(xs[k], ys[k], xs[k + 1], ys[k + 1], a, b);
  return a * x + b;
}

/* main */

int main() {
  for (;;) {
    cin >> n >> r;
    if (n == 0) break;

    for (int i = 0; i < n; i++)
      cin >> xs[i] >> ys[i];
    minx = xs[0];
    maxx = xs[n - 1];

    cn = 0;
    for (int i = 0; i < n; i++) {
      if (xs[i] + r <= maxx)
	cxys[cn++] = tri(xs[i], ys[i], f(xs[i] + r));
      if (xs[i] - r >= minx)
	cxys[cn++] = tri(xs[i] - r, f(xs[i] - r), ys[i]);
    }

    sort(cxys, cxys + cn);

    double sum = 0.0;

    for (int i = 0; i < cn - 1; i++) {
      double a0, b0, a1, b1;
      tri& cxy0 = cxys[i], cxy1 = cxys[i + 1];
      double x0 = cxy0.x, x1 = cxy1.x;
      if (x1 - x0 < DELTA) continue;

      line_eq(x0, cxy0.y0, x1, cxy1.y0, a0, b0);
      line_eq(x0, cxy0.y1, x1, cxy1.y1, a1, b1);

      // f(x)*f(x+r) = (a0*x+b0)(a1*x+b1)=(a0a1)*x^2+(a0b1+a1b0)*x+(b0b1)
      double aa = a0 * a1;
      double bb = a0 * b1 + a1 * b0;
      double cc = b0 * b1;

      double itg =
	aa * (x1 * x1 * x1 - x0 * x0 * x0) / 3 +
	bb * (x1 * x1 - x0 * x0) / 2 +
	cc * (x1 - x0);

      sum += itg;
    }

    printf("%.10lf\n", sum);
  }

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1256
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;

int main(){
    int N;
    double R;
    while(cin >> N >> R && N > 0) {
        vector<double> xs(N), ys(N);
        REP(i, N) cin >> xs[i] >> ys[i];

        vector<double> p;
        REP(i, xs.size()){
            double x = xs[i];
            if(xs.front() <= x + R && x + R <= xs.back()) {
                p.push_back(x);
            }
        }
        REP(i, xs.size()){
            double x = xs[i] - R;
            if(xs.front() <= x && x <= xs.back()) {
                p.push_back(x);
            }
        }
        sort(p.begin(), p.end());
        p.erase(unique(p.begin(), p.end()), p.end());
        double ans = 0.0;
        for(int i = 0; i + 1 < p.size(); i++) {
            double lx = p[i];
            double rx = p[i+1];
            double cx = (lx + rx) / 2;

            int k = upper_bound(xs.begin(), xs.end(), cx) - xs.begin() - 1;
            //assert(xs[k] <= lx && rx <= xs[k+1]);
            assert(k >= 0);
            double A = (ys[k+1] - ys[k]) / (xs[k+1] - xs[k]);
            double B = ys[k] - A * xs[k];
            
            double lxr = p[i] + R;
            double rxr = p[i+1] + R;
            double cxr = (lxr + rxr) / 2;
            int kr = upper_bound(xs.begin(), xs.end(), cxr) - xs.begin() - 1;
            //assert(xs[kr] <= lxr && rxr <= xs[kr+1]);
            assert(kr >= 0);
            double C = (ys[kr+1] - ys[kr]) / (xs[kr+1] - xs[kr]);
            double D = ys[kr] - C * xs[kr] + C * R;

            double E = A * C;
            double F = A * D + B * C;
            double G = B * D;
            // E * x^3/3 + F * x^2/2 + G*x
            auto f = [&](double x) -> double {
                return E*x*x*x/3 + F*x*x/2 + G*x;
            };
            ans += f(p[i+1]) - f(p[i]);
        }
        printf("%.12f\n", ans);
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1268
final_rank: 10

code_snippet: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        double r;
        cin >> n >> r;
        if(n == 0)
            return 0;

        vector<double> x(n), y(n);
        for(int i=0; i<n; ++i)
            cin >> x[i] >> y[i];

        vector<double> s(n-1), t(n-1); // y = s * x + t
        for(int i=0; i<n-1; ++i){
            s[i] = (y[i+1] - y[i]) / (x[i+1] - x[i]);
            t[i] = y[i] - s[i] * x[i];
        }

        double ret = 0.0;
        for(int i=0; i<n-1; ++i){
            for(int j=0; j<n-1; ++j){
                double x1 = max(x[i], x[j] - r);
                double x2 = min(x[i+1], x[j+1] - r);
                if(x1 > x2)
                    continue;

                double s1 = s[i];
                double t1 = t[i];
                double s2 = s[j];
                double t2 = t[j] + s[j] * r;

                double a, b, c; // y = a * x^2 + b * x + c
                a = s1 * s2;
                b = s1 * t2 + s2 * t1;
                c = t1 * t2;
                
                ret += a / 3 * pow(x2, 3) + b / 2 * pow(x2, 2) + c * x2;
                ret -= a / 3 * pow(x1, 3) + b / 2 * pow(x1, 2) + c * x1;
            }
        }
        printf("%.10f\n", ret);
    }
}
accuracy: 1
time_ms: 20
memory_kb: 1252
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include <cstring>

using namespace std;

const double EPS=(1e-10);
bool EQ(double a,double b){
    return abs(a-b)<EPS;
}

typedef complex<double> P;
typedef pair<double,double> pdd;

double calc(double a1,double b1,double a2,double b2,double x,double r){
    return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;
}

int main(){
    int n;
    double r;
    while(cin>>n>>r&&!(n==0&&EQ(r,0))){
        vector<P> dots;
        vector<pdd> co;
        dots.push_back(P(-1000,0));
        for(int i=0;i<n;i++){
            double x,y;
            cin>>x>>y;
            dots.push_back(P(x,y));
        }
        dots.push_back(P(1000,0));
        double sum=0;
        n+=2;
        for(int i=1;i<n;i++){
            double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());
            double b=dots[i-1].imag()-a*dots[i-1].real();
            co.push_back(pdd(a,b));
        }
        double ax=dots[1].real();
        double bx=ax+r;
        int nxtApos=n;
        int nxtBpos=n;
        // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ
        for(int i=0;i<n;i++){
            if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){
                nxtBpos=i;
                break;
            }
        }
        for(int i=0;i<n;i++){
            if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){
                nxtApos=i;
                break;
            }
        }
        // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ
        while(nxtBpos!=n&&nxtApos!=n){
            // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ
            double adis=dots[nxtApos].real()-ax;
            double bdis=dots[nxtBpos].real()-bx;
            double dis=min(adis,bdis);
            double t=ax+dis;
            double s=ax;
            sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);
            sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);
            ax+=dis;bx+=dis;
            if(EQ(adis,bdis)){
                nxtApos++;
                nxtBpos++;
            }
            else if(adis<bdis)nxtApos++;
            else nxtBpos++;
        }
        printf("%.10f\n",sum);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 988
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include <cstring>

using namespace std;

const double EPS=(1e-10);
bool EQ(double a,double b){
    return abs(a-b)<EPS;
}

typedef complex<double> P;
typedef pair<double,double> pdd;

double calc(double a1,double b1,double a2,double b2,double x,double r){
    return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;
}

int main(){
    int n;
    double r;
    while(cin>>n>>r&&!(n==0&&EQ(r,0))){
        vector<P> dots;
        vector<pdd> co;
        dots.push_back(P(-1000,0));
        for(int i=0;i<n;i++){
            double x,y;
            cin>>x>>y;
            dots.push_back(P(x,y));
        }
        dots.push_back(P(1000,0));
        double sum=0;
        for(int i=1;i<=n+1;i++){
            double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());
            double b=dots[i-1].imag()-a*dots[i-1].real();
            co.push_back(pdd(a,b));
        }
        double ax=dots[1].real();
        double bx=ax+r;
        int nxtApos=n+2;
        int nxtBpos=n+2;
        // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){
                nxtBpos=i;
                break;
            }
        }
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){
                nxtApos=i;
                break;
            }
        }
        // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ
        while(nxtBpos!=n+2&&nxtApos!=n+2){
            // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ
            double adis=dots[nxtApos].real()-ax;
            double bdis=dots[nxtBpos].real()-bx;
            double dis=min(adis,bdis);
            double t=ax+dis;
            double s=ax;
            sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);
            sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);
            ax+=dis;bx+=dis;
            if(EQ(adis,bdis)){
                nxtApos++;
                nxtBpos++;
            }
            else if(adis<bdis)nxtApos++;
            else nxtBpos++;
        }
        printf("%.10f\n",sum);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 988
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include <cstring>

using namespace std;

const double EPS=(1e-10);
bool EQ(double a,double b){
    return abs(a-b)<EPS;
}

typedef complex<double> P;
typedef pair<double,double> pdd;

double calc(double a1,double b1,double a2,double b2,double x,double r){
    return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;
}

int main(){
    int n;
    double r;
    while(cin>>n>>r&&!(n==0&&EQ(r,0))){
        vector<P> dots;
        vector<pdd> co;
        dots.push_back(P(-1000,0));
        for(int i=0;i<n;i++){
            double x,y;
            cin>>x>>y;
            dots.push_back(P(x,y));
        }
        dots.push_back(P(1000,0));
        double sum=0;
        for(int i=1;i<=n+1;i++){
            double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());
            double b=dots[i-1].imag()-a*dots[i-1].real();
            co.push_back(pdd(a,b));
        }
        double ax=dots[1].real();
        double bx=ax+r;
        int nxtApos=n+2;
        int nxtBpos=n+2;
        // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){
                nxtBpos=i;
                break;
            }
        }
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){
                nxtApos=i;
                break;
            }
        }
        //bool rev=false;
        //if(ax>bx){
        //    swap(ax,bx);
        //    swap(nxtApos,nxtBpos);
        //    rev=true;
        //}
        // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ
        while(nxtBpos!=n+2&&nxtApos!=n+2){
            // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ
            double adis=dots[nxtApos].real()-ax;
            double bdis=dots[nxtBpos].real()-bx;
            double dis=min(adis,bdis);
            double t=ax+dis;
            double s=ax;
            sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);
            sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);
            //if(rev){
            //    t=bx+dis;
            //    s=bx;
            //    sum+=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,t,r);
            //    sum-=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,s,r);
            //}
            //else{
            //    sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);
            //    sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);
            //}
            ax+=dis;bx+=dis;
            if(EQ(adis,bdis)){
                nxtApos++;
                nxtBpos++;
            }
            else if(adis<bdis)nxtApos++;
            else nxtBpos++;
        }
        printf("%.10f\n",sum);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 992
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include <cstring>

using namespace std;

const double EPS=(1e-10);
bool EQ(double a,double b){
    return abs(a-b)<EPS;
}

typedef complex<double> P;
typedef pair<double,double> pdd;

double calc(double a1,double b1,double a2,double b2,double x,double r){
    return (a2*a1*x*x*x)/3.0+(x*x*(a1*a2*r+b2*a1+a2*b1))/2.0+(a2*b1*r+b1*b2)*x;
}

int main(){
    int n;
    double r;
    while(cin>>n>>r&&!(n==0&&EQ(r,0))){
        vector<P> dots;
        vector<pdd> co;
        dots.push_back(P(-1000,0));
        for(int i=0;i<n;i++){
            double x,y;
            cin>>x>>y;
            dots.push_back(P(x,y));
        }
        dots.push_back(P(1000,0));
        double sum=0;
        for(int i=1;i<=n+1;i++){
            double a=(dots[i-1].imag()-dots[i].imag())/(dots[i-1].real()-dots[i].real());
            double b=dots[i-1].imag()-a*dots[i-1].real();
            co.push_back(pdd(a,b));
        }
        double ax=dots[1].real();
        double bx=ax+r;
        int nxtApos=n+2;
        int nxtBpos=n+2;
        // rツづ個値ツつェツづづ個凝ヲツ甘板づ可甘慊づ慊づェツづつ「ツづゥツつゥツづーツ探ツ催オ
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>bx&&!EQ(dots[i].real(),bx)){
                nxtBpos=i;
                break;
            }
        }
        for(int i=0;i<=n+1;i++){
            if(dots[i].real()>ax&&!EQ(dots[i].real(),ax)){
                nxtApos=i;
                break;
            }
        }
        bool rev=false;
        if(ax>bx){
            swap(ax,bx);
            swap(nxtApos,nxtBpos);
            rev=true;
        }
        // nxtBposツつェnツづ可づ按づゥツづ慊づ堕アツつッツづゥ
        while(nxtBpos!=n+2){
            // ツ篠淞づ個湘ェツ渉環づ鳴づ個仰猟猟」ツつェツ渉ャツつウツつ「ツづ個づ債づつソツづァツつゥ
            double adis=dots[nxtApos].real()-ax;
            double bdis=dots[nxtBpos].real()-bx;
            double dis=min(adis,bdis);
            double t=ax+dis;
            double s=ax;
            if(rev){
                t=bx+dis;
                s=bx;
                sum+=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,t,r);
                sum-=calc(co[nxtBpos-1].first,co[nxtBpos-1].second,co[nxtApos-1].first,co[nxtApos-1].second,s,r);
            }
            else{
                sum+=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,t,r);
                sum-=calc(co[nxtApos-1].first,co[nxtApos-1].second,co[nxtBpos-1].first,co[nxtBpos-1].second,s,r);
            }
            ax+=dis;bx+=dis;
            if(EQ(adis,bdis)){
                nxtApos++;
                nxtBpos++;
            }
            else if(adis<bdis)nxtApos++;
            else nxtBpos++;
        }
        printf("%.10f\n",sum);
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 992
final_rank: 4

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

struct P {
  double x, y;
  bool f;
  P(double x, double y, bool f) : x(x), y(y), f(f) {}
};
bool operator<(const P &a, const P &b) {
  return a.x!=b.x?a.x<b.x : a.y<b.y;
}

double X[100];
double Y[100];
int n;

double func(double x) {
  if (x<=X[0] || x>=X[n-1]) return 0;
  int q = upper_bound(X, X+n, x) - X;
  int p = q-1;
  return (Y[q]-Y[p])/(X[q]-X[p])*(x-X[p])+Y[p];
}

int main() {
  double r;
  while(cin>>n>>r,n) {
    vector<P> v;
    REP(i, n) {
      cin >> X[i] >> Y[i];
      v.push_back(P(X[i], Y[i], 0));
      v.push_back(P(X[i]-r, Y[i], 1));
    }
    sort(ALL(v));
    double ans = 0;
    REP(i, v.size()-1) {
      double a = v[i].x;
      double b = v[i+1].x;
      double c = (a+b)/2;
      double fa = func(a)*func(a+r);
      double fb = func(b)*func(b+r);
      double fc = func(c)*func(c+r);
      ans += (b-a)/6*(fa+4*fc+fb);
    }
    printf("%.10f\n",ans);
  }
}
accuracy: 1
time_ms: 50
memory_kb: 992
final_rank: 6

code_snippet: #include<cstdio>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const double EPS=1e-9;

struct Point{ double x,y; };

int n;
Point _f[100],_g[100];

double f(double x){
	if(x<_f[0].x) return 0;
	rep(i,n-1){
		double x1=_f[i].x,x2=_f[i+1].x;
		double y1=_f[i].y,y2=_f[i+1].y;
		if(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;
	}
	return 0;
}

double g(double x){
	if(x<_g[0].x) return 0;
	rep(i,n-1){
		double x1=_g[i].x,x2=_g[i+1].x;
		double y1=_g[i].y,y2=_g[i+1].y;
		if(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;
	}
	return 0;
}

int main(){
	for(double r;scanf("%d%lf",&n,&r),n;){
		double ev[200];
		rep(i,n){
			int x,y; scanf("%d%d",&x,&y);
			_f[i]=(Point){x,y};
			_g[i]=(Point){x+r,y};
			ev[2*i+0]=x;
			ev[2*i+1]=x+r;
		}
		sort(ev,ev+2*n);

		double ans=0;
		rep(i,2*n-1){
			double x1=ev[i],x2=ev[i+1];
			if(x2-x1<EPS) continue;

			double f1=f(x1),f2=f(x2),g1=g(x1),g2=g(x2);
			double af=(f2-f1)/(x2-x1),bf=f1-af*x1;
			double ag=(g2-g1)/(x2-x1),bg=g1-ag*x1;
			ans+=af*ag*(x2*x2*x2-x1*x1*x1)/3+(af*bg+bf*ag)*(x2*x2-x1*x1)/2+bf*bg*(x2-x1);
		}
		printf("%.15f\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 820
final_rank: 1

code_snippet: #include<cstdio>
#include<vector>
#include<complex>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef complex<double> Point;

const double EPS=1e-9;

namespace std{
	bool operator <(const Point &a,const Point &b){
		return abs(a.real()-b.real())<EPS?(a.imag()+EPS<b.imag()):(a.real()+EPS<b.real());
	}
}

class Line:public vector<Point>{
public:
	Line(){}
	Line(const Point &a,const Point &b){ push_back(a), push_back(b); }
};

class Segment:public Line{
public:
	Segment(){}
	Segment(const Point &a,const Point &b):Line(a,b){}
};

inline double dot(const Point &a,const Point &b){
	return a.real()*b.real()+a.imag()*b.imag();
}

inline double cross(const Point &a,const Point &b){
	return a.real()*b.imag()-a.imag()*b.real();
}

enum {CCW=1,CW=-1,ON=0};
int ccw(const Point &a,Point b,Point c){
	b-=a, c-=a;
	double rotdir=cross(b,c);
	if(rotdir>EPS)  return CCW;
	if(rotdir<-EPS) return CW;
	return ON;
}

inline void calc_abc(const Line &l,double &a,double &b,double &c){
	a=l[0].imag()-l[1].imag();
	b=l[1].real()-l[0].real();
	c=cross(l[0],l[1]);
}

bool cover(const Segment &s,const Point &p){
	return dot(s[0]-p,s[1]-p)<EPS;
}

bool intersect(const Segment &s,const Segment &t,Point *p){
	bool flg;
	if(abs(cross(s[1]-s[0],t[1]-t[0]))<EPS && abs(cross(s[1]-s[0],t[0]-s[0]))<EPS){
		flg=(cover(s,t[0]) || cover(s,t[1]) || cover(t,s[0]) || cover(t,s[1]));
		if(!flg) return false;
	}

	if(flg || (ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0
			&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0)){
		double a1,b1,c1,a2,b2,c2;
		calc_abc(s,a1,b1,c1);
		calc_abc(t,a2,b2,c2);
		double det=a1*b2-a2*b1;
		if(abs(det)<EPS){ // s is parallel to t
			Point q[3]={s[0],s[1],t[0]};
			rep(i,3){
				if(cover(s,q[i]) && cover(t,q[i])){
					*p=q[i];
					break;
				}
			}
		}
		else *p=Point(b1*c2-b2*c1,a2*c1-a1*c2)/det;
		return true;
	}
	return false;
}

int n;
Point _f[100],_g[100];

double f(double x){
	if(x<_f[0].real()) return 0;
	rep(i,n-1){
		double x1=_f[i].real(),x2=_f[i+1].real();
		double y1=_f[i].imag(),y2=_f[i+1].imag();
		if(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;
	}
	return 0;
}

double g(double x){
	if(x<_g[0].real()) return 0;
	rep(i,n-1){
		double x1=_g[i].real(),x2=_g[i+1].real();
		double y1=_g[i].imag(),y2=_g[i+1].imag();
		if(x<x2) return (y2-y1)/(x2-x1)*(x-x1)+y1;
	}
	return 0;
}

int main(){
	for(double r;scanf("%d%lf",&n,&r),n;){
		vector<double> ev;
		rep(i,n){
			int x,y; scanf("%d%d",&x,&y);
			_f[i]=Point(x,y);
			_g[i]=Point(x+r,y);
			ev.push_back(x);
			ev.push_back(x+r);
		}

		rep(i,n-1) rep(j,n-1) {
			Point p;
			if(intersect(Segment(_f[i],_f[i+1]),Segment(_g[i],_g[i+1]),&p)){
				ev.push_back(p.real());
			}
		}

		sort(_f,_f+n);
		sort(_g,_g+n);
		sort(ev.begin(),ev.end());

		double ans=0;
		rep(i,ev.size()-1){
			double x1=ev[i],x2=ev[i+1];
			if(x2-x1<EPS) continue;
			double f1=f(x1),f2=f(x2),g1=g(x1),g2=g(x2);
			double af=(f2-f1)/(x2-x1),bf=f1-af*x1;
			double ag=(g2-g1)/(x2-x1),bg=g1-ag*x1;
			ans+=af*ag*(x2*x2*x2-x1*x1*x1)/3+(af*bg+bf*ag)*(x2*x2-x1*x1)/2+bf*bg*(x2-x1);
		}
		printf("%.15f\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 770
memory_kb: 924
final_rank: 11

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

class EQ{
public:
  double a,b;
  double xs,xe;
};

double compute(double a,double b,double c,double x){
  return a*x*x*x+b*x*x+c*x;
}

double integ(double ai,double bi,double aj,double bj,double xs,double xe){
  double ta=compute((ai*aj)/3,(ai*bj+aj*bi)/2,bi*bj,xs);
  double tb=compute((ai*aj)/3,(ai*bj+aj*bi)/2,bi*bj,xe);
  return tb-ta;
}

bool have_common(double xs1,double xe1,double xs2,double xe2){
  if ((xs2 <= xs1 && xs1 <= xe2)||
      (xs2 <= xe1 && xe1 <= xe2)||
      (xs1 <= xs2 && xs2 <= xe1)||
      (xs1 <= xe2 && xe2 <= xe1))return true;
  else return false;
}

double solve(vector<EQ> &in,double r){
  int n = in.size();
  double ans = 0;
  rep(i,n){
    double xs=in[i].xs+r,xe=in[i].xe+r;
    rep(j,n){
      //cout << xs <<" " << xe << endl;
      if (have_common(xs,xe,in[j].xs,in[j].xe)){
        double cxs=max(xs,in[j].xs),cxe=min(xe,in[j].xe);
        //      cout <<"test :" <<  cxs-r <<" " << cxe-r <<
        //        " " << integ(in[i].a,in[i].b,in[j].a,in[j].b+r*in[j].a,cxs-r,cxe-r) <<" test end " <<  endl;

        ans+=    integ(in[i].a,in[i].b,in[j].a,in[j].b+r*in[j].a,cxs-r,cxe-r);
        if (xe < in[j].xe)break;
        else xs=in[j].xe;
      }
    }
  }
  return ans;
}


main(){
  int n;
  double r;
  while(cin>>n>>r && n){
    vector<double> x,y;
    vector<EQ>in;
    rep(i,n){
      double tx,ty;
      cin>>tx>>ty;
      x.push_back(tx);
      y.push_back(ty);
    }

    EQ ins;
    rep(i,n-1){
      ins.xs=x[i];ins.xe=x[i+1];
      ins.a=(y[i+1]-y[i])/(x[i+1]-x[i]);
      ins.b=-ins.a*x[i]+y[i];
      in.push_back(ins);
    }

    printf("%.10lf\n",solve(in,r));
  }
  return false;
}
accuracy: 1
time_ms: 50
memory_kb: 996
final_rank: 7