-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practicesegmenttree3a-inversions.cpp
59 lines (49 loc) · 1.5 KB
/
Practicesegmenttree3a-inversions.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
#include <cstdio>
#include <vector>
typedef long long ll;
struct segtree{
ll sz; std::vector<ll> f;
void init(ll num){
sz = 1; while(sz < num){sz *= 2;}
f.assign(2 * sz, 0LL);
}
void set(ll idx){set(idx, 0, 0, sz);}
void set(ll idx, ll node, ll lv, ll rv){
//printf("idx:%lld node:%lld lv:%lld rv:%lld\n", idx, node, lv, rv);
if(rv == lv + 1){f[node] = 1; return;}
ll mid = (lv + rv) / 2;
if(idx < mid){set(idx, 2 * node + 1, lv, mid);}
else{set(idx, 2 * node + 2, mid, rv);}
f[node] = f[2 * node + 1] + f[2 * node + 2];
}
ll getInversions(ll pos){return getInversions(pos, 0, 0, sz);}
ll getInversions(ll pos, ll node, ll lv, ll rv){
if(rv <= pos){return 0LL;}
else if(pos <= lv){return f[node];}
else{
ll mid = (lv + rv) / 2;
ll leftSum = getInversions(pos, 2 * node + 1, lv, mid);
ll rightSum = getInversions(pos, 2 * node + 2, mid, rv);
return (leftSum + rightSum);
}
return 0LL;
}
void print(){
puts("===================");
for(ll p = 0; p < 2 * sz; p++){printf("%lld:%lld\n",p, f[p]);}
puts("===================");
return;
}
};
int main(){
ll n; scanf("%lld", &n);
segtree st; st.init(n + 1);
for(ll p = 1; p <= n; p++){
ll x; scanf("%lld", &x);
printf("%lld ", st.getInversions(x));
st.set(x);
//st.print();
}
puts("");
return 0;
}