Python:如何查找数组中K个连续元素的总和?

Link for the problem - https://www.hackerrank.com/contests/dinnertimemarch20/challenges/dashboard-queries/problem

以下是我对问题的尝试。当N和Q = 100,000时,它给出了超过时间限制的错误,那么更快的方法是什么?

N,Q = map(int,input().split())
x = 0
m = []
for i in range(0,N+1):
    m.append(0)
#print(m)
for q in range(0,Q):
    n = list(map(int,input().split()))
    if n[0] == 2:
        for i in range(n[1]-1,len(m)):
            m[i] = m[i] + n[2]
    else:
        for i in range(0,len(m)):
            if m[i] >= n[1]:
                print(i+1)
                break
评论