#P1471. 方差
方差
Description
The noob HansBug found a magical sequence in a math book, consisting of real numbers. He wants to compute the mean and variance of this sequence.
Input Format
The first line contains two positive integers , denoting the number of real numbers in the sequence and the number of operations.
The second line contains real numbers, where the -th real number is the -th term of the sequence.
The next lines each describe an operation, in one of the following three formats:
- Operation 1:
1 x y k, add (a real number) to every term from the -th to the -th item. - Operation 2:
2 x y, compute the mean of the subsequence from the -th to the -th item. - Operation 3:
3 x y, compute the variance of the subsequence from the -th to the -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 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 with terms, its variance is defined as:
$$s^2=\frac{1}{n}\sum\limits_{i=1}^n\left(A_i-\overline A\right)^2$$where is the mean of sequence , and is the -th term of sequence .
Sample explanation:
| Step | Input | Requirement | Sequence | Output | Note |
| :----------: | :----------: | :----------: | :----------: | :----------: | :----------: |
| | - | - | 1 5 4 2 3 | - | - |
| | 2 1 4 | Compute the mean of all numbers in | 1 5 4 2 3 | 3.0000 | Mean . |
| | 3 1 5 | Compute the variance of all numbers in | 1 5 4 2 3 | 2.0000 | Mean , 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$. |
| | 1 1 1 1 | Add to all numbers in | 2 5 4 2 3 | - | - |
| | 1 2 2 -1 | Add to all numbers in | 2 4 4 2 3 | - | - |
| | 3 1 5 | Compute the variance of all numbers in | 2 4 4 2 3 | 0.8000 | Mean , 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 | | | Note | | :----------: | :----------: | :----------: | :----------: | | | | | - | | | | | Operation not included. | | | | | - |
All numbers in the original sequence and all input are real numbers within the range .
Translated by ChatGPT 5
京公网安备 11011102002149号