1
0
Fork 0
cp-templates/trees/BIT.py

32 lines
443 B
Python
Raw Normal View History

2023-12-02 12:05:10 +08:00
# @ref https://oi.wiki/ds/fenwick
n=...
c=[0]*(n+1)
lowbit=lambda x: x&-x
def prep(a):
res=[0]*(n+1)
s=0
for i in range(1,n+1):
s+=a[i]
res[i]=s
return res
def init(a):
pa=prep(a)
for i in range(1,n+1):
c[i]=pa[i]-pa[i-lowbit(i)]
def getsum(x):
ans=0
while x:
ans+=c[x]
x-=lowbit(x)
return ans
def add(x,k):
while x<=n:
c[x]+=k
x+=lowbit(x)