CSES - Practice Contest 2024 - Results
Submission details
Task:Approximate
Sender:GenericTeamname
Submission time:2024-09-28 15:51:47 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.30 sdetails

Code

#include <bits/stdc++.h>

using namespace std;
#define int long long

// g++ <filename>.cpp -g -Wall -Wextra -DDEBUG -o <executable>

// copied from: https://codeforces.com/blog/entry/79024
// === Debug macro starts here ===

int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

// === Debug macro ends here ===

signed main() {
    
    int n, q;
    cin >> n >> q;
    
    vector<int> prefix_sum(n+1, 0);
    vector<int> prefix_sum_square(n+1, 0);

    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        prefix_sum[i] = prefix_sum[i-1] + x;
        prefix_sum_square[i] = prefix_sum_square[i-1] + x * x;
    }

    // dbg(prefix_sum_square);

    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;

        double k = (b-a)+1;
        double sum = prefix_sum[b] - prefix_sum[a-1];
        double squared = prefix_sum_square[b] - prefix_sum_square[a-1];
        double constant = sum / k;

        double error = squared / k - constant * constant;
        printf ("%.6f\n", error);
    } 

    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
...
Truncated