#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N = 5e5 + 5;
const long long M = (long long)1e15;
int n,a[N];
long long pref[N];
pair<int,long long> dp[N];
unordered_map< long long,pair<int,long long> > t;
void upd(long long pos,pair<long long,long long> val,long long v = 1,long long tl = 1,long long tr = M){
if(tl == tr){
t[v] = val;
return;
}
long long tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,v << 1,tl,tm);
else upd(pos,val,v << 1 | 1,tm + 1,tr);
t[v] = max(t[v << 1],t[v << 1 | 1]);
}
pair<int,long long> get(long long l,long long r,long long v = 1,long long tl = 1,long long tr = M){
if(l <= tl && tr <= r) return t[v];
if(tl > r || tr < l) return make_pair(0,0);
long long tm = (tl + tr) >> 1;
return max(get(l,r,v << 1,tl,tm),get(l,r,v << 1 | 1,tm + 1,tr));
}
int main(){
//ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
pref[i] = pref[i - 1] + a[i];
}
dp[1] = make_pair(1,a[1]);
upd(pref[1] + dp[1].S,make_pair(dp[1].F,1));
for(int i = 2; i <= n; i++){
dp[i] = make_pair(dp[i - 1].F,dp[i - 1].S + a[i]);
pair<int,long long> cur = get(1,pref[i]);
if(cur.F > 0) dp[i] = make_pair(cur.F + 1,pref[i] - pref[cur.S]);
upd(pref[i] + dp[i].S,make_pair(dp[i].F,i));
}
printf("%d",dp[n].F);
return 0;
}
Compilation message
segments.cpp: In function 'int main()':
segments.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
segments.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1400 KB |
Output is correct |
17 |
Correct |
11 ms |
2296 KB |
Output is correct |
18 |
Correct |
11 ms |
2344 KB |
Output is correct |
19 |
Correct |
11 ms |
2168 KB |
Output is correct |
20 |
Correct |
11 ms |
2168 KB |
Output is correct |
21 |
Correct |
11 ms |
2468 KB |
Output is correct |
22 |
Correct |
10 ms |
2172 KB |
Output is correct |
23 |
Correct |
9 ms |
1524 KB |
Output is correct |
24 |
Correct |
7 ms |
504 KB |
Output is correct |
25 |
Correct |
12 ms |
2216 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
7 ms |
504 KB |
Output is correct |
28 |
Correct |
7 ms |
632 KB |
Output is correct |
29 |
Correct |
9 ms |
1016 KB |
Output is correct |
30 |
Correct |
9 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1400 KB |
Output is correct |
17 |
Correct |
11 ms |
2296 KB |
Output is correct |
18 |
Correct |
11 ms |
2344 KB |
Output is correct |
19 |
Correct |
11 ms |
2168 KB |
Output is correct |
20 |
Correct |
11 ms |
2168 KB |
Output is correct |
21 |
Correct |
11 ms |
2468 KB |
Output is correct |
22 |
Correct |
10 ms |
2172 KB |
Output is correct |
23 |
Correct |
9 ms |
1524 KB |
Output is correct |
24 |
Correct |
7 ms |
504 KB |
Output is correct |
25 |
Correct |
12 ms |
2216 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
7 ms |
504 KB |
Output is correct |
28 |
Correct |
7 ms |
632 KB |
Output is correct |
29 |
Correct |
9 ms |
1016 KB |
Output is correct |
30 |
Correct |
9 ms |
888 KB |
Output is correct |
31 |
Correct |
41 ms |
8068 KB |
Output is correct |
32 |
Correct |
52 ms |
9020 KB |
Output is correct |
33 |
Correct |
56 ms |
10424 KB |
Output is correct |
34 |
Correct |
50 ms |
9272 KB |
Output is correct |
35 |
Correct |
57 ms |
10424 KB |
Output is correct |
36 |
Correct |
70 ms |
12348 KB |
Output is correct |
37 |
Correct |
55 ms |
10680 KB |
Output is correct |
38 |
Correct |
46 ms |
8888 KB |
Output is correct |
39 |
Correct |
18 ms |
1528 KB |
Output is correct |
40 |
Correct |
60 ms |
11064 KB |
Output is correct |
41 |
Correct |
17 ms |
1400 KB |
Output is correct |
42 |
Correct |
21 ms |
2984 KB |
Output is correct |
43 |
Correct |
18 ms |
1400 KB |
Output is correct |
44 |
Correct |
25 ms |
4488 KB |
Output is correct |
45 |
Correct |
27 ms |
4484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1400 KB |
Output is correct |
17 |
Correct |
11 ms |
2296 KB |
Output is correct |
18 |
Correct |
11 ms |
2344 KB |
Output is correct |
19 |
Correct |
11 ms |
2168 KB |
Output is correct |
20 |
Correct |
11 ms |
2168 KB |
Output is correct |
21 |
Correct |
11 ms |
2468 KB |
Output is correct |
22 |
Correct |
10 ms |
2172 KB |
Output is correct |
23 |
Correct |
9 ms |
1524 KB |
Output is correct |
24 |
Correct |
7 ms |
504 KB |
Output is correct |
25 |
Correct |
12 ms |
2216 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
7 ms |
504 KB |
Output is correct |
28 |
Correct |
7 ms |
632 KB |
Output is correct |
29 |
Correct |
9 ms |
1016 KB |
Output is correct |
30 |
Correct |
9 ms |
888 KB |
Output is correct |
31 |
Correct |
41 ms |
8068 KB |
Output is correct |
32 |
Correct |
52 ms |
9020 KB |
Output is correct |
33 |
Correct |
56 ms |
10424 KB |
Output is correct |
34 |
Correct |
50 ms |
9272 KB |
Output is correct |
35 |
Correct |
57 ms |
10424 KB |
Output is correct |
36 |
Correct |
70 ms |
12348 KB |
Output is correct |
37 |
Correct |
55 ms |
10680 KB |
Output is correct |
38 |
Correct |
46 ms |
8888 KB |
Output is correct |
39 |
Correct |
18 ms |
1528 KB |
Output is correct |
40 |
Correct |
60 ms |
11064 KB |
Output is correct |
41 |
Correct |
17 ms |
1400 KB |
Output is correct |
42 |
Correct |
21 ms |
2984 KB |
Output is correct |
43 |
Correct |
18 ms |
1400 KB |
Output is correct |
44 |
Correct |
25 ms |
4488 KB |
Output is correct |
45 |
Correct |
27 ms |
4484 KB |
Output is correct |
46 |
Execution timed out |
1593 ms |
148932 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1400 KB |
Output is correct |
17 |
Correct |
11 ms |
2296 KB |
Output is correct |
18 |
Correct |
11 ms |
2344 KB |
Output is correct |
19 |
Correct |
11 ms |
2168 KB |
Output is correct |
20 |
Correct |
11 ms |
2168 KB |
Output is correct |
21 |
Correct |
11 ms |
2468 KB |
Output is correct |
22 |
Correct |
10 ms |
2172 KB |
Output is correct |
23 |
Correct |
9 ms |
1524 KB |
Output is correct |
24 |
Correct |
7 ms |
504 KB |
Output is correct |
25 |
Correct |
12 ms |
2216 KB |
Output is correct |
26 |
Correct |
6 ms |
504 KB |
Output is correct |
27 |
Correct |
7 ms |
504 KB |
Output is correct |
28 |
Correct |
7 ms |
632 KB |
Output is correct |
29 |
Correct |
9 ms |
1016 KB |
Output is correct |
30 |
Correct |
9 ms |
888 KB |
Output is correct |
31 |
Correct |
41 ms |
8068 KB |
Output is correct |
32 |
Correct |
52 ms |
9020 KB |
Output is correct |
33 |
Correct |
56 ms |
10424 KB |
Output is correct |
34 |
Correct |
50 ms |
9272 KB |
Output is correct |
35 |
Correct |
57 ms |
10424 KB |
Output is correct |
36 |
Correct |
70 ms |
12348 KB |
Output is correct |
37 |
Correct |
55 ms |
10680 KB |
Output is correct |
38 |
Correct |
46 ms |
8888 KB |
Output is correct |
39 |
Correct |
18 ms |
1528 KB |
Output is correct |
40 |
Correct |
60 ms |
11064 KB |
Output is correct |
41 |
Correct |
17 ms |
1400 KB |
Output is correct |
42 |
Correct |
21 ms |
2984 KB |
Output is correct |
43 |
Correct |
18 ms |
1400 KB |
Output is correct |
44 |
Correct |
25 ms |
4488 KB |
Output is correct |
45 |
Correct |
27 ms |
4484 KB |
Output is correct |
46 |
Execution timed out |
1593 ms |
148932 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |