#P1471. 方差

    ID: 1832 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>数学线段树树状数组洛谷原创

方差

Description

The noob HansBug found a magical sequence in a math book, consisting of NN real numbers. He wants to compute the mean and variance of this sequence.

Input Format

The first line contains two positive integers N,MN, M, denoting the number of real numbers in the sequence and the number of operations.

The second line contains NN real numbers, where the ii-th real number is the ii-th term of the sequence.

The next MM lines each describe an operation, in one of the following three formats:

  • Operation 1: 1 x y k, add kk (a real number) to every term from the xx-th to the yy-th item.
  • Operation 2: 2 x y, compute the mean of the subsequence from the xx-th to the yy-th item.
  • Operation 3: 3 x y, compute the variance of the subsequence from the xx-th to the yy-th item.

Output Format

Output several lines, each containing one real number, which are the results of every Operation 2 or Operation 3 in order. Round all results to 44 decimal places using standard rounding.

5 5
1 5 4 2 3
2 1 4
3 1 5
1 1 1 1
1 2 2 -1
3 1 5

3.0000
2.0000
0.8000

Hint

About variance: For a sequence AA with nn terms, its variance s2s^2 is defined as:

$$s^2=\frac{1}{n}\sum\limits_{i=1}^n\left(A_i-\overline A\right)^2$$

where A\overline A is the mean of sequence AA, and AiA_i is the ii-th term of sequence AA.

Sample explanation: | Step | Input | Requirement | Sequence | Output | Note | | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | | 00 | - | - | 1 5 4 2 3 | - | - | | 11 | 2 1 4 | Compute the mean of all numbers in [1,4]\left[1,4\right] | 1 5 4 2 3 | 3.0000 | Mean =(1+5+4+2)÷4=3.0000=\left(1+5+4+2\right)\div 4=3.0000. | | 22 | 3 1 5 | Compute the variance of all numbers in [1,5]\left[1,5\right] | 1 5 4 2 3 | 2.0000 | Mean =(1+5+4+2+3)÷5=3=\left(1+5+4+2+3\right)\div 5=3, variance $=\left(\left(1-3\right)^2+\left(5-3\right)^2+\left(4-3\right)^2+\left(2-3\right)^2+\left(3-3\right)^2\right)\div 5=2.0000$. | | 33 | 1 1 1 1 | Add 11 to all numbers in [1,1]\left[1,1\right] | 2 5 4 2 3 | - | - | | 44 | 1 2 2 -1 | Add 1-1 to all numbers in [2,2]\left[2,2\right] | 2 4 4 2 3 | - | - | | 55 | 3 1 5 | Compute the variance of all numbers in [1,5]\left[1,5\right] | 2 4 4 2 3 | 0.8000 | Mean =(2+4+4+2+3)÷5=3=\left(2+4+4+2+3\right)\div 5=3, variance $=\left(\left(2-3\right)^2+\left(4-3\right)^2+\left(4-3\right)^2+\left(2-3\right)^2+\left(3-3\right)^2\right)\div 5=0.8000$. |

Constraints: | Test points | NN | MM | Note | | :----------: | :----------: | :----------: | :----------: | | 131\sim3 | N8N\le 8 | M15M\le 15 | - | | 474\sim7 | N105N\le 10^5 | M105M\le 10^5 | Operation 33 not included. | | 8108\sim10 | N105N\le 10^5 | M105M\le 10^5 | - |

All numbers in the original sequence and all input kk are real numbers within the range [100,100][-100, 100].

Translated by ChatGPT 5