-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPower Segment Tree.cpp
54 lines (51 loc) · 1.35 KB
/
Power Segment Tree.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
#include <iostream>
#include <unordered_map>
#include <algorithm>
#define push_up(rt) cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
#define endl "\n"
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n, cnt[N << 2], a[N], b[N];
unordered_map<int, int> Map;
inline int query(int rt, int l, int r, int x, int y){
if(l == x && y == r){
return cnt[rt];
}
int mid = (l + r) >> 1;
if(y <= mid) return query(rt << 1, l, mid, x, y);
else if(x > mid) return query(rt << 1 | 1, mid + 1, r, x, y);
else return query(rt << 1, l, mid, x, mid) + query(rt << 1 | 1, mid + 1, r, mid + 1, y);
}
inline void update(int rt, int l, int r, int pos, int val){
if(l == r){
cnt[rt] += val;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(rt << 1, l, mid, pos, val);
else update(rt << 1 | 1, mid + 1, r, pos, val);
push_up(rt);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
ll ans = 0;
for(int i = 1;i<=n;i++){
cin>>a[i];
b[i] = a[i];
}
sort(a + 1, a + n + 1);
int len = 0;
for(int i = 1;i<=n;i++){
if(Map[a[i]] == 0) Map[a[i]] = ++len;
}
for(int i = 1;i<=n;i++){
int x = Map[b[i]];
ans += query(1, 1, n + 1, x + 1, n + 1);
update(1, 1, n, x, 1);
}
cout<<ans<<endl;
return 0;
}