Autocorrelation Function
InterviewTime limit8sMemory limit512 MB
A piecewise linear function is given by its endpoints; compute the integral of f(x)f(x+r) over the whole line for a given shift r.
- Level
Medium5 of 10
- Topics
- Math, Geometry, Implementation, Simulation
- Solved
- No attempts yet
Problem
Autocorrelation measures how much a signal matches a shifted copy of itself. For a continuous function defined on the whole real line, the autocorrelation function is
where is a real number.
Every function in this problem is piecewise linear and continuous. It is given by endpoints through with distinct -coordinates, and consecutive endpoints are joined by a straight segment. The first and the last endpoint have -coordinate 0, and is 0 outside the interval . The figure below shows one such function.

Given and , compute .
Input
The input holds several data sets.
The first line of each data set has an integer () and a real number (). Here is the number of endpoints that form , and is the parameter of the autocorrelation function. Each of the next lines has two integers () and (), the coordinates of the -th endpoint. The endpoints come in increasing order of -coordinate, and no two endpoints share an -coordinate. The first and the last endpoint always have -coordinate 0.
A line with and both equal to 0 ends the input. That line is not a data set, so do not process it.
Output
For each data set, print the value of on its own line. Round the value at the seventh digit after the decimal point and print exactly six digits after the decimal point. If the value is 0, print 0.000000 and not -0.000000.