-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_11_2.py
More file actions
34 lines (29 loc) · 792 Bytes
/
Copy pathproblem_11_2.py
File metadata and controls
34 lines (29 loc) · 792 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
import sys
input = sys.stdin.readline
TREE_SIZE = 1000000
class FenwickTree:
def __init__(self, n):
self.n = self.tree = [0] * (n+1)
def add(self, pos, x):
pos += 1
while pos < len(self.tree):
self.tree[pos] += x
pos += (pos& -pos)
def sum(self, pos):
pos += 1
ret = 0
while pos > 0:
ret += self.tree[pos]
pos &= (pos - 1)
return ret
def measuretime(n, A):
tree = FenwickTree(TREE_SIZE)
ret = 0
for i in range(n):
ret += tree.sum(TREE_SIZE - 1) - tree.sum(A[i])
tree.add(A[i], 1)
return ret
for _ in range(int(input().strip())):
n = int(input().strip())
A = list(map(int, input().split()))
print(measuretime(n, A))