题目
思路
- 线段树&树状数组 - AcWing
- 算法学习笔记(14): 线段树 - 知乎 (zhihu.com)
代码
Python超时版
def calculate_subarray_sum(nums, a, b):
return sum(nums[a-1:b])
n, m = map(int, input().split())
nums = list(map(int, input().split()))
for _ in range(m):
op, a, b = map(int, input().split())
if op == 0:
print(calculate_subarray_sum(nums, a, b))
elif op == 1:
nums[a-1] += b