/**
link: https://oj.uz/problem/view/NOI20_mountains
*/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define debug(x) cout << #x << " = " << x << endl;
using namespace std;
const int MAXN = 3e5 + 5;
ll n, A[MAXN], BIT[MAXN], tmp[MAXN], sz;
void upd(int idx){
for(int _idx = idx + 1; _idx < MAXN; _idx += bit(_idx)){
++ BIT[_idx];
}
}
ll qry(int idx){
ll ret = 0;
for(int _idx = idx + 1; _idx > 0; _idx -= bit(_idx)){
ret += BIT[_idx];
}
return ret;
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
cin >> n;
for(int i = 0; i < n; i ++){
cin >> A[i];
tmp[sz ++] = A[i];
}
sort(tmp, tmp + sz);
sz = unique(tmp, tmp + sz) - tmp;
for(int i = 0; i < n; i ++)
A[i] = lower_bound(tmp, tmp + sz, A[i]) - tmp;
ll L[MAXN] = {0}, R[MAXN] = {0};
for(int i = 0; i < n; i ++){
L[i] = qry(A[i] - 1);
upd(A[i]);
}
memset(BIT, 0, sizeof(BIT));
for(int i = n - 1; i >= 0; i --){
R[i] = qry(A[i] - 1);
upd(A[i]);
}
/// cout << "A: "; for(int i = 0; i < n; i ++) cout << A[i] << " "; cout << endl;
/// cout << "L: "; for(int i = 0; i < n; i ++) cout << L[i] << " "; cout << endl;
/// cout << "R: "; for(int i = 0; i < n; i ++) cout << R[i] << " "; cout << endl;
ll ans = 0;
for(int i = 0; i < n; i ++)
ans += L[i] * R[i];
cout << ans << endl;
}
/**
5
0 1 1 0 1
2
6
500 20 900 0 900 70
7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
122 ms |
17656 KB |
Output is correct |
3 |
Correct |
116 ms |
17656 KB |
Output is correct |
4 |
Correct |
120 ms |
17528 KB |
Output is correct |
5 |
Correct |
131 ms |
17648 KB |
Output is correct |
6 |
Correct |
124 ms |
17656 KB |
Output is correct |
7 |
Correct |
126 ms |
17656 KB |
Output is correct |
8 |
Correct |
120 ms |
17552 KB |
Output is correct |
9 |
Correct |
128 ms |
17656 KB |
Output is correct |
10 |
Correct |
126 ms |
17656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12664 KB |
Output is correct |
2 |
Correct |
72 ms |
12664 KB |
Output is correct |
3 |
Correct |
70 ms |
12664 KB |
Output is correct |
4 |
Correct |
70 ms |
12664 KB |
Output is correct |
5 |
Correct |
71 ms |
12668 KB |
Output is correct |
6 |
Correct |
71 ms |
12664 KB |
Output is correct |
7 |
Correct |
71 ms |
12664 KB |
Output is correct |
8 |
Correct |
67 ms |
12664 KB |
Output is correct |
9 |
Correct |
66 ms |
12792 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12664 KB |
Output is correct |
2 |
Correct |
72 ms |
12664 KB |
Output is correct |
3 |
Correct |
70 ms |
12664 KB |
Output is correct |
4 |
Correct |
70 ms |
12664 KB |
Output is correct |
5 |
Correct |
71 ms |
12668 KB |
Output is correct |
6 |
Correct |
71 ms |
12664 KB |
Output is correct |
7 |
Correct |
71 ms |
12664 KB |
Output is correct |
8 |
Correct |
67 ms |
12664 KB |
Output is correct |
9 |
Correct |
66 ms |
12792 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
84 ms |
12920 KB |
Output is correct |
12 |
Correct |
86 ms |
12920 KB |
Output is correct |
13 |
Correct |
85 ms |
12920 KB |
Output is correct |
14 |
Correct |
85 ms |
12920 KB |
Output is correct |
15 |
Correct |
85 ms |
12920 KB |
Output is correct |
16 |
Correct |
85 ms |
12860 KB |
Output is correct |
17 |
Correct |
84 ms |
12920 KB |
Output is correct |
18 |
Correct |
63 ms |
12920 KB |
Output is correct |
19 |
Correct |
64 ms |
12920 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
8 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
8 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
8 ms |
7424 KB |
Output is correct |
3 |
Correct |
8 ms |
7424 KB |
Output is correct |
4 |
Correct |
9 ms |
7424 KB |
Output is correct |
5 |
Correct |
8 ms |
7424 KB |
Output is correct |
6 |
Correct |
8 ms |
7424 KB |
Output is correct |
7 |
Correct |
8 ms |
7424 KB |
Output is correct |
8 |
Correct |
9 ms |
7424 KB |
Output is correct |
9 |
Correct |
8 ms |
7424 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
13 ms |
7680 KB |
Output is correct |
12 |
Correct |
13 ms |
7552 KB |
Output is correct |
13 |
Correct |
14 ms |
7680 KB |
Output is correct |
14 |
Correct |
13 ms |
7680 KB |
Output is correct |
15 |
Correct |
13 ms |
7680 KB |
Output is correct |
16 |
Correct |
14 ms |
7680 KB |
Output is correct |
17 |
Correct |
18 ms |
7936 KB |
Output is correct |
18 |
Correct |
13 ms |
7808 KB |
Output is correct |
19 |
Correct |
11 ms |
7552 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12664 KB |
Output is correct |
2 |
Correct |
72 ms |
12664 KB |
Output is correct |
3 |
Correct |
70 ms |
12664 KB |
Output is correct |
4 |
Correct |
70 ms |
12664 KB |
Output is correct |
5 |
Correct |
71 ms |
12668 KB |
Output is correct |
6 |
Correct |
71 ms |
12664 KB |
Output is correct |
7 |
Correct |
71 ms |
12664 KB |
Output is correct |
8 |
Correct |
67 ms |
12664 KB |
Output is correct |
9 |
Correct |
66 ms |
12792 KB |
Output is correct |
10 |
Correct |
8 ms |
7424 KB |
Output is correct |
11 |
Correct |
84 ms |
12920 KB |
Output is correct |
12 |
Correct |
86 ms |
12920 KB |
Output is correct |
13 |
Correct |
85 ms |
12920 KB |
Output is correct |
14 |
Correct |
85 ms |
12920 KB |
Output is correct |
15 |
Correct |
85 ms |
12920 KB |
Output is correct |
16 |
Correct |
85 ms |
12860 KB |
Output is correct |
17 |
Correct |
84 ms |
12920 KB |
Output is correct |
18 |
Correct |
63 ms |
12920 KB |
Output is correct |
19 |
Correct |
64 ms |
12920 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
21 |
Correct |
144 ms |
13816 KB |
Output is correct |
22 |
Correct |
144 ms |
13816 KB |
Output is correct |
23 |
Correct |
143 ms |
13816 KB |
Output is correct |
24 |
Correct |
145 ms |
13816 KB |
Output is correct |
25 |
Correct |
144 ms |
13816 KB |
Output is correct |
26 |
Correct |
143 ms |
13816 KB |
Output is correct |
27 |
Correct |
143 ms |
13820 KB |
Output is correct |
28 |
Correct |
87 ms |
13816 KB |
Output is correct |
29 |
Correct |
87 ms |
13816 KB |
Output is correct |
30 |
Correct |
8 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7424 KB |
Output is correct |
2 |
Correct |
122 ms |
17656 KB |
Output is correct |
3 |
Correct |
116 ms |
17656 KB |
Output is correct |
4 |
Correct |
120 ms |
17528 KB |
Output is correct |
5 |
Correct |
131 ms |
17648 KB |
Output is correct |
6 |
Correct |
124 ms |
17656 KB |
Output is correct |
7 |
Correct |
126 ms |
17656 KB |
Output is correct |
8 |
Correct |
120 ms |
17552 KB |
Output is correct |
9 |
Correct |
128 ms |
17656 KB |
Output is correct |
10 |
Correct |
126 ms |
17656 KB |
Output is correct |
11 |
Correct |
75 ms |
12664 KB |
Output is correct |
12 |
Correct |
72 ms |
12664 KB |
Output is correct |
13 |
Correct |
70 ms |
12664 KB |
Output is correct |
14 |
Correct |
70 ms |
12664 KB |
Output is correct |
15 |
Correct |
71 ms |
12668 KB |
Output is correct |
16 |
Correct |
71 ms |
12664 KB |
Output is correct |
17 |
Correct |
71 ms |
12664 KB |
Output is correct |
18 |
Correct |
67 ms |
12664 KB |
Output is correct |
19 |
Correct |
66 ms |
12792 KB |
Output is correct |
20 |
Correct |
8 ms |
7424 KB |
Output is correct |
21 |
Correct |
84 ms |
12920 KB |
Output is correct |
22 |
Correct |
86 ms |
12920 KB |
Output is correct |
23 |
Correct |
85 ms |
12920 KB |
Output is correct |
24 |
Correct |
85 ms |
12920 KB |
Output is correct |
25 |
Correct |
85 ms |
12920 KB |
Output is correct |
26 |
Correct |
85 ms |
12860 KB |
Output is correct |
27 |
Correct |
84 ms |
12920 KB |
Output is correct |
28 |
Correct |
63 ms |
12920 KB |
Output is correct |
29 |
Correct |
64 ms |
12920 KB |
Output is correct |
30 |
Correct |
8 ms |
7424 KB |
Output is correct |
31 |
Correct |
8 ms |
7424 KB |
Output is correct |
32 |
Correct |
8 ms |
7424 KB |
Output is correct |
33 |
Correct |
8 ms |
7424 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
8 ms |
7424 KB |
Output is correct |
36 |
Correct |
8 ms |
7424 KB |
Output is correct |
37 |
Correct |
8 ms |
7424 KB |
Output is correct |
38 |
Correct |
9 ms |
7424 KB |
Output is correct |
39 |
Correct |
8 ms |
7424 KB |
Output is correct |
40 |
Correct |
8 ms |
7424 KB |
Output is correct |
41 |
Correct |
13 ms |
7680 KB |
Output is correct |
42 |
Correct |
13 ms |
7552 KB |
Output is correct |
43 |
Correct |
14 ms |
7680 KB |
Output is correct |
44 |
Correct |
13 ms |
7680 KB |
Output is correct |
45 |
Correct |
13 ms |
7680 KB |
Output is correct |
46 |
Correct |
14 ms |
7680 KB |
Output is correct |
47 |
Correct |
18 ms |
7936 KB |
Output is correct |
48 |
Correct |
13 ms |
7808 KB |
Output is correct |
49 |
Correct |
11 ms |
7552 KB |
Output is correct |
50 |
Correct |
8 ms |
7424 KB |
Output is correct |
51 |
Correct |
144 ms |
13816 KB |
Output is correct |
52 |
Correct |
144 ms |
13816 KB |
Output is correct |
53 |
Correct |
143 ms |
13816 KB |
Output is correct |
54 |
Correct |
145 ms |
13816 KB |
Output is correct |
55 |
Correct |
144 ms |
13816 KB |
Output is correct |
56 |
Correct |
143 ms |
13816 KB |
Output is correct |
57 |
Correct |
143 ms |
13820 KB |
Output is correct |
58 |
Correct |
87 ms |
13816 KB |
Output is correct |
59 |
Correct |
87 ms |
13816 KB |
Output is correct |
60 |
Correct |
8 ms |
7424 KB |
Output is correct |
61 |
Correct |
192 ms |
17656 KB |
Output is correct |
62 |
Correct |
192 ms |
17784 KB |
Output is correct |
63 |
Correct |
184 ms |
17656 KB |
Output is correct |
64 |
Correct |
188 ms |
17784 KB |
Output is correct |
65 |
Correct |
188 ms |
17656 KB |
Output is correct |
66 |
Correct |
182 ms |
17656 KB |
Output is correct |
67 |
Correct |
185 ms |
17656 KB |
Output is correct |
68 |
Correct |
183 ms |
17656 KB |
Output is correct |
69 |
Correct |
148 ms |
13944 KB |
Output is correct |
70 |
Correct |
8 ms |
7424 KB |
Output is correct |