题目:https://vjudge.net/problem/HDU-1166
from typing import List
import ipdb
length = 11
def lowbit(x):
return x&(-x)
def update(a, c, index):
idx = index
while idx <= length:
print(index)
c[idx] += a[index]
k = lowbit(idx)
if k == 0:
break
else:
idx += k
def query(c, index):
total = 0
idx = index
while idx > 0:
total += c[idx]
k = lowbit(idx)
if k == 0:
break
else:
idx -= k
return total
def get_sum(c, start, end):
# a-> [start, end)
total = query(c, end)
return total - query(c, start)
if __name__ == '__main__':
para = [0, 3,5,7,6,9,23,23,5,6,8, 0, 0]
c = [0]*(len(para) + 1)
for i in range(1, len(para)+1):
update(para, c, i)
print(para)
print(c)
#query(c, 1)
print(get_sum(c, 1, 4))
~