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 14:42:01
2016-05-28 11:25:47
Task:Approximate
Sender:Verto
Submission time:2016-05-28 14:42:01
Status:READY
Result:ACCEPTED

Show test data

Code

import sys

lines = sys.stdin.readlines()

def asints(s):
	return [int(x.strip()) for x in s.split()]

n, q = asints(lines[0])
sequence = asints(lines[1])

sums = [sequence[0]]
sums2 = [sequence[0]**2]
for i in range(1,len(sequence)):
	val = sequence[i]
	sums.append(sums[i-1] + val)
	sums2.append(sums2[i-1] + val*val)
def ex(a,b):
	if a == 1:
		return sums[b-1] / (b-a+1.0)
	return (sums[b-1] - sums[a-2]) / (b-a+1.0)

def ex2(a,b):
	if a == 1:
		return sums2[b-1] / (b-a+1.0)
	return (sums2[b-1] - sums2[a-2]) / (b-a+1.0)

for i in range(2, q + 2):
	low, high = asints(lines[i])
	var = ex2(low,high) - ex(low,high)**2
	#subs = sequence[low-1:high]
	#avg = sum(subs) *1.0 / len(subs)
	#errs = [(x*1.0-avg)**2 for x in subs]
	print "%.6f" % var#sum(errs) / len(errs)