#include<bits/stdc++.h>
using namespace std;
int fw[300001], cnt = 1;
void update(int x, int v){
for(; x <= cnt; x += x&-x) fw[x] += v;
}
int query(int x){
int ret = 0;
for(; x; x -= x&-x) ret += fw[x];
return ret;
}
long long invLeft[300001], invRight[300001];
pair<long long, int> disArr[300001];
long long solve(int n, long long H[]){
for(int i = 1; i <= n; i++) disArr[i] = {H[i], i};
sort(disArr + 1, disArr + n + 1);
for(int i = 1; i <= n; i++){
if (i > 1 && disArr[i].first != disArr[i-1].first) cnt++;
H[disArr[i].second] = cnt;
}
for(int i = 1; i <= n; i++){
invLeft[i] = query(H[i] - 1);
update(H[i], 1);
}
fill(fw, fw + n + 1, 0);
for(int i = n; i > 0; i--){
invRight[i] = query(H[i] - 1);
update(H[i], 1);
}
long long total = 0;
for(int i = 1; i <= n; i++){
total += invLeft[i] * invRight[i];
}
return total;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
long long H[300001];
cin >> n;
for(int i = 1; i <= n; i++) cin >> H[i];
long long total = solve(n, H);
cout << total;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
106 ms |
13304 KB |
Output is correct |
3 |
Correct |
105 ms |
13176 KB |
Output is correct |
4 |
Correct |
100 ms |
13176 KB |
Output is correct |
5 |
Correct |
120 ms |
13304 KB |
Output is correct |
6 |
Correct |
106 ms |
13304 KB |
Output is correct |
7 |
Correct |
101 ms |
13176 KB |
Output is correct |
8 |
Correct |
109 ms |
13304 KB |
Output is correct |
9 |
Correct |
103 ms |
13280 KB |
Output is correct |
10 |
Correct |
120 ms |
13228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
13304 KB |
Output is correct |
2 |
Correct |
67 ms |
13192 KB |
Output is correct |
3 |
Correct |
70 ms |
13280 KB |
Output is correct |
4 |
Correct |
64 ms |
13280 KB |
Output is correct |
5 |
Correct |
78 ms |
13304 KB |
Output is correct |
6 |
Correct |
62 ms |
13304 KB |
Output is correct |
7 |
Correct |
59 ms |
13304 KB |
Output is correct |
8 |
Correct |
46 ms |
13176 KB |
Output is correct |
9 |
Correct |
56 ms |
13304 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
13304 KB |
Output is correct |
2 |
Correct |
67 ms |
13192 KB |
Output is correct |
3 |
Correct |
70 ms |
13280 KB |
Output is correct |
4 |
Correct |
64 ms |
13280 KB |
Output is correct |
5 |
Correct |
78 ms |
13304 KB |
Output is correct |
6 |
Correct |
62 ms |
13304 KB |
Output is correct |
7 |
Correct |
59 ms |
13304 KB |
Output is correct |
8 |
Correct |
46 ms |
13176 KB |
Output is correct |
9 |
Correct |
56 ms |
13304 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
87 ms |
13284 KB |
Output is correct |
12 |
Correct |
147 ms |
13280 KB |
Output is correct |
13 |
Correct |
80 ms |
13232 KB |
Output is correct |
14 |
Correct |
88 ms |
13176 KB |
Output is correct |
15 |
Correct |
75 ms |
13176 KB |
Output is correct |
16 |
Correct |
79 ms |
13176 KB |
Output is correct |
17 |
Correct |
77 ms |
13176 KB |
Output is correct |
18 |
Correct |
46 ms |
13176 KB |
Output is correct |
19 |
Correct |
46 ms |
13176 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
352 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
352 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
380 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
13 ms |
760 KB |
Output is correct |
12 |
Correct |
14 ms |
760 KB |
Output is correct |
13 |
Correct |
8 ms |
760 KB |
Output is correct |
14 |
Correct |
15 ms |
760 KB |
Output is correct |
15 |
Correct |
9 ms |
760 KB |
Output is correct |
16 |
Correct |
8 ms |
760 KB |
Output is correct |
17 |
Correct |
10 ms |
760 KB |
Output is correct |
18 |
Correct |
8 ms |
760 KB |
Output is correct |
19 |
Correct |
7 ms |
760 KB |
Output is correct |
20 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
13304 KB |
Output is correct |
2 |
Correct |
67 ms |
13192 KB |
Output is correct |
3 |
Correct |
70 ms |
13280 KB |
Output is correct |
4 |
Correct |
64 ms |
13280 KB |
Output is correct |
5 |
Correct |
78 ms |
13304 KB |
Output is correct |
6 |
Correct |
62 ms |
13304 KB |
Output is correct |
7 |
Correct |
59 ms |
13304 KB |
Output is correct |
8 |
Correct |
46 ms |
13176 KB |
Output is correct |
9 |
Correct |
56 ms |
13304 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
87 ms |
13284 KB |
Output is correct |
12 |
Correct |
147 ms |
13280 KB |
Output is correct |
13 |
Correct |
80 ms |
13232 KB |
Output is correct |
14 |
Correct |
88 ms |
13176 KB |
Output is correct |
15 |
Correct |
75 ms |
13176 KB |
Output is correct |
16 |
Correct |
79 ms |
13176 KB |
Output is correct |
17 |
Correct |
77 ms |
13176 KB |
Output is correct |
18 |
Correct |
46 ms |
13176 KB |
Output is correct |
19 |
Correct |
46 ms |
13176 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
98 ms |
13308 KB |
Output is correct |
22 |
Correct |
108 ms |
13176 KB |
Output is correct |
23 |
Correct |
98 ms |
13304 KB |
Output is correct |
24 |
Correct |
100 ms |
13176 KB |
Output is correct |
25 |
Correct |
100 ms |
13272 KB |
Output is correct |
26 |
Correct |
99 ms |
13176 KB |
Output is correct |
27 |
Correct |
98 ms |
13176 KB |
Output is correct |
28 |
Correct |
64 ms |
13176 KB |
Output is correct |
29 |
Correct |
65 ms |
13176 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
106 ms |
13304 KB |
Output is correct |
3 |
Correct |
105 ms |
13176 KB |
Output is correct |
4 |
Correct |
100 ms |
13176 KB |
Output is correct |
5 |
Correct |
120 ms |
13304 KB |
Output is correct |
6 |
Correct |
106 ms |
13304 KB |
Output is correct |
7 |
Correct |
101 ms |
13176 KB |
Output is correct |
8 |
Correct |
109 ms |
13304 KB |
Output is correct |
9 |
Correct |
103 ms |
13280 KB |
Output is correct |
10 |
Correct |
120 ms |
13228 KB |
Output is correct |
11 |
Correct |
60 ms |
13304 KB |
Output is correct |
12 |
Correct |
67 ms |
13192 KB |
Output is correct |
13 |
Correct |
70 ms |
13280 KB |
Output is correct |
14 |
Correct |
64 ms |
13280 KB |
Output is correct |
15 |
Correct |
78 ms |
13304 KB |
Output is correct |
16 |
Correct |
62 ms |
13304 KB |
Output is correct |
17 |
Correct |
59 ms |
13304 KB |
Output is correct |
18 |
Correct |
46 ms |
13176 KB |
Output is correct |
19 |
Correct |
56 ms |
13304 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
87 ms |
13284 KB |
Output is correct |
22 |
Correct |
147 ms |
13280 KB |
Output is correct |
23 |
Correct |
80 ms |
13232 KB |
Output is correct |
24 |
Correct |
88 ms |
13176 KB |
Output is correct |
25 |
Correct |
75 ms |
13176 KB |
Output is correct |
26 |
Correct |
79 ms |
13176 KB |
Output is correct |
27 |
Correct |
77 ms |
13176 KB |
Output is correct |
28 |
Correct |
46 ms |
13176 KB |
Output is correct |
29 |
Correct |
46 ms |
13176 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
352 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
380 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
38 |
Correct |
5 ms |
376 KB |
Output is correct |
39 |
Correct |
5 ms |
376 KB |
Output is correct |
40 |
Correct |
5 ms |
376 KB |
Output is correct |
41 |
Correct |
13 ms |
760 KB |
Output is correct |
42 |
Correct |
14 ms |
760 KB |
Output is correct |
43 |
Correct |
8 ms |
760 KB |
Output is correct |
44 |
Correct |
15 ms |
760 KB |
Output is correct |
45 |
Correct |
9 ms |
760 KB |
Output is correct |
46 |
Correct |
8 ms |
760 KB |
Output is correct |
47 |
Correct |
10 ms |
760 KB |
Output is correct |
48 |
Correct |
8 ms |
760 KB |
Output is correct |
49 |
Correct |
7 ms |
760 KB |
Output is correct |
50 |
Correct |
6 ms |
376 KB |
Output is correct |
51 |
Correct |
98 ms |
13308 KB |
Output is correct |
52 |
Correct |
108 ms |
13176 KB |
Output is correct |
53 |
Correct |
98 ms |
13304 KB |
Output is correct |
54 |
Correct |
100 ms |
13176 KB |
Output is correct |
55 |
Correct |
100 ms |
13272 KB |
Output is correct |
56 |
Correct |
99 ms |
13176 KB |
Output is correct |
57 |
Correct |
98 ms |
13176 KB |
Output is correct |
58 |
Correct |
64 ms |
13176 KB |
Output is correct |
59 |
Correct |
65 ms |
13176 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
124 ms |
13176 KB |
Output is correct |
62 |
Correct |
128 ms |
13176 KB |
Output is correct |
63 |
Correct |
126 ms |
13176 KB |
Output is correct |
64 |
Correct |
147 ms |
13176 KB |
Output is correct |
65 |
Correct |
130 ms |
13176 KB |
Output is correct |
66 |
Correct |
130 ms |
13176 KB |
Output is correct |
67 |
Correct |
130 ms |
13176 KB |
Output is correct |
68 |
Correct |
149 ms |
13176 KB |
Output is correct |
69 |
Correct |
97 ms |
13176 KB |
Output is correct |
70 |
Correct |
5 ms |
376 KB |
Output is correct |