-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy_Is_Weak.cpp
100 lines (89 loc) · 2.5 KB
/
Enemy_Is_Weak.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <limits>
using namespace std;
struct FenwickTree {
std::vector<long long> tree;
static const int MIN = 0;
static const int MAX = 1;
static const int SUM = 2;
// CHANGE ACCORDING TO TYPE OF FENWICK TREE
const int TYPE = SUM;
FenwickTree(std::vector<long long> &arr) {
tree.resize(arr.size()+1, 0);
if (TYPE != SUM) {
for (int i = 0; i < arr.size(); i++) {
FenwickTree::insert(i, arr[i]);
}
} else {
// TODO Change faster insertion
for (int i = 0; i < arr.size(); i++) {
FenwickTree::insert(i, arr[i]);
}
}
}
FenwickTree(long long size) {
tree.resize(size+1, 0);
}
void insert(int index, long long value) {
index++;
while(index < tree.size()) {
tree[index] = operate(tree[index], value);
index += index & (-index);
}
}
long long getVal(int index) {
index++;
long long total = MIN ? numeric_limits<long long int>::max() : 0;
while(index > 0) {
total = operate(tree[index], total);
index -= index & (-index);
}
return total;
}
long long operate(long long a, long long b) {
switch (TYPE)
{
case SUM:
return a + b;
case MIN:
return min(a, b);
case MAX:
return max(a, b);
}
cerr << "Invalid Fenwick Tree Type" << endl;
return -1;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<long long> arr (N);
vector<long long> sortedArr;
for (int i = 0; i < arr.size(); i++) {
cin >> arr[i];
}
sortedArr = arr;
sort(sortedArr.rbegin(), sortedArr.rend());
unordered_map<long long, long long> inverseMap;
for (int i = 0; i < N; i++) {
inverseMap[sortedArr[i]] = i;
}
FenwickTree tree (N);
FenwickTree tree2 (N);
long long total = 0;
for (int i = 0; i < N; i++) {
int index = inverseMap[arr[i]];
long long below = tree.getVal(index);
long long seconds = tree2.getVal(index);
total += seconds;
tree2.insert(index, below);
tree.insert(index, 1);
}
cout << total << '\n';
return 0;
}