#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
int n, d[maxn], p[maxn];
#define mid ((l + r) >> 1)
#define ls (k << 1)
#define rs (k << 1 | 1)
int mx[maxn << 2];
void build(int k, int l, int r) {
if (l == r) { mx[k] = d[l]; return; }
build(ls, l, mid), build(rs, mid + 1, r);
mx[k] = max(mx[ls], mx[rs]);
}
void upd(int k, int l, int r, int p) {
if (l == r) { mx[k] = 0; return; }
mid >= p ? upd(ls, l, mid, p) : upd(rs, mid + 1, r, p);
mx[k] = max(mx[ls], mx[rs]);
}
int find(int k, int l, int r, int v) {
if (l == r) return l;
return mx[rs] >= v ? find(rs, mid + 1, r, v) : find(ls, l, mid, v);
}
namespace BIT {
int c[maxn];
void add(int p, int v) {
for (; p <= n; p += p & -p) c[p] += v;
}
int sum(int p) {
int s = 0;
for (; p; p -= p & -p) s += c[p];
return s;
}
} // namespace BIT
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> d[i];
}
build(1, 1, n);
for (int i = n; i; i--) {
if (mx[1] < i) puts("-1"), exit(0);
int j = find(1, 1, n, i);
p[j] = i, upd(1, 1, n, j);
}
long long ans = 0;
for (int i = n; i; i--) {
ans += BIT::sum(p[i]), BIT::add(p[i], 1);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
472 KB |
Output is correct |
40 |
Correct |
2 ms |
472 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
500 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
488 KB |
Output is correct |
8 |
Correct |
3 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
2 ms |
468 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
472 KB |
Output is correct |
40 |
Correct |
2 ms |
472 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
64 ms |
6008 KB |
Output is correct |
47 |
Correct |
63 ms |
5924 KB |
Output is correct |
48 |
Correct |
55 ms |
6040 KB |
Output is correct |
49 |
Correct |
69 ms |
6192 KB |
Output is correct |
50 |
Correct |
77 ms |
5956 KB |
Output is correct |
51 |
Correct |
64 ms |
5992 KB |
Output is correct |
52 |
Correct |
78 ms |
5972 KB |
Output is correct |
53 |
Correct |
84 ms |
6068 KB |
Output is correct |
54 |
Correct |
86 ms |
5924 KB |
Output is correct |
55 |
Correct |
54 ms |
5964 KB |
Output is correct |
56 |
Correct |
68 ms |
5876 KB |
Output is correct |
57 |
Correct |
68 ms |
5964 KB |
Output is correct |
58 |
Correct |
91 ms |
5880 KB |
Output is correct |
59 |
Correct |
88 ms |
6036 KB |
Output is correct |
60 |
Correct |
74 ms |
5964 KB |
Output is correct |
61 |
Correct |
66 ms |
5924 KB |
Output is correct |
62 |
Correct |
64 ms |
5964 KB |
Output is correct |
63 |
Correct |
71 ms |
5972 KB |
Output is correct |
64 |
Correct |
82 ms |
5960 KB |
Output is correct |
65 |
Correct |
75 ms |
5928 KB |
Output is correct |
66 |
Correct |
68 ms |
5972 KB |
Output is correct |
67 |
Correct |
62 ms |
5856 KB |
Output is correct |
68 |
Correct |
75 ms |
5964 KB |
Output is correct |
69 |
Correct |
82 ms |
5960 KB |
Output is correct |
70 |
Correct |
68 ms |
5884 KB |
Output is correct |
71 |
Correct |
69 ms |
5980 KB |
Output is correct |
72 |
Correct |
65 ms |
6000 KB |
Output is correct |
73 |
Correct |
79 ms |
6024 KB |
Output is correct |
74 |
Correct |
62 ms |
6004 KB |
Output is correct |
75 |
Correct |
67 ms |
6004 KB |
Output is correct |
76 |
Correct |
30 ms |
5192 KB |
Output is correct |
77 |
Correct |
53 ms |
5988 KB |
Output is correct |
78 |
Correct |
54 ms |
5964 KB |
Output is correct |
79 |
Correct |
57 ms |
5944 KB |
Output is correct |
80 |
Correct |
40 ms |
5212 KB |
Output is correct |
81 |
Correct |
50 ms |
5964 KB |
Output is correct |
82 |
Correct |
53 ms |
5988 KB |
Output is correct |
83 |
Correct |
55 ms |
5960 KB |
Output is correct |
84 |
Correct |
72 ms |
5960 KB |
Output is correct |
85 |
Correct |
67 ms |
6036 KB |
Output is correct |
86 |
Correct |
67 ms |
5964 KB |
Output is correct |
87 |
Correct |
17 ms |
4436 KB |
Output is correct |
88 |
Correct |
82 ms |
6148 KB |
Output is correct |