CorrelationTTV (Hard version)

Normal+ 4 Exclusive 10s 256 MB
Math HRT Millennium

This is the hard version of RollingCorr. The only difference between this and the easy version are the constraints. Solving one problem does not guarantee solving the other.

This is an interactive problem.

Implement the class RollingCorr, which persistently calculates the Pearson correlation coefficient of a two-dimensional incoming data stream.

You will receive a sequence of pairs (xi,yi)(x_i, y_i). After each pair arrives, you must update your internal state and return the correlation of all points seen so far.

The interactor is non-adaptive, meaning the pair you receive is independent of your previous correlation calculation. However, the judge will immediately terminate if your correlation is incorrect.

Signature
RollingCorr()
  • Initializes whatever internal state you need for the rolling updates.
Signature
update(double x, double y)
  • The judge will call update(x, y) repeatedly.

  • Each call appends the pair (x,y)(x,y) to the stream.

  • Return the Pearson correlation coefficient of all appended pairs so far (including the new one).

Pearson Correlation Coefficient

The correlation after nn points is:

r=∑i=1n(xi−x‾)(yi−y‾)∑i=1n(xi−x‾)2⋅∑i=1n(yi−y‾)2r=\frac{\sum_{i=1}^{n}(x_i-\overline{x})(y_i - \overline{y})}{\sqrt{\sum_{i=1}^{n}(x_i-\overline{x})^2}\cdot\sqrt{\sum_{i=1}^{n}(y_i-\overline{y})^2}}

  • If fewer than two points have been seen, return 0.0.

    • If the correlation is undefined (for example, all xix_i are identical or all yiy_i are identical), return 0.0.

    • Your answer will be accepted if it is within 1e-6 absolute or relative error of the expected value.

    • You do not need to write any input/output parsing.

  • x and y are finite double values.
  • |x| and |y| are at most 1,000,000,001.
  • The number of updates may be at most 1e6.
Accepted 14/166
Acceptance 8%
Loading editor...
Input
Inserting data stream...
Expected output