题解 | #【模板】前缀和#

【模板】前缀和

http://www.nowcoder.com/practice/acead2f4c28c401889915da98ecdc6bf

while True:
    try:
        n,q = map(int,input().split())
        nums = list(map(int,input().split()))
        dp = [0]*n
        dp[0] = nums[0]
        for i in range(1,n):
            dp[i] = dp[i-1] + nums[i]
        for j in range(q):
            l,r = map(int,input().split())
            if 0 <= l <= n and 0 <= r <= n and l <= r:
                result = dp[r-1] - dp[l-1] + nums[l-1]
                print(result)
    except:
        break

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务