-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCEPC206.cpp
More file actions
53 lines (53 loc) · 1.01 KB
/
DCEPC206.cpp
File metadata and controls
53 lines (53 loc) · 1.01 KB
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
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define MAX 100001
LL tree[100009];
LL read(int pos)
{
LL res = 0;
while(pos)
{
res += tree[pos];
pos -= (pos & -pos);
}
return res;
}
void update(int pos , int val)
{
while(pos<=MAX)
{
tree[pos] += val;
pos +=(pos & -pos);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int arr[n+9] , hold[n+9] ;
for(int i = 0; i < n ; i++){
scanf("%d",&arr[i]);
hold[i] = arr[i];
}
sort(arr,arr+n);
for(int i = 0; i < n ; i++)
{
int pos = lower_bound(arr , arr + n , hold[i]) - arr;
hold[i] = pos;
}
LL sum = 0;
memset(tree, 0 , sizeof tree);
for(int i = 0; i < n ; i++)
{
sum += read(hold[i]);
update(hold[i] + 1 , arr[hold[i]]);
}
printf("%lld\n",sum);
}
return 0;
}