-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61E.cpp
More file actions
85 lines (77 loc) · 1.42 KB
/
Copy path61E.cpp
File metadata and controls
85 lines (77 loc) · 1.42 KB
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
int data,pos;
};
bool cmp(const node &a, const node &b){
return a.data < b.data;
}
struct Solution{
int n;
vector<int> a, p;
vector<node> keyv;
vector<ll> d, ansl, ansr;
int lowbit(int x){return x & -x;}
void update(int x) {
while(x <= n) {
d[x]++;
x += lowbit(x);
}
}
ll gsum(int x) {
ll sum = 0;
while(x) {
sum += d[x];
x -= lowbit(x);
}
return sum;
}
void Solve(){
scanf("%d", &n);
a.resize(n + 1);
keyv.resize(n + 1);
p.resize(n + 1);
ansl.resize(n + 1);
ansr.resize(n + 1);
d.resize(n + 1);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
keyv[i].data = a[i];
keyv[i].pos = i;
}
sort(keyv.begin() + 1, keyv.end(), cmp);
int id = 1;
p[keyv[1].pos] = 1;
for(int i = 2; i <= n; i++) {
if(keyv[i].data == keyv[i - 1].data) {
p[keyv[i].pos] = id;
}
else {
p[keyv[i].pos] = ++id;
}
}
for(int i = 1; i <= id; i++) {
update(p[i]);
ansl[i] = i - gsum(p[i]);
}
reverse(p.begin() + 1, p.end());
d = vector<ll> (n + 1);
for(int i = 1; i <= n; i++) {
d[i] = 0;
p[i] = id + 1 - p[i];
}
for(int i = 1; i <= n; i++) {
update(p[i]);
ansr[i] = i - gsum(p[i]);
}
reverse(ansr.begin() + 1, ansr.end());
ll ans = 0;
for(int i = 1; i <= n; i++) ans += ansl[i] * ansr[i];
printf("%lld\n", ans);
}
};
int main(){
Solution().Solve();
return 0;
}