Code Submission Evaluation System Login

CSES - HIIT Open 2016

HIIT Open 2016

Contest start:2016-05-28 11:00:00
Contest end:2016-05-28 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2016-05-28 12:42:17
2016-05-28 12:37:40
2016-05-28 12:37:12
Task:Approximate
Sender:Oispa Kaljaa
Submission time:2016-05-28 12:42:17
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>

using namespace std;
long long cs[110][101010];
int main(){
  cin.sync_with_stdio(0);
  cin.tie(0);
  
  int n, q; cin >> n >> q;
  int x[101010];
  for(int i = 0; i < n; i++)
    cin >> x[i];
 
  long long s[101010] = {0};
  s[0] = x[0];
  cs[x[0]][0]++;
  for(int i = 1; i < n; i++){
    s[i] = s[i-1]+x[i];
    for(int j = 1; j <= 100; j++)
      cs[j][i] = cs[j][i-1];
    cs[x[i]][i]++;
  }
  while(q--){
      int a, b; cin >> a >> b;
      a--;
      b--;
      long long su = s[b];
      if(a > 0)
	su-=s[a-1];
      double le = (b-a)+1;
      double c = su/le;
      double ans = 0;
      for(long long i = 1; i <= 100; i++){
	  long long mm = cs[i][b];
	  if(a > 0)
	    mm-=cs[i][a-1];
	  //cout << i << " on " << mm << " delta c " << (i-c)  << endl;
	  ans+=(i-c)*(i-c)*mm;
      }
      ans/=le;
      printf("%4.6f\n", ans);
  }
  return 0;
}