#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x))
using namespace std;
struct Fenwick {
vector <int> aib;
int n;
Fenwick(int _n = 0) {
n = _n;
aib.resize(n + 1);
}
inline void init(int _n) {
n = _n;
aib.resize(n + 1);
}
inline void update(int pos, int val) {
for(int i = pos; i <= n; i += lsb(i)) {
aib[i] += val;
}
}
inline int query(int pos) {
int ans = 0;
for(int i = pos; i > 0; i -= lsb(i)) {
ans += aib[i];
}
return ans;
}
inline int sum(int l, int r) {
return query(r) - query(l - 1);
}
};
int main() {
#ifdef HOME
ifstream cin("C.in");
ofstream cout("C.out");
#endif
int i, n;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
vector<pair<ll, int>> h(n);
for(i = 0; i < n; i++) {
cin >> h[i].first;
h[i].second = i + 1;
}
sort(h.begin(), h.end());
Fenwick fen(n);
ll ans = 0;
i = 0;
while(i < n) {
int j = i;
while(j < n && h[i].first == h[j].first) {
int cur = fen.query(h[j].second);
ans += 1LL * (i - cur) * cur;
j++;
}
while(i < j) {
fen.update(h[i].second, 1);
i++;
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
89 ms |
11896 KB |
Output is correct |
3 |
Correct |
86 ms |
11768 KB |
Output is correct |
4 |
Correct |
97 ms |
11768 KB |
Output is correct |
5 |
Correct |
87 ms |
11768 KB |
Output is correct |
6 |
Correct |
93 ms |
11768 KB |
Output is correct |
7 |
Correct |
84 ms |
11768 KB |
Output is correct |
8 |
Correct |
85 ms |
11768 KB |
Output is correct |
9 |
Correct |
92 ms |
11784 KB |
Output is correct |
10 |
Correct |
95 ms |
11768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6776 KB |
Output is correct |
2 |
Correct |
68 ms |
6776 KB |
Output is correct |
3 |
Correct |
63 ms |
6776 KB |
Output is correct |
4 |
Correct |
61 ms |
6776 KB |
Output is correct |
5 |
Correct |
63 ms |
6752 KB |
Output is correct |
6 |
Correct |
63 ms |
6904 KB |
Output is correct |
7 |
Correct |
60 ms |
6776 KB |
Output is correct |
8 |
Correct |
39 ms |
6776 KB |
Output is correct |
9 |
Correct |
42 ms |
6776 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6776 KB |
Output is correct |
2 |
Correct |
68 ms |
6776 KB |
Output is correct |
3 |
Correct |
63 ms |
6776 KB |
Output is correct |
4 |
Correct |
61 ms |
6776 KB |
Output is correct |
5 |
Correct |
63 ms |
6752 KB |
Output is correct |
6 |
Correct |
63 ms |
6904 KB |
Output is correct |
7 |
Correct |
60 ms |
6776 KB |
Output is correct |
8 |
Correct |
39 ms |
6776 KB |
Output is correct |
9 |
Correct |
42 ms |
6776 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
72 ms |
7032 KB |
Output is correct |
12 |
Correct |
71 ms |
7032 KB |
Output is correct |
13 |
Correct |
72 ms |
7036 KB |
Output is correct |
14 |
Correct |
71 ms |
7032 KB |
Output is correct |
15 |
Correct |
71 ms |
7032 KB |
Output is correct |
16 |
Correct |
71 ms |
7032 KB |
Output is correct |
17 |
Correct |
74 ms |
7032 KB |
Output is correct |
18 |
Correct |
45 ms |
7120 KB |
Output is correct |
19 |
Correct |
49 ms |
7032 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
768 KB |
Output is correct |
12 |
Correct |
8 ms |
768 KB |
Output is correct |
13 |
Correct |
8 ms |
768 KB |
Output is correct |
14 |
Correct |
8 ms |
768 KB |
Output is correct |
15 |
Correct |
8 ms |
768 KB |
Output is correct |
16 |
Correct |
8 ms |
768 KB |
Output is correct |
17 |
Correct |
8 ms |
768 KB |
Output is correct |
18 |
Correct |
8 ms |
768 KB |
Output is correct |
19 |
Correct |
7 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6776 KB |
Output is correct |
2 |
Correct |
68 ms |
6776 KB |
Output is correct |
3 |
Correct |
63 ms |
6776 KB |
Output is correct |
4 |
Correct |
61 ms |
6776 KB |
Output is correct |
5 |
Correct |
63 ms |
6752 KB |
Output is correct |
6 |
Correct |
63 ms |
6904 KB |
Output is correct |
7 |
Correct |
60 ms |
6776 KB |
Output is correct |
8 |
Correct |
39 ms |
6776 KB |
Output is correct |
9 |
Correct |
42 ms |
6776 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
72 ms |
7032 KB |
Output is correct |
12 |
Correct |
71 ms |
7032 KB |
Output is correct |
13 |
Correct |
72 ms |
7036 KB |
Output is correct |
14 |
Correct |
71 ms |
7032 KB |
Output is correct |
15 |
Correct |
71 ms |
7032 KB |
Output is correct |
16 |
Correct |
71 ms |
7032 KB |
Output is correct |
17 |
Correct |
74 ms |
7032 KB |
Output is correct |
18 |
Correct |
45 ms |
7120 KB |
Output is correct |
19 |
Correct |
49 ms |
7032 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
83 ms |
7928 KB |
Output is correct |
22 |
Correct |
83 ms |
7928 KB |
Output is correct |
23 |
Correct |
86 ms |
7928 KB |
Output is correct |
24 |
Correct |
85 ms |
7932 KB |
Output is correct |
25 |
Correct |
84 ms |
7928 KB |
Output is correct |
26 |
Correct |
84 ms |
7948 KB |
Output is correct |
27 |
Correct |
84 ms |
7928 KB |
Output is correct |
28 |
Correct |
60 ms |
7928 KB |
Output is correct |
29 |
Correct |
58 ms |
7928 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
89 ms |
11896 KB |
Output is correct |
3 |
Correct |
86 ms |
11768 KB |
Output is correct |
4 |
Correct |
97 ms |
11768 KB |
Output is correct |
5 |
Correct |
87 ms |
11768 KB |
Output is correct |
6 |
Correct |
93 ms |
11768 KB |
Output is correct |
7 |
Correct |
84 ms |
11768 KB |
Output is correct |
8 |
Correct |
85 ms |
11768 KB |
Output is correct |
9 |
Correct |
92 ms |
11784 KB |
Output is correct |
10 |
Correct |
95 ms |
11768 KB |
Output is correct |
11 |
Correct |
59 ms |
6776 KB |
Output is correct |
12 |
Correct |
68 ms |
6776 KB |
Output is correct |
13 |
Correct |
63 ms |
6776 KB |
Output is correct |
14 |
Correct |
61 ms |
6776 KB |
Output is correct |
15 |
Correct |
63 ms |
6752 KB |
Output is correct |
16 |
Correct |
63 ms |
6904 KB |
Output is correct |
17 |
Correct |
60 ms |
6776 KB |
Output is correct |
18 |
Correct |
39 ms |
6776 KB |
Output is correct |
19 |
Correct |
42 ms |
6776 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
72 ms |
7032 KB |
Output is correct |
22 |
Correct |
71 ms |
7032 KB |
Output is correct |
23 |
Correct |
72 ms |
7036 KB |
Output is correct |
24 |
Correct |
71 ms |
7032 KB |
Output is correct |
25 |
Correct |
71 ms |
7032 KB |
Output is correct |
26 |
Correct |
71 ms |
7032 KB |
Output is correct |
27 |
Correct |
74 ms |
7032 KB |
Output is correct |
28 |
Correct |
45 ms |
7120 KB |
Output is correct |
29 |
Correct |
49 ms |
7032 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
8 ms |
768 KB |
Output is correct |
42 |
Correct |
8 ms |
768 KB |
Output is correct |
43 |
Correct |
8 ms |
768 KB |
Output is correct |
44 |
Correct |
8 ms |
768 KB |
Output is correct |
45 |
Correct |
8 ms |
768 KB |
Output is correct |
46 |
Correct |
8 ms |
768 KB |
Output is correct |
47 |
Correct |
8 ms |
768 KB |
Output is correct |
48 |
Correct |
8 ms |
768 KB |
Output is correct |
49 |
Correct |
7 ms |
640 KB |
Output is correct |
50 |
Correct |
4 ms |
384 KB |
Output is correct |
51 |
Correct |
83 ms |
7928 KB |
Output is correct |
52 |
Correct |
83 ms |
7928 KB |
Output is correct |
53 |
Correct |
86 ms |
7928 KB |
Output is correct |
54 |
Correct |
85 ms |
7932 KB |
Output is correct |
55 |
Correct |
84 ms |
7928 KB |
Output is correct |
56 |
Correct |
84 ms |
7948 KB |
Output is correct |
57 |
Correct |
84 ms |
7928 KB |
Output is correct |
58 |
Correct |
60 ms |
7928 KB |
Output is correct |
59 |
Correct |
58 ms |
7928 KB |
Output is correct |
60 |
Correct |
5 ms |
384 KB |
Output is correct |
61 |
Correct |
111 ms |
11768 KB |
Output is correct |
62 |
Correct |
112 ms |
11768 KB |
Output is correct |
63 |
Correct |
116 ms |
11768 KB |
Output is correct |
64 |
Correct |
110 ms |
11768 KB |
Output is correct |
65 |
Correct |
110 ms |
11768 KB |
Output is correct |
66 |
Correct |
117 ms |
11768 KB |
Output is correct |
67 |
Correct |
112 ms |
11768 KB |
Output is correct |
68 |
Correct |
114 ms |
11768 KB |
Output is correct |
69 |
Correct |
92 ms |
8056 KB |
Output is correct |
70 |
Correct |
5 ms |
384 KB |
Output is correct |