#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 1<<17;
struct query {
unsigned l,r,block,idx;
bool operator <(const query &a) const {
return ((block==a.block) ? ((block&1) ? (r<a.r) : (r>a.r)) : (block<a.block));
}
};
template < typename T >
struct fenwick_tree {
int n;
T *it;
void initialize(int k) {
int i;
n=k;
delete [] it;
it=(T*)malloc((sizeof(T)*(n+1)));
for(i=1;i<=n;i++) it[i]=0;
}
void update(int pos, T val) {
for(;pos<=n;pos+=pos&(-pos)) it[pos]+=val;
}
T query(int pos) {
T ans=0;
for(;pos>0;pos-=pos&(-pos)) ans+=it[pos];
return ans;
}
T query(int l, int r) {
return query(r)-query(l-1);
}
};
int tests,current_case;
query queries[N];
int n,q,a[N];
fenwick_tree < long long > sum,cnt;
int sz;
int l,r;
unsigned sq;
double answer[N];
long long s;
long long ps[N];
void message(int current_case) {
fprintf(stderr, "Case %d solved!\n", current_case);
}
void add_it(int value) {
++sz;
sum.update(value,value);
cnt.update(value,1);
s+=value*1ll*cnt.query(1,value);
s+=sum.query(value+1,100000);
}
void remove_it(int value) {
--sz;
s-=value*1ll*cnt.query(1,value);
s-=sum.query(value+1,100000);
sum.update(value,-value);
cnt.update(value,-1);
}
double get_answer() {
long long all=sz*1ll*sz,sum=(s-(ps[r]-ps[l-1]))*2+ps[r]-ps[l-1];
return (double)(sum)/(double)(all);
}
int main() {
int i;
tests=1;
for(current_case=1;current_case<=tests;current_case++) {
scanf("%d %d", &n, &q);
for(i=1;i<=n;i++) scanf("%d", &a[i]),ps[i]=ps[i-1]+a[i];
sum.initialize(100000);
cnt.initialize(100000);
sq=sqrt(n);
for(i=1;i<=q;i++) {
scanf("%u %u", &queries[i].l, &queries[i].r);
queries[i].idx=i;
queries[i].block=queries[i].l/sq;
}
sort(queries+1,queries+1+q);
l=queries[1].l;
r=queries[1].r;
for(i=l;i<=r;i++) add_it(a[i]);
for(i=1;i<=q;i++) {
while(r<queries[i].r) ++r,add_it(a[r]);
while(l<queries[i].l) remove_it(a[l]),++l;
while(l>queries[i].l) --l,add_it(a[l]);
while(r>queries[i].r) remove_it(a[r]),--r;
answer[queries[i].idx]=get_answer();
}
for(i=1;i<=q;i++) printf("%.10lf\n", answer[i]);
message(current_case);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE4gPSAxPDwxNzsKCnN0cnVjdCBxdWVyeSB7CiAgICB1bnNpZ25lZCBsLHIsYmxvY2ssaWR4OwogICAgYm9vbCBvcGVyYXRvciA8KGNvbnN0IHF1ZXJ5ICZhKSBjb25zdCB7CiAgICAgICAgcmV0dXJuICgoYmxvY2s9PWEuYmxvY2spID8gKChibG9jayYxKSA/IChyPGEucikgOiAocj5hLnIpKSA6IChibG9jazxhLmJsb2NrKSk7CiAgICB9Cn07Cgp0ZW1wbGF0ZSA8IHR5cGVuYW1lIFQgPgpzdHJ1Y3QgZmVud2lja190cmVlIHsKICAgIGludCBuOwogICAgVCAqaXQ7CiAgICB2b2lkIGluaXRpYWxpemUoaW50IGspIHsKICAgICAgICBpbnQgaTsKICAgICAgICBuPWs7CiAgICAgICAgZGVsZXRlIFtdIGl0OwogICAgICAgIGl0PShUKiltYWxsb2MoKHNpemVvZihUKSoobisxKSkpOwogICAgICAgIGZvcihpPTE7aTw9bjtpKyspIGl0W2ldPTA7CiAgICB9CiAgICB2b2lkIHVwZGF0ZShpbnQgcG9zLCBUIHZhbCkgewogICAgICAgIGZvcig7cG9zPD1uO3Bvcys9cG9zJigtcG9zKSkgaXRbcG9zXSs9dmFsOwogICAgfQogICAgVCBxdWVyeShpbnQgcG9zKSB7CiAgICAgICAgVCBhbnM9MDsKICAgICAgICBmb3IoO3Bvcz4wO3Bvcy09cG9zJigtcG9zKSkgYW5zKz1pdFtwb3NdOwogICAgICAgIHJldHVybiBhbnM7CiAgICB9CiAgICBUIHF1ZXJ5KGludCBsLCBpb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