-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathE. Enemy is weak_BIT.cpp
47 lines (43 loc) · 973 Bytes
/
E. Enemy is weak_BIT.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
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const int NMAX = 1e6 + 5;
int arr[NMAX];
int cc[NMAX];
int f1[NMAX];
ll f2[NMAX];
template <class T>
void upd(T* f, int index, int val, int N) {
for (int i = index + 1; i <= N; i += i & (-i)) {
f[i] += val;
}
}
template <class T>
T get(T* f, int index) {
T sum = 0;
for (int i = index + 1; i >= 1; i -= i & (-i)) {
sum += f[i];
}
return sum;
}
int main(int argc, char const* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
copy(begin(arr), begin(arr) + n, cc);
sort(begin(cc), begin(cc) + n);
ll ret = 0;
for (int i = n - 1; i >= 0; i--) {
int index = lower_bound(begin(cc), begin(cc) + n, arr[i]) - begin(cc);
// cout << index << "\n";
ret += get(f2, index - 1);
upd(f2, index, get(f1, index - 1), n);
upd(f1, index, 1, n);
}
cout << ret << "\n";
return 0;
}