-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryIndexedTree.py
More file actions
42 lines (36 loc) · 848 Bytes
/
BinaryIndexedTree.py
File metadata and controls
42 lines (36 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
BITを行った後の数列は昇順になるので注意!
"""
import copy
def BIT(A: list) -> int:
"転倒数を求める"
cnt = 0
n = len(A)
if n > 1:
A1 = A[: n >> 1]
A2 = A[n >> 1 :]
cnt += BIT(A1)
cnt += BIT(A2)
i1, i2 = 0, 0
for i in range(n):
if i2 == len(A2):
A[i] = A1[i1]
i1 += 1
elif i1 == len(A1):
A[i] = A2[i2]
i2 += 1
elif A1[i1] <= A2[i2]:
A[i] = A1[i1]
i1 += 1
else:
A[i] = A2[i2]
i2 += 1
cnt += n // 2 - i1
return cnt
def main() -> None:
a = [2, 15, 23, 32, 7, 19]
x = copy.copy(a)
ans = BIT(x)
print(ans)
if __name__ == "__main__":
main()