forked from Yuyo1984/Python_Library
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinversion.py
More file actions
Latest commit
24 lines (18 loc) · 621 Bytes
/
Copy pathinversion.py
File metadata and controls
24 lines (18 loc) · 621 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
fromatcoder.fenwicktreeimportFenwickTree
# 転倒数
definversion(arr):
# 座圧
defshrink(arr):
shrinked_set=sorted(set(arr))
shrinked_dict= {s: ifori, sinenumerate(shrinked_set)}
shrinked= [shrinked_dict[a] forainarr]
returnshrinked
shrinked_arr=shrink(arr)
MAX=max(shrinked_arr)
bit=FenwickTree(MAX+1)
result=0
forainshrinked_arr:
# 「aより左にあるaより大きい数」の個数を足す
result+=bit.sum(0, MAX+1) -bit.sum(0, a+1)
bit.add(a, 1)
returnresult