Borrowed Query Convention
The USACO Guide builds prefix sums with a leading zero, so psums[k] is the sum of the first k elements, and its sample code answers a query pair l, r with psums[r] - psums[l].
def psum(arr):
psums = [0]
for i in arr:
psums.append(psums[-1] + i)
return psums
arr = [5, 2, 9, 1, 7]
psums = psum(arr) # [0, 5, 7, 16, 17, 24]
l, r = 2, 2 # 0-indexed, inclusive on both ends
print(psums[r] - psums[l])
The query here is written 0-indexed and inclusive, so the intended answer is arr[2] == 9. What is printed, and what is the correct expression for this convention?
Sign in to answer questions and track your progress
Sign In