Advanced Data Structure 树状数组


树状数组 

更高效地对点、区间进行查询、更新

ref:树状数组详解

k为i的二进制中从低位到高位的连续零长度

C[i] = A[i-2k+1] + A[i-2k+2] + ... + A[i]

2k = i&(-i)

sum[i] = C[i] + C[i - 2k1] + C[i - 2k1 - 2k2]

eg:315. 计算右侧小于当前元素的个数

def lowBit(x:int) -> int:
    return x&(-x)

def update(x: int, n: int, y: int, c:[]):
     i = x
     while i < n:
         c[i] += y
         i += lowBit(i)
def getSum(x: int, c:[]) -> int:
    ans = 0
    i = x
    while i > 0:
        ans += c[i]
        i -= lowBit(i)
    return ans
class Solution:
    def countSmaller(self, nums:[int]) ->[int]:
        buckets = set()
        for num in nums:
            buckets.add(num)
        buckets = sorted(buckets)
        pos = {}
        for i, e in enumerate(list(buckets)):
            pos[e] = i
        n = len(nums)
        counts = [0] * n
        c = [0] * n
        for i in range(n - 1, -1, -1):
            id = pos[nums[i]] + 1
            update(id, n, 1, c)
            counts[i] = getSum(id - 1, c)
        return counts

相关