#include <bits/stdc++.h>
using namespace std;
const int N = 1'000'000 + 10,
M = 1'000'003,
MAX = 1'000'000'001;
int n;
int h[N];
int goalH[N];
int f[N << 2];
void build(int s, int l, int r) {
if (l == r) {
f[s] = h[l];
return;
}
int mid = (l + r) >> 1;
build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
f[s] = min(f[s << 1], f[s << 1 | 1]);
}
void update(int s, int l, int r, int p, int x) {
if (p < l || p > r) return;
if (l == r) {
f[s] = x;
return;
}
int mid = (l + r) >> 1;
update(s << 1, l, mid, p, x); update(s << 1 | 1, mid + 1, r, p, x);
f[s] = min(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return MAX;
if (u <= l && r <= v) return f[s];
int mid = (l + r) >> 1;
return min(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= n; ++i) {
goalH[i] = min(*max_element(h + 1, h + i + 1),
*max_element(h + i, h + n + 1));
}
long long answer = 0;
build(1, 1, n);
for (int height = 1; height <= 100; ++height) {
vector<int> vt;
for (int i = 1; i <= n; ++i) {
if (h[i] == height) update(1, 1, n, i, MAX);
if (h[i] != goalH[i] && h[i] == height) vt.push_back(i);
}
if (!vt.size()) continue;
if (vt.size() == 1) {
int i = vt[0];
answer += query(1, 1, n, 1, i) + query(1, 1, n, i, n) + h[i];
} else {
int l = vt[0], r = vt.back();
long long addLFirst = query(1, 1, n, 1, l) + query(1, 1, n, l, n) + height +
height + 1 + height + query(1, 1, n, r, n);
long long addRFirst = query(1, 1, n, 1, r) + query(1, 1, n, r, n) + height +
height + 1 + height + query(1, 1, n, 1, l);
answer += min(addLFirst, addRFirst);
answer += 1ll * (vt.size() - 2) * (height * 3 + 2);
}
for (const auto& i : vt) h[i] += 1;
}
cout << answer % M << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Incorrect |
19 ms |
592 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Incorrect |
19 ms |
592 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Execution timed out |
5033 ms |
7260 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Incorrect |
19 ms |
592 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
41 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
592 KB |
Output is correct |
6 |
Correct |
20 ms |
592 KB |
Output is correct |
7 |
Correct |
19 ms |
532 KB |
Output is correct |
8 |
Correct |
62 ms |
648 KB |
Output is correct |
9 |
Correct |
19 ms |
592 KB |
Output is correct |
10 |
Correct |
19 ms |
592 KB |
Output is correct |
11 |
Correct |
43 ms |
688 KB |
Output is correct |
12 |
Correct |
46 ms |
592 KB |
Output is correct |
13 |
Correct |
42 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
45 ms |
636 KB |
Output is correct |
17 |
Correct |
41 ms |
592 KB |
Output is correct |
18 |
Correct |
41 ms |
592 KB |
Output is correct |
19 |
Correct |
41 ms |
592 KB |
Output is correct |
20 |
Correct |
57 ms |
592 KB |
Output is correct |
21 |
Correct |
59 ms |
760 KB |
Output is correct |
22 |
Correct |
59 ms |
480 KB |
Output is correct |
23 |
Correct |
41 ms |
604 KB |
Output is correct |
24 |
Correct |
52 ms |
520 KB |
Output is correct |
25 |
Correct |
40 ms |
592 KB |
Output is correct |
26 |
Correct |
40 ms |
592 KB |
Output is correct |
27 |
Correct |
41 ms |
524 KB |
Output is correct |
28 |
Correct |
41 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Incorrect |
19 ms |
592 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |