#include<bits/stdc++.h>
#define vi vector<int>
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 2e5+5;
int MX[4*N],sum[4*N];
vector<int> v,v1;
void build(int v,int tl,int tr) {
if (tl == tr) {
MX[v] = v1[tl];
sum[v] = 1;
return;
}
int tm = (tl + tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
sum[v] = sum[v*2] + sum[v*2+1];
MX[v] = max(MX[v*2],MX[v*2+1]);
}
long long ans;
int idx;
void upd(int v,int tl,int tr,int val) {
if (tl == tr) {
sum[v] = 0;
MX[v] = -1;
idx = tl;
return;
}
int tm = (tl + tr)/2;
if (MX[v*2+1] >= val) {
upd(v*2+1,tm+1,tr,val);
}else
upd(v*2,tl,tm,val);
sum[v] = sum[v*2] + sum[v*2+1];
MX[v] = max(MX[v*2],MX[v*2+1]);
}
int get(int v,int tl,int tr,int pos) {
if (pos > tr) return 0;
if (pos <= tl) return sum[v];
int tm = (tl + tr)/2;
return get(v*2,tl,tm,pos) +
get(v*2+1,tm+1,tr,pos);
}
main() {
int n;
cin >> n;
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
--v[i];
}
vi g = v;
v1 = v;
sort(g.begin(),g.end());
// INVALID
for (int i = 0; i < n; i++)
if (g[i] < i) {
cout <<-1;
return 0;
}//
build(1,0,n-1);
for (int i = n-1; i >= 0; i--) {
upd(1,0,n-1,i);
ans += get(1,0,n-1,idx);
}
cout << ans;
return 0;
}
Compilation message
Main.cpp:49:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
49 | main() {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
4 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
4 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
512 KB |
Output is correct |
37 |
Correct |
4 ms |
512 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
4 ms |
548 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
512 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
4 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
4 ms |
512 KB |
Output is correct |
22 |
Correct |
4 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
512 KB |
Output is correct |
24 |
Correct |
4 ms |
512 KB |
Output is correct |
25 |
Correct |
4 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
4 ms |
512 KB |
Output is correct |
28 |
Correct |
6 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
512 KB |
Output is correct |
33 |
Correct |
4 ms |
512 KB |
Output is correct |
34 |
Correct |
4 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
384 KB |
Output is correct |
36 |
Correct |
4 ms |
512 KB |
Output is correct |
37 |
Correct |
4 ms |
512 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
4 ms |
548 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
512 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
256 KB |
Output is correct |
46 |
Correct |
220 ms |
7108 KB |
Output is correct |
47 |
Correct |
212 ms |
8184 KB |
Output is correct |
48 |
Correct |
194 ms |
8312 KB |
Output is correct |
49 |
Correct |
192 ms |
8184 KB |
Output is correct |
50 |
Correct |
203 ms |
8184 KB |
Output is correct |
51 |
Correct |
203 ms |
8312 KB |
Output is correct |
52 |
Correct |
223 ms |
8184 KB |
Output is correct |
53 |
Correct |
226 ms |
8056 KB |
Output is correct |
54 |
Correct |
205 ms |
8312 KB |
Output is correct |
55 |
Correct |
185 ms |
8232 KB |
Output is correct |
56 |
Correct |
207 ms |
8056 KB |
Output is correct |
57 |
Correct |
210 ms |
8184 KB |
Output is correct |
58 |
Correct |
230 ms |
8056 KB |
Output is correct |
59 |
Correct |
225 ms |
8184 KB |
Output is correct |
60 |
Correct |
227 ms |
8060 KB |
Output is correct |
61 |
Correct |
201 ms |
8124 KB |
Output is correct |
62 |
Correct |
191 ms |
8196 KB |
Output is correct |
63 |
Correct |
208 ms |
8108 KB |
Output is correct |
64 |
Correct |
227 ms |
8056 KB |
Output is correct |
65 |
Correct |
235 ms |
8184 KB |
Output is correct |
66 |
Correct |
210 ms |
8056 KB |
Output is correct |
67 |
Correct |
199 ms |
8184 KB |
Output is correct |
68 |
Correct |
211 ms |
8060 KB |
Output is correct |
69 |
Correct |
222 ms |
8184 KB |
Output is correct |
70 |
Correct |
223 ms |
8136 KB |
Output is correct |
71 |
Correct |
215 ms |
8056 KB |
Output is correct |
72 |
Correct |
212 ms |
8184 KB |
Output is correct |
73 |
Correct |
205 ms |
8184 KB |
Output is correct |
74 |
Correct |
188 ms |
8056 KB |
Output is correct |
75 |
Correct |
211 ms |
8056 KB |
Output is correct |
76 |
Correct |
139 ms |
3960 KB |
Output is correct |
77 |
Correct |
183 ms |
8312 KB |
Output is correct |
78 |
Correct |
183 ms |
8056 KB |
Output is correct |
79 |
Correct |
181 ms |
8188 KB |
Output is correct |
80 |
Correct |
139 ms |
3960 KB |
Output is correct |
81 |
Correct |
185 ms |
8336 KB |
Output is correct |
82 |
Correct |
181 ms |
8184 KB |
Output is correct |
83 |
Correct |
187 ms |
8184 KB |
Output is correct |
84 |
Correct |
218 ms |
8056 KB |
Output is correct |
85 |
Correct |
212 ms |
8056 KB |
Output is correct |
86 |
Correct |
212 ms |
8312 KB |
Output is correct |
87 |
Correct |
144 ms |
3996 KB |
Output is correct |
88 |
Correct |
216 ms |
8340 KB |
Output is correct |