题解 | #【模板】前缀和#
【模板】前缀和
https://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf?tpId=230&tqId=2021480&ru=/exam/oj&qru=/ta/dynamic-programming/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D230
import sys #接收数据,将nqdata按需转换 s = input().split() n,q =int(s[0]),int(s[1]) data =input().split() data = list(map(lambda x:int(x),data)) #接收查询的数据 i=1 querylist=[] while i<=q: querylist.append(input().split() ) i+=1 #创建dp数组保存前n项和 dp =[i for i in range(len(data))] dp[0]=0 for i in range(1,len(data)): dp[i]=dp[i-1]+data[i] #print(dp) #计算sum,sum的值就等于前r项的和减去前l项的和 for x in querylist: #print(x) l,r=int(x[0]),int(x[1]) sums =dp[r-1]-dp[l-1]+data[l-1] print(sums)