不使用高级数据结构,仅用最基础算法的一个优雅的写法 思路:区间合并+前缀和
n,m=map(int,input().split())
pool=[0]+list(map(int,input().split()))
s=[0]*(n+10) #前缀和
for i in range(1,n+1):
s[i]=s[i-1]+pool[i]
is_connected=[] #维护已经连通的水池
for i in range(m):
op=input().split()
if op[0]=='1':
l,r=map(int,op[1:])
is_connected.append([l,r])
if len(is_connected)>1: #区间合并板子
is_connected.sort()
t=[]
st=is_connected[0][0]
ed=is_connected[0][1]
for j in range(1,len(is_connected)):
if is_connected[j][1]<=ed:
continue
elif is_connected[j][0]<=ed:
ed=is_connected[j][1]
else:
t.append([st,ed])
st=is_connected[j][0]
ed=is_connected[j][1]
t.append([st,ed]) #别忘了把最后一个区间加进去
is_connected=t.copy()
else:
x=int(op[1])
for j in range(len(is_connected)):
if is_connected[j][0]<=x:
if is_connected[j][1]>=x: #如果当前查询的水池在已经连通的某个区间中
print('%.10f'%((s[is_connected[j][1]]-s[is_connected[j][0]-1])/(is_connected[j][1]-is_connected[j][0]+1)))
break
else: #查不到说明这个池子两边的板子都没拆,依然是独立的
print('%.10f'%pool[x])