python树状数组 50
查看原帖
python树状数组 50
165533
浮梦CalcuLus楼主2021/9/14 12:44

c++树状数组过了

py树状数组t了

有没有办法救一下

希望各位聚聚抬一手

c = [0 for i in range(500005)]
n = 0
def lowbit(x):
    return x & (-x)
def add(x, k, n):
    while k <= n:
        c[k] += x
        k += lowbit(k)
def getsum(x):
    ret = 0
    while x > 0:
        ret += c[x]
        x -= lowbit(x)
    return ret

d = {}
n = int(input())
a = list(map(int, input().split()))
cnt = 1
b = sorted(a)
for i in range(n):
    if b[i] not in d:
        d[b[i]] = cnt
        cnt += 1
a = list(map(lambda x: d[x], a))
ans = 0
for i in range(n):
    num = a[i]
    ans += i - getsum(num)
    add(1, num, n)
print(ans)
2021/9/14 12:44
加载中...