#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
using ll = long long;
const int N = 300'000 + 10;
struct FenWick {
int n_node = 0;
vector<int> tree = {};
void resize(int n) { n_node = n; tree.resize(n + 10, 0); }
FenWick() {};
FenWick(int n) { this -> resize(n); };
void Update(int idx, int val) {
for( ; idx <= n_node; idx += (idx & - idx)) tree[idx] += val;
return;
}
int Query(int idx) {
int res = 0;
for( ; idx > 0; idx -= (idx & - idx)) res += tree[idx];
return res;
}
int Sum_to(int l, int r) {
if(l > r) return 0;
return Query(r) - Query(l - 1);
}
int kth(int val) {
int res = 0, s = 0;
for(int mask = 30; mask >= 0; -- mask) {
int nxt = res | (1 << mask);
if(nxt < n_node && s + tree[nxt] < val) {
res = nxt;
s += tree[nxt];
}
}
++ res; return res;
}
void reset(int idx) {
for ( ; idx <= n_node; idx += (idx & - idx)) tree[idx] = 0;
return;
}
};
int n;
ll a[N];
int L[N], R[N];
void compress() {
vector<ll> Pos = {};
for (int i = 1; i <= n; ++ i) {
Pos.push_back(a[i]);
}
sort(Pos.begin(), Pos.end());
Pos.resize(unique(Pos.begin(), Pos.end()) - Pos.begin());
for (int i = 1; i <= n; ++ i) {
a[i] = lower_bound(Pos.begin(), Pos.end(), a[i]) - Pos.begin() + 1;
}
}
void calc(int cnt[]) {
FenWick bit(n);
for (int i = 1; i <= n; ++ i) {
cnt[i] = bit.Query(a[i]);
bit.Update(a[i] + 1, 1);
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++ i) cin >> a[i];
compress();
calc(L);
reverse(a + 1, a + 1 + n);
calc(R);
ll res = 0;
for (int i = 1; i <= n; ++ i) res += 1ll * L[i] * R[n - i + 1];
cout << res;
return (0 ^ 0);
}
/// Code by vuavisao
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
62 ms |
6864 KB |
Output is correct |
3 |
Correct |
70 ms |
6996 KB |
Output is correct |
4 |
Correct |
61 ms |
6868 KB |
Output is correct |
5 |
Correct |
61 ms |
6860 KB |
Output is correct |
6 |
Correct |
71 ms |
6868 KB |
Output is correct |
7 |
Correct |
64 ms |
6856 KB |
Output is correct |
8 |
Correct |
61 ms |
6944 KB |
Output is correct |
9 |
Correct |
70 ms |
6856 KB |
Output is correct |
10 |
Correct |
64 ms |
6852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
6868 KB |
Output is correct |
2 |
Correct |
44 ms |
6964 KB |
Output is correct |
3 |
Correct |
39 ms |
6864 KB |
Output is correct |
4 |
Correct |
44 ms |
6868 KB |
Output is correct |
5 |
Correct |
47 ms |
7152 KB |
Output is correct |
6 |
Correct |
43 ms |
6864 KB |
Output is correct |
7 |
Correct |
41 ms |
6868 KB |
Output is correct |
8 |
Correct |
41 ms |
6868 KB |
Output is correct |
9 |
Correct |
39 ms |
6932 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
6868 KB |
Output is correct |
2 |
Correct |
44 ms |
6964 KB |
Output is correct |
3 |
Correct |
39 ms |
6864 KB |
Output is correct |
4 |
Correct |
44 ms |
6868 KB |
Output is correct |
5 |
Correct |
47 ms |
7152 KB |
Output is correct |
6 |
Correct |
43 ms |
6864 KB |
Output is correct |
7 |
Correct |
41 ms |
6868 KB |
Output is correct |
8 |
Correct |
41 ms |
6868 KB |
Output is correct |
9 |
Correct |
39 ms |
6932 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
52 ms |
6868 KB |
Output is correct |
12 |
Correct |
50 ms |
6868 KB |
Output is correct |
13 |
Correct |
50 ms |
6864 KB |
Output is correct |
14 |
Correct |
51 ms |
6868 KB |
Output is correct |
15 |
Correct |
50 ms |
7036 KB |
Output is correct |
16 |
Correct |
52 ms |
6868 KB |
Output is correct |
17 |
Correct |
55 ms |
6864 KB |
Output is correct |
18 |
Correct |
39 ms |
6864 KB |
Output is correct |
19 |
Correct |
35 ms |
6868 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
856 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
860 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
780 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
6868 KB |
Output is correct |
2 |
Correct |
44 ms |
6964 KB |
Output is correct |
3 |
Correct |
39 ms |
6864 KB |
Output is correct |
4 |
Correct |
44 ms |
6868 KB |
Output is correct |
5 |
Correct |
47 ms |
7152 KB |
Output is correct |
6 |
Correct |
43 ms |
6864 KB |
Output is correct |
7 |
Correct |
41 ms |
6868 KB |
Output is correct |
8 |
Correct |
41 ms |
6868 KB |
Output is correct |
9 |
Correct |
39 ms |
6932 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
52 ms |
6868 KB |
Output is correct |
12 |
Correct |
50 ms |
6868 KB |
Output is correct |
13 |
Correct |
50 ms |
6864 KB |
Output is correct |
14 |
Correct |
51 ms |
6868 KB |
Output is correct |
15 |
Correct |
50 ms |
7036 KB |
Output is correct |
16 |
Correct |
52 ms |
6868 KB |
Output is correct |
17 |
Correct |
55 ms |
6864 KB |
Output is correct |
18 |
Correct |
39 ms |
6864 KB |
Output is correct |
19 |
Correct |
35 ms |
6868 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
106 ms |
7004 KB |
Output is correct |
22 |
Correct |
97 ms |
6868 KB |
Output is correct |
23 |
Correct |
94 ms |
6864 KB |
Output is correct |
24 |
Correct |
95 ms |
7060 KB |
Output is correct |
25 |
Correct |
94 ms |
6868 KB |
Output is correct |
26 |
Correct |
93 ms |
7056 KB |
Output is correct |
27 |
Correct |
97 ms |
6888 KB |
Output is correct |
28 |
Correct |
45 ms |
6864 KB |
Output is correct |
29 |
Correct |
43 ms |
6868 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
62 ms |
6864 KB |
Output is correct |
3 |
Correct |
70 ms |
6996 KB |
Output is correct |
4 |
Correct |
61 ms |
6868 KB |
Output is correct |
5 |
Correct |
61 ms |
6860 KB |
Output is correct |
6 |
Correct |
71 ms |
6868 KB |
Output is correct |
7 |
Correct |
64 ms |
6856 KB |
Output is correct |
8 |
Correct |
61 ms |
6944 KB |
Output is correct |
9 |
Correct |
70 ms |
6856 KB |
Output is correct |
10 |
Correct |
64 ms |
6852 KB |
Output is correct |
11 |
Correct |
45 ms |
6868 KB |
Output is correct |
12 |
Correct |
44 ms |
6964 KB |
Output is correct |
13 |
Correct |
39 ms |
6864 KB |
Output is correct |
14 |
Correct |
44 ms |
6868 KB |
Output is correct |
15 |
Correct |
47 ms |
7152 KB |
Output is correct |
16 |
Correct |
43 ms |
6864 KB |
Output is correct |
17 |
Correct |
41 ms |
6868 KB |
Output is correct |
18 |
Correct |
41 ms |
6868 KB |
Output is correct |
19 |
Correct |
39 ms |
6932 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
52 ms |
6868 KB |
Output is correct |
22 |
Correct |
50 ms |
6868 KB |
Output is correct |
23 |
Correct |
50 ms |
6864 KB |
Output is correct |
24 |
Correct |
51 ms |
6868 KB |
Output is correct |
25 |
Correct |
50 ms |
7036 KB |
Output is correct |
26 |
Correct |
52 ms |
6868 KB |
Output is correct |
27 |
Correct |
55 ms |
6864 KB |
Output is correct |
28 |
Correct |
39 ms |
6864 KB |
Output is correct |
29 |
Correct |
35 ms |
6868 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
4 ms |
860 KB |
Output is correct |
42 |
Correct |
3 ms |
856 KB |
Output is correct |
43 |
Correct |
4 ms |
860 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
4 ms |
860 KB |
Output is correct |
46 |
Correct |
4 ms |
860 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
3 ms |
860 KB |
Output is correct |
49 |
Correct |
2 ms |
780 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
106 ms |
7004 KB |
Output is correct |
52 |
Correct |
97 ms |
6868 KB |
Output is correct |
53 |
Correct |
94 ms |
6864 KB |
Output is correct |
54 |
Correct |
95 ms |
7060 KB |
Output is correct |
55 |
Correct |
94 ms |
6868 KB |
Output is correct |
56 |
Correct |
93 ms |
7056 KB |
Output is correct |
57 |
Correct |
97 ms |
6888 KB |
Output is correct |
58 |
Correct |
45 ms |
6864 KB |
Output is correct |
59 |
Correct |
43 ms |
6868 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
122 ms |
12380 KB |
Output is correct |
62 |
Correct |
129 ms |
12492 KB |
Output is correct |
63 |
Correct |
123 ms |
12504 KB |
Output is correct |
64 |
Correct |
124 ms |
12492 KB |
Output is correct |
65 |
Correct |
127 ms |
12472 KB |
Output is correct |
66 |
Correct |
122 ms |
12388 KB |
Output is correct |
67 |
Correct |
116 ms |
12488 KB |
Output is correct |
68 |
Correct |
116 ms |
12488 KB |
Output is correct |
69 |
Correct |
105 ms |
8916 KB |
Output is correct |
70 |
Correct |
0 ms |
344 KB |
Output is correct |