“1264. 动态求连续区间和”那题,发现自己写的代码有时超时过不了,代码如下
n,m = map(int,input().split())
lis = list(map(int,input().split()))
lis.insert(0,0)
class Node:
def __init__(self):
self.L = 0
self.R = 0
self.sum = 0
tr = [Node() for _ in range(100010*4)]
def pushup(x):
tr[x].sum = tr[x << 1].sum + tr[x << 1 | 1].sum
def build(x,l,r):
tr[x].L = l
tr[x].R = r
if l == r:
tr[x].sum = lis[l]
else:
mid = l + r >> 1
build(x << 1, l, mid)
build(x << 1 | 1, mid + 1, r)
pushup(x)
def query(x,l,r):
if tr[x].L >= l and tr[x].R <= r:
return tr[x].sum
mid = tr[x].L + tr[x].R >> 1
sum = 0
if l <= mid:
sum += query(x << 1, l, r)
if r > mid:
sum += query(x << 1 | 1, l, r)
return sum
def modify(x,i,v):
if tr[x].L == tr[x].R:
tr[x].sum += v
else:
mid = tr[x].L + tr[x].R >> 1
if i <= mid:
modify(x << 1, i, v)
else:
modify(x << 1 | 1, i ,v)
pushup(x)
build(1,1,n)
for i in range(m):
k,a,b = map(int,input().split())
if k == 0:
print(query(1,a,b))
else:
modify(1,a,b)
#print(tr[16].sum,tr[17].sum,tr[9].sum,tr[10].sum,tr[11].sum,tr[24].sum,tr[25].sum,tr[13].sum,tr[14].sum,tr[15].sum)
发现代码有时能过有时不能,为什么啊