题解 | #【模板】线段树1#(Python3)
【模板】线段树1
https://www.nowcoder.com/practice/e767b7b441ce4345aad1fac0a3633afb
N = 100100
tr = [0] * N
def lowbit(x):
# x&-x可以取到最右边的1
return x&-x
# 添加固定写法
def add(now, val):
i = now
while(i<N):
tr[i] += val
i += lowbit(i)
# 查询固定写法
def query(now):
res = 0
i = now
while(i>0):
res += tr[i]
i -= lowbit(i)
return res
n, q = map(int, input().split())
a = [0]
a.extend(list(map(int, input().split())))
for i in range(1, n+1):
add(i, a[i])
for _ in range(0, q):
p, x, y = map(int, input().split())
if(p==1):
# 修改:把原来的值抹掉,再加上新值
add(x, -a[x])
# 根据题目
a[x] += y
add(x, a[x])
else:
print(query(y)-query(x-1))
#15天刷题#