-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dcepc206.cpp
52 lines (52 loc) · 1.26 KB
/
Dcepc206.cpp
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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/DCEPC206/
#include <cstdio>
#include <cstring>
#include <set>
#include <unordered_map>
#define MAXN 1000001
#define LSOne(S) (S & (-S))
using namespace std;
typedef long long ll;
ll bit[MAXN];
int n, contador, vetor[MAXN];
void update(int idx, ll val) {
while (idx <= n) {
bit[idx] += val;
idx += LSOne(idx);
}
}
ll sum(int idx) {
ll answer = 0;
while (idx > 0) {
answer += bit[idx];
idx -= LSOne(idx);
}
return answer;
}
int main() {
int TC;
scanf("%d", &TC);
while (TC--) {
set<int> conjunto;
unordered_map<int, int> compressao;
contador = 0;
scanf("%d", &n);
ll resposta = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &vetor[i]);
conjunto.insert(vetor[i]);
}
for (set<int>::iterator it = conjunto.begin(); it != conjunto.end();
it++) {
compressao[*it] = ++contador;
}
for (int i = 0; i <= n; i++) bit[i] = 0;
for (int i = 1; i <= n; i++) {
resposta += sum(compressao[vetor[i]] - 1);
update(compressao[vetor[i]], ll(vetor[i]));
}
printf("%lld\n", resposta);
}
return 0;
}