#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define MAXN 300000
int N, M;
LL H[MAXN + 3], answer;
int prefix[MAXN + 3], suffix[MAXN + 3]; //BIT
vector<LL> uniqH;
void update(int bit[], int idx, int val) {
while (idx <= M) {
bit[idx] += val;
idx += (idx & -idx);
}
}
int query(int bit[], int idx) {
int result = 0;
while (idx > 0) {
result += bit[idx];
idx -= (idx & -idx);
}
return result;
}
int id(LL h) {
return (lower_bound(uniqH.begin(), uniqH.end(), h) - uniqH.begin()) + 1;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &H[i]);
uniqH.push_back(H[i]);
}
sort(uniqH.begin(), uniqH.end());
uniqH.resize(distance(uniqH.begin(), unique(uniqH.begin(), uniqH.end())));
M = (int) uniqH.size();
memset(prefix, 0, sizeof(prefix));
memset(suffix, 0, sizeof(suffix));
for (int i = 2; i <= N; i++)
update(suffix, id(H[i]), 1);
answer = 0;
for (int i = 2; i < N; i++) {
update(prefix, id(H[i - 1]), 1);
update(suffix, id(H[i]), -1);
int numLeft = query(prefix, id(H[i]) - 1);
int numRight = query(suffix, id(H[i]) - 1);
answer += (LL) numLeft * numRight;
}
printf("%lld\n", answer);
}
Compilation message
Mountains.cpp: In function 'int main()':
Mountains.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
Mountains.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &H[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
190 ms |
13168 KB |
Output is correct |
3 |
Correct |
188 ms |
13024 KB |
Output is correct |
4 |
Correct |
185 ms |
13024 KB |
Output is correct |
5 |
Correct |
197 ms |
13148 KB |
Output is correct |
6 |
Correct |
187 ms |
13128 KB |
Output is correct |
7 |
Correct |
200 ms |
13064 KB |
Output is correct |
8 |
Correct |
184 ms |
13024 KB |
Output is correct |
9 |
Correct |
155 ms |
13024 KB |
Output is correct |
10 |
Correct |
155 ms |
13152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
8168 KB |
Output is correct |
2 |
Correct |
56 ms |
8164 KB |
Output is correct |
3 |
Correct |
55 ms |
8036 KB |
Output is correct |
4 |
Correct |
55 ms |
8036 KB |
Output is correct |
5 |
Correct |
56 ms |
8172 KB |
Output is correct |
6 |
Correct |
56 ms |
8036 KB |
Output is correct |
7 |
Correct |
57 ms |
8036 KB |
Output is correct |
8 |
Correct |
49 ms |
8032 KB |
Output is correct |
9 |
Correct |
51 ms |
8036 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
8168 KB |
Output is correct |
2 |
Correct |
56 ms |
8164 KB |
Output is correct |
3 |
Correct |
55 ms |
8036 KB |
Output is correct |
4 |
Correct |
55 ms |
8036 KB |
Output is correct |
5 |
Correct |
56 ms |
8172 KB |
Output is correct |
6 |
Correct |
56 ms |
8036 KB |
Output is correct |
7 |
Correct |
57 ms |
8036 KB |
Output is correct |
8 |
Correct |
49 ms |
8032 KB |
Output is correct |
9 |
Correct |
51 ms |
8036 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
11 |
Correct |
107 ms |
8288 KB |
Output is correct |
12 |
Correct |
106 ms |
8412 KB |
Output is correct |
13 |
Correct |
110 ms |
8416 KB |
Output is correct |
14 |
Correct |
107 ms |
8412 KB |
Output is correct |
15 |
Correct |
109 ms |
8416 KB |
Output is correct |
16 |
Correct |
105 ms |
8416 KB |
Output is correct |
17 |
Correct |
104 ms |
8416 KB |
Output is correct |
18 |
Correct |
66 ms |
8420 KB |
Output is correct |
19 |
Correct |
62 ms |
8416 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
7 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
14 ms |
3200 KB |
Output is correct |
12 |
Correct |
14 ms |
3200 KB |
Output is correct |
13 |
Correct |
14 ms |
3200 KB |
Output is correct |
14 |
Correct |
14 ms |
3200 KB |
Output is correct |
15 |
Correct |
14 ms |
3200 KB |
Output is correct |
16 |
Correct |
14 ms |
3200 KB |
Output is correct |
17 |
Correct |
15 ms |
3200 KB |
Output is correct |
18 |
Correct |
13 ms |
3200 KB |
Output is correct |
19 |
Correct |
11 ms |
2944 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
8168 KB |
Output is correct |
2 |
Correct |
56 ms |
8164 KB |
Output is correct |
3 |
Correct |
55 ms |
8036 KB |
Output is correct |
4 |
Correct |
55 ms |
8036 KB |
Output is correct |
5 |
Correct |
56 ms |
8172 KB |
Output is correct |
6 |
Correct |
56 ms |
8036 KB |
Output is correct |
7 |
Correct |
57 ms |
8036 KB |
Output is correct |
8 |
Correct |
49 ms |
8032 KB |
Output is correct |
9 |
Correct |
51 ms |
8036 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
11 |
Correct |
107 ms |
8288 KB |
Output is correct |
12 |
Correct |
106 ms |
8412 KB |
Output is correct |
13 |
Correct |
110 ms |
8416 KB |
Output is correct |
14 |
Correct |
107 ms |
8412 KB |
Output is correct |
15 |
Correct |
109 ms |
8416 KB |
Output is correct |
16 |
Correct |
105 ms |
8416 KB |
Output is correct |
17 |
Correct |
104 ms |
8416 KB |
Output is correct |
18 |
Correct |
66 ms |
8420 KB |
Output is correct |
19 |
Correct |
62 ms |
8416 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
256 ms |
9180 KB |
Output is correct |
22 |
Correct |
251 ms |
9180 KB |
Output is correct |
23 |
Correct |
250 ms |
9308 KB |
Output is correct |
24 |
Correct |
255 ms |
9180 KB |
Output is correct |
25 |
Correct |
257 ms |
9312 KB |
Output is correct |
26 |
Correct |
253 ms |
9180 KB |
Output is correct |
27 |
Correct |
255 ms |
9184 KB |
Output is correct |
28 |
Correct |
116 ms |
9184 KB |
Output is correct |
29 |
Correct |
117 ms |
9180 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
190 ms |
13168 KB |
Output is correct |
3 |
Correct |
188 ms |
13024 KB |
Output is correct |
4 |
Correct |
185 ms |
13024 KB |
Output is correct |
5 |
Correct |
197 ms |
13148 KB |
Output is correct |
6 |
Correct |
187 ms |
13128 KB |
Output is correct |
7 |
Correct |
200 ms |
13064 KB |
Output is correct |
8 |
Correct |
184 ms |
13024 KB |
Output is correct |
9 |
Correct |
155 ms |
13024 KB |
Output is correct |
10 |
Correct |
155 ms |
13152 KB |
Output is correct |
11 |
Correct |
56 ms |
8168 KB |
Output is correct |
12 |
Correct |
56 ms |
8164 KB |
Output is correct |
13 |
Correct |
55 ms |
8036 KB |
Output is correct |
14 |
Correct |
55 ms |
8036 KB |
Output is correct |
15 |
Correct |
56 ms |
8172 KB |
Output is correct |
16 |
Correct |
56 ms |
8036 KB |
Output is correct |
17 |
Correct |
57 ms |
8036 KB |
Output is correct |
18 |
Correct |
49 ms |
8032 KB |
Output is correct |
19 |
Correct |
51 ms |
8036 KB |
Output is correct |
20 |
Correct |
5 ms |
2688 KB |
Output is correct |
21 |
Correct |
107 ms |
8288 KB |
Output is correct |
22 |
Correct |
106 ms |
8412 KB |
Output is correct |
23 |
Correct |
110 ms |
8416 KB |
Output is correct |
24 |
Correct |
107 ms |
8412 KB |
Output is correct |
25 |
Correct |
109 ms |
8416 KB |
Output is correct |
26 |
Correct |
105 ms |
8416 KB |
Output is correct |
27 |
Correct |
104 ms |
8416 KB |
Output is correct |
28 |
Correct |
66 ms |
8420 KB |
Output is correct |
29 |
Correct |
62 ms |
8416 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2688 KB |
Output is correct |
35 |
Correct |
7 ms |
2688 KB |
Output is correct |
36 |
Correct |
6 ms |
2688 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2688 KB |
Output is correct |
39 |
Correct |
6 ms |
2688 KB |
Output is correct |
40 |
Correct |
6 ms |
2688 KB |
Output is correct |
41 |
Correct |
14 ms |
3200 KB |
Output is correct |
42 |
Correct |
14 ms |
3200 KB |
Output is correct |
43 |
Correct |
14 ms |
3200 KB |
Output is correct |
44 |
Correct |
14 ms |
3200 KB |
Output is correct |
45 |
Correct |
14 ms |
3200 KB |
Output is correct |
46 |
Correct |
14 ms |
3200 KB |
Output is correct |
47 |
Correct |
15 ms |
3200 KB |
Output is correct |
48 |
Correct |
13 ms |
3200 KB |
Output is correct |
49 |
Correct |
11 ms |
2944 KB |
Output is correct |
50 |
Correct |
6 ms |
2688 KB |
Output is correct |
51 |
Correct |
256 ms |
9180 KB |
Output is correct |
52 |
Correct |
251 ms |
9180 KB |
Output is correct |
53 |
Correct |
250 ms |
9308 KB |
Output is correct |
54 |
Correct |
255 ms |
9180 KB |
Output is correct |
55 |
Correct |
257 ms |
9312 KB |
Output is correct |
56 |
Correct |
253 ms |
9180 KB |
Output is correct |
57 |
Correct |
255 ms |
9184 KB |
Output is correct |
58 |
Correct |
116 ms |
9184 KB |
Output is correct |
59 |
Correct |
117 ms |
9180 KB |
Output is correct |
60 |
Correct |
6 ms |
2688 KB |
Output is correct |
61 |
Correct |
324 ms |
13020 KB |
Output is correct |
62 |
Correct |
338 ms |
13028 KB |
Output is correct |
63 |
Correct |
319 ms |
13020 KB |
Output is correct |
64 |
Correct |
326 ms |
13148 KB |
Output is correct |
65 |
Correct |
336 ms |
13024 KB |
Output is correct |
66 |
Correct |
326 ms |
13020 KB |
Output is correct |
67 |
Correct |
338 ms |
13020 KB |
Output is correct |
68 |
Correct |
288 ms |
13148 KB |
Output is correct |
69 |
Correct |
260 ms |
9308 KB |
Output is correct |
70 |
Correct |
6 ms |
2688 KB |
Output is correct |