CSES - HIIT Open 2016 - Results
Submission details
Task:Approximate
Sender:ContinuedLife
Submission time:2016-05-28 11:40:12 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.20 sdetails

Code

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(6);

using namespace std;

int main(){ _
	int n, q;
	cin >> n >> q;
	vector<int> vi(n, 0); 
	vector<int> sum(n, 0);
	vector<long long> sum2(n, 0);

	for(int i = 0; i < n; ++i)	cin >> vi[i];
	sum[0] = vi[0];
	for(int i = 1; i < n; ++i)	sum[i] = sum[i-1] + vi[i];

	for(int i = 0; i < n; ++i)	vi[i] = vi[i] * vi[i];
	sum2[0] = vi[0];
	for(int i = 1; i < n; ++i)	sum2[i] = sum2[i-1] + vi[i];

//	for(int i = 0; i < n; ++i)	cout << sum2[i] << endl;	

	int a, b, diff;
	double mean, ans;
	for(int i = 0; i < q; ++i){
		cin >> a >> b;
		a -= 2; b -= 1;
		
		if(a < 0)	diff = sum[b];
		else diff = sum[b] - sum[a];
		mean = diff / (double)(b - a);

//		cout << "a = " << a  << endl;
//		cout << "b = " << b << endl;
		// s_i^2 - 2s_ic_i + c_i^2
		if(a < 0)	ans = sum2[b];
		else ans = sum2[b] - sum2[a];
//		cout << "first = " << ans << endl;

		ans = ans - 2*diff*mean + (b-a)*mean*mean;


		ans /= (b-a);
		cout << fixed << ans << endl;
	}
	
	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100000 100000
62 64 35 47 57 38 52 4 56 13 7...

correct output
831.753342
833.361649
833.847478
834.425131
831.468120
...

user output
831.753342
833.361649
833.847478
834.425131
831.468120
...