#include<bits/stdc++.h>
using namespace std;
struct node{
int s, e, m, cnt;
node *l, *r;
node(int _s, int _e): s(_s), e(_e), m((_s+_e)/2), cnt(0){
if(s == e) return;
l = new node(s, m);
r = new node(m+1, e);
}
void update(int x, int v){
cnt += v;
if(s == e) return;
if(x <= m) l->update(x, v);
else r->update(x, v);
}
int query(int x, int y){
if(x <= s && y >= e) return cnt;
int ret = 0;
if(x <= m) ret += l->query(x, y);
if(y >= m+1) ret += r->query(x, y);
return ret;
}
void clear(){
cnt = 0;
if(s == e) return;
l->clear();
r->clear();
}
};
long long leftof[300001], rightof[300001];
pair<long long, int> distinctArr[300001];
int cnt = 1;
long long solve(int n, long long H[]){
for(int i = 1; i <= n; i++) distinctArr[i] = {H[i], i};
sort(distinctArr + 1, distinctArr + n + 1);
for(int i = 1; i <= n; i++){
if (i > 1 && distinctArr[i].first != distinctArr[i-1].first) cnt++;
H[distinctArr[i].second] = cnt;
}
node *root = new node(0, n);
for(int i = 1; i <= n; i++){
leftof[i] = root->query(0, H[i] - 1);
root->update(H[i], 1);
}
root->clear();
for(int i = n; i > 0; i--){
rightof[i] = root->query(0, H[i] - 1);
root->update(H[i], 1);
}
long long total = 0;
for(int i = 1; i <= n; i++){
total += leftof[i] * rightof[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 |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
186 ms |
45652 KB |
Output is correct |
3 |
Correct |
190 ms |
45872 KB |
Output is correct |
4 |
Correct |
188 ms |
45672 KB |
Output is correct |
5 |
Correct |
185 ms |
45680 KB |
Output is correct |
6 |
Correct |
191 ms |
45808 KB |
Output is correct |
7 |
Correct |
191 ms |
45900 KB |
Output is correct |
8 |
Correct |
186 ms |
45776 KB |
Output is correct |
9 |
Correct |
193 ms |
45788 KB |
Output is correct |
10 |
Correct |
190 ms |
45800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
40756 KB |
Output is correct |
2 |
Correct |
164 ms |
40848 KB |
Output is correct |
3 |
Correct |
157 ms |
40760 KB |
Output is correct |
4 |
Correct |
155 ms |
40760 KB |
Output is correct |
5 |
Correct |
154 ms |
40812 KB |
Output is correct |
6 |
Correct |
156 ms |
40908 KB |
Output is correct |
7 |
Correct |
152 ms |
40780 KB |
Output is correct |
8 |
Correct |
137 ms |
40752 KB |
Output is correct |
9 |
Correct |
134 ms |
40728 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
40756 KB |
Output is correct |
2 |
Correct |
164 ms |
40848 KB |
Output is correct |
3 |
Correct |
157 ms |
40760 KB |
Output is correct |
4 |
Correct |
155 ms |
40760 KB |
Output is correct |
5 |
Correct |
154 ms |
40812 KB |
Output is correct |
6 |
Correct |
156 ms |
40908 KB |
Output is correct |
7 |
Correct |
152 ms |
40780 KB |
Output is correct |
8 |
Correct |
137 ms |
40752 KB |
Output is correct |
9 |
Correct |
134 ms |
40728 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
181 ms |
40964 KB |
Output is correct |
12 |
Correct |
185 ms |
41076 KB |
Output is correct |
13 |
Correct |
181 ms |
40980 KB |
Output is correct |
14 |
Correct |
179 ms |
41036 KB |
Output is correct |
15 |
Correct |
182 ms |
41064 KB |
Output is correct |
16 |
Correct |
177 ms |
41080 KB |
Output is correct |
17 |
Correct |
176 ms |
41036 KB |
Output is correct |
18 |
Correct |
134 ms |
41056 KB |
Output is correct |
19 |
Correct |
134 ms |
41008 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
9 ms |
4180 KB |
Output is correct |
12 |
Correct |
10 ms |
4104 KB |
Output is correct |
13 |
Correct |
9 ms |
4064 KB |
Output is correct |
14 |
Correct |
9 ms |
4012 KB |
Output is correct |
15 |
Correct |
9 ms |
4016 KB |
Output is correct |
16 |
Correct |
10 ms |
4012 KB |
Output is correct |
17 |
Correct |
9 ms |
4052 KB |
Output is correct |
18 |
Correct |
8 ms |
4052 KB |
Output is correct |
19 |
Correct |
7 ms |
3972 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
40756 KB |
Output is correct |
2 |
Correct |
164 ms |
40848 KB |
Output is correct |
3 |
Correct |
157 ms |
40760 KB |
Output is correct |
4 |
Correct |
155 ms |
40760 KB |
Output is correct |
5 |
Correct |
154 ms |
40812 KB |
Output is correct |
6 |
Correct |
156 ms |
40908 KB |
Output is correct |
7 |
Correct |
152 ms |
40780 KB |
Output is correct |
8 |
Correct |
137 ms |
40752 KB |
Output is correct |
9 |
Correct |
134 ms |
40728 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
181 ms |
40964 KB |
Output is correct |
12 |
Correct |
185 ms |
41076 KB |
Output is correct |
13 |
Correct |
181 ms |
40980 KB |
Output is correct |
14 |
Correct |
179 ms |
41036 KB |
Output is correct |
15 |
Correct |
182 ms |
41064 KB |
Output is correct |
16 |
Correct |
177 ms |
41080 KB |
Output is correct |
17 |
Correct |
176 ms |
41036 KB |
Output is correct |
18 |
Correct |
134 ms |
41056 KB |
Output is correct |
19 |
Correct |
134 ms |
41008 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
300 ms |
42064 KB |
Output is correct |
22 |
Correct |
304 ms |
41860 KB |
Output is correct |
23 |
Correct |
294 ms |
41944 KB |
Output is correct |
24 |
Correct |
304 ms |
41908 KB |
Output is correct |
25 |
Correct |
295 ms |
41936 KB |
Output is correct |
26 |
Correct |
325 ms |
41908 KB |
Output is correct |
27 |
Correct |
327 ms |
42004 KB |
Output is correct |
28 |
Correct |
159 ms |
41932 KB |
Output is correct |
29 |
Correct |
156 ms |
41956 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2676 KB |
Output is correct |
2 |
Correct |
186 ms |
45652 KB |
Output is correct |
3 |
Correct |
190 ms |
45872 KB |
Output is correct |
4 |
Correct |
188 ms |
45672 KB |
Output is correct |
5 |
Correct |
185 ms |
45680 KB |
Output is correct |
6 |
Correct |
191 ms |
45808 KB |
Output is correct |
7 |
Correct |
191 ms |
45900 KB |
Output is correct |
8 |
Correct |
186 ms |
45776 KB |
Output is correct |
9 |
Correct |
193 ms |
45788 KB |
Output is correct |
10 |
Correct |
190 ms |
45800 KB |
Output is correct |
11 |
Correct |
152 ms |
40756 KB |
Output is correct |
12 |
Correct |
164 ms |
40848 KB |
Output is correct |
13 |
Correct |
157 ms |
40760 KB |
Output is correct |
14 |
Correct |
155 ms |
40760 KB |
Output is correct |
15 |
Correct |
154 ms |
40812 KB |
Output is correct |
16 |
Correct |
156 ms |
40908 KB |
Output is correct |
17 |
Correct |
152 ms |
40780 KB |
Output is correct |
18 |
Correct |
137 ms |
40752 KB |
Output is correct |
19 |
Correct |
134 ms |
40728 KB |
Output is correct |
20 |
Correct |
1 ms |
2644 KB |
Output is correct |
21 |
Correct |
181 ms |
40964 KB |
Output is correct |
22 |
Correct |
185 ms |
41076 KB |
Output is correct |
23 |
Correct |
181 ms |
40980 KB |
Output is correct |
24 |
Correct |
179 ms |
41036 KB |
Output is correct |
25 |
Correct |
182 ms |
41064 KB |
Output is correct |
26 |
Correct |
177 ms |
41080 KB |
Output is correct |
27 |
Correct |
176 ms |
41036 KB |
Output is correct |
28 |
Correct |
134 ms |
41056 KB |
Output is correct |
29 |
Correct |
134 ms |
41008 KB |
Output is correct |
30 |
Correct |
1 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2680 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
1 ms |
2644 KB |
Output is correct |
38 |
Correct |
2 ms |
2644 KB |
Output is correct |
39 |
Correct |
2 ms |
2644 KB |
Output is correct |
40 |
Correct |
2 ms |
2644 KB |
Output is correct |
41 |
Correct |
9 ms |
4180 KB |
Output is correct |
42 |
Correct |
10 ms |
4104 KB |
Output is correct |
43 |
Correct |
9 ms |
4064 KB |
Output is correct |
44 |
Correct |
9 ms |
4012 KB |
Output is correct |
45 |
Correct |
9 ms |
4016 KB |
Output is correct |
46 |
Correct |
10 ms |
4012 KB |
Output is correct |
47 |
Correct |
9 ms |
4052 KB |
Output is correct |
48 |
Correct |
8 ms |
4052 KB |
Output is correct |
49 |
Correct |
7 ms |
3972 KB |
Output is correct |
50 |
Correct |
1 ms |
2644 KB |
Output is correct |
51 |
Correct |
300 ms |
42064 KB |
Output is correct |
52 |
Correct |
304 ms |
41860 KB |
Output is correct |
53 |
Correct |
294 ms |
41944 KB |
Output is correct |
54 |
Correct |
304 ms |
41908 KB |
Output is correct |
55 |
Correct |
295 ms |
41936 KB |
Output is correct |
56 |
Correct |
325 ms |
41908 KB |
Output is correct |
57 |
Correct |
327 ms |
42004 KB |
Output is correct |
58 |
Correct |
159 ms |
41932 KB |
Output is correct |
59 |
Correct |
156 ms |
41956 KB |
Output is correct |
60 |
Correct |
1 ms |
2644 KB |
Output is correct |
61 |
Correct |
448 ms |
45756 KB |
Output is correct |
62 |
Correct |
434 ms |
45768 KB |
Output is correct |
63 |
Correct |
436 ms |
45788 KB |
Output is correct |
64 |
Correct |
427 ms |
45772 KB |
Output is correct |
65 |
Correct |
433 ms |
45724 KB |
Output is correct |
66 |
Correct |
436 ms |
45768 KB |
Output is correct |
67 |
Correct |
466 ms |
45776 KB |
Output is correct |
68 |
Correct |
380 ms |
45948 KB |
Output is correct |
69 |
Correct |
337 ms |
42100 KB |
Output is correct |
70 |
Correct |
1 ms |
2644 KB |
Output is correct |