题解 | 【模板】前缀和

【模板】前缀和

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

import sys

# for line in sys.stdin:
#     a = line.split()
#     print(int(a[0]) + int(a[1]))

def  sovle():
    ns = list(map(int,input().split()))
    n =ns[0]
    m =ns[1]
    nums=list(map(int,input().split()))
    
    arr =[0]*(n+1)
    for i in range (1,n+1):
        arr[i]=nums[i-1]+arr[i-1]
    #print(arr)
    for i in range(m):
        cm=list(map(int,input().split()))
        left =cm[0]
        right=cm[1]
        ret=arr[right]-arr[left-1]
        print(ret)
sovle()

全部评论

相关推荐

给个offer灞:校友 是不是金die
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务