#include <stdio.h>
#define N 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N + 1))) */
int st[N_ * 2], n_;
void build(int n) {
int i;
n_ = 1;
while (n_ <= n)
n_ <<= 1;
for (i = 0; i < n; i++)
st[n_ + i] = 1;
for (i = n_ - 1; i > 0; i--)
st[i] = st[i << 1 | 0] + st[i << 1 | 1];
}
void update(int i) {
for (i += n_; i > 0; i >>= 1)
st[i]--;
}
int query(int l) {
int r = n_ - 1, x = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((l & 1) == 1)
x += st[l++];
return x;
}
int prev(int r) {
int l = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0) {
if (st[r]) {
while (r < n_)
r = st[r << 1 | 1] ? r << 1 | 1 : r << 1 | 0;
return r - n_;
}
r--;
}
return -1;
}
int main() {
static int dd[N];
int n, i, d;
long long ans;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &dd[i]), dd[i]--;
build(n);
ans = 0;
for (i = n - 1; i >= 0; i--) {
if ((d = prev(dd[i])) == -1) {
printf("-1\n");
return 0;
}
ans += query(d + 1);
update(d);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:54:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:56:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d", &dd[i]), dd[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
304 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
388 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
308 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
304 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
304 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
308 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
304 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
388 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
308 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
304 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
292 KB |
Output is correct |
46 |
Correct |
54 ms |
4196 KB |
Output is correct |
47 |
Correct |
54 ms |
4172 KB |
Output is correct |
48 |
Correct |
57 ms |
4196 KB |
Output is correct |
49 |
Correct |
55 ms |
4200 KB |
Output is correct |
50 |
Correct |
51 ms |
4096 KB |
Output is correct |
51 |
Correct |
49 ms |
4196 KB |
Output is correct |
52 |
Correct |
43 ms |
4048 KB |
Output is correct |
53 |
Correct |
43 ms |
4028 KB |
Output is correct |
54 |
Correct |
38 ms |
4044 KB |
Output is correct |
55 |
Correct |
33 ms |
4044 KB |
Output is correct |
56 |
Correct |
36 ms |
4016 KB |
Output is correct |
57 |
Correct |
36 ms |
4076 KB |
Output is correct |
58 |
Correct |
50 ms |
4116 KB |
Output is correct |
59 |
Correct |
46 ms |
4052 KB |
Output is correct |
60 |
Correct |
45 ms |
4112 KB |
Output is correct |
61 |
Correct |
41 ms |
4116 KB |
Output is correct |
62 |
Correct |
35 ms |
4116 KB |
Output is correct |
63 |
Correct |
41 ms |
4116 KB |
Output is correct |
64 |
Correct |
47 ms |
4044 KB |
Output is correct |
65 |
Correct |
51 ms |
4116 KB |
Output is correct |
66 |
Correct |
45 ms |
4116 KB |
Output is correct |
67 |
Correct |
43 ms |
4116 KB |
Output is correct |
68 |
Correct |
43 ms |
4044 KB |
Output is correct |
69 |
Correct |
47 ms |
4112 KB |
Output is correct |
70 |
Correct |
51 ms |
4152 KB |
Output is correct |
71 |
Correct |
51 ms |
4128 KB |
Output is correct |
72 |
Correct |
51 ms |
4060 KB |
Output is correct |
73 |
Correct |
53 ms |
4132 KB |
Output is correct |
74 |
Correct |
39 ms |
4064 KB |
Output is correct |
75 |
Correct |
47 ms |
4156 KB |
Output is correct |
76 |
Correct |
54 ms |
4180 KB |
Output is correct |
77 |
Correct |
48 ms |
4172 KB |
Output is correct |
78 |
Correct |
32 ms |
4124 KB |
Output is correct |
79 |
Correct |
33 ms |
4024 KB |
Output is correct |
80 |
Correct |
58 ms |
4132 KB |
Output is correct |
81 |
Correct |
51 ms |
4228 KB |
Output is correct |
82 |
Correct |
35 ms |
4120 KB |
Output is correct |
83 |
Correct |
35 ms |
4120 KB |
Output is correct |
84 |
Correct |
51 ms |
4172 KB |
Output is correct |
85 |
Correct |
51 ms |
4180 KB |
Output is correct |
86 |
Correct |
51 ms |
4112 KB |
Output is correct |
87 |
Correct |
53 ms |
4140 KB |
Output is correct |
88 |
Correct |
52 ms |
4176 KB |
Output is correct |