#include <bits/stdc++.h>
using namespace std;
int a[500009];
long long rs[500009];
pair<int,long long> dp[500009];
unordered_map<long long,pair<int,long long> >fw;
struct node{
long long s,e,m;
pair<int,long long> v;
node *l,*r;
node(long long _s,long long _e):s(_s),e(_e),m((_s+_e)/2),v({-1,-1}),l(NULL),r(NULL){}
void up(long long x,pair<int,long long> uv){
if(s==e){
v = max(v,uv);return;
}
if(x>m){
if(r==NULL)r = new node(m+1,e);
r->up(x,uv);
}
else{
if(l==NULL)l = new node(s,m);
l->up(x,uv);
}
v = {-1,-1};
if(r)v = max(v,r->v);
if(l)v = max(v,l->v);
}
pair<int,long long> qu(long long x,long long y){
if(s==x&&e==y)return v;
if(x>m){
if(r)return r->qu(x,y);
else return {-1,-1};
}
if(y<=m){
if(l)return l->qu(x,y);
else return {-1,-1};
}
pair<int,long long> qv = {-1,-1};
if(r)qv = max(qv,r->qu(m+1,y));
if(l)qv = max(qv,l->qu(x,m));
return qv;
}
}*root;
pair<int,long long> query(long long x){
pair<int,long long> ans = {-1,-1};
for(x++;x;x-=x&(-x)){
if(fw.find(x)==fw.end())continue;
ans = max(ans,fw[x]);
}
return ans;
}
void up(long long x,pair<int,long long> v ){
for(x++;x<1e15;x+=x&(-x)){
if(fw.find(x)==fw.end())fw[x] = v;
else fw[x] = max(fw[x],v);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
rs[i+1]=rs[i]+a[i];
}
root = new node(0,(long long)1e15);
root->up(0,{0,0});
for(int i=1;i<=n;i++){
pair<int,long long> res = root->qu(0,rs[i]);
dp[i] = {res.first,res.second - rs[i]};
dp[i].first++;
root->up(rs[i] - dp[i].second,{dp[i].first,rs[i]});
//cout << dp[i].first << ' ' << dp[i].second << '\n';
// cout << res.first << ' ' << res.second.first << ' ' << res.second.second << '\n';
}
printf("%d", dp[n].first);
}
Compilation message
segments.cpp: In function 'int main()':
segments.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
segments.cpp:68: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 |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
380 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 |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
380 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 |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
6 ms |
1272 KB |
Output is correct |
19 |
Correct |
6 ms |
1144 KB |
Output is correct |
20 |
Correct |
6 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1272 KB |
Output is correct |
22 |
Correct |
6 ms |
1144 KB |
Output is correct |
23 |
Correct |
6 ms |
892 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
6 ms |
1276 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
632 KB |
Output is correct |
30 |
Correct |
5 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
380 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 |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
6 ms |
1272 KB |
Output is correct |
19 |
Correct |
6 ms |
1144 KB |
Output is correct |
20 |
Correct |
6 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1272 KB |
Output is correct |
22 |
Correct |
6 ms |
1144 KB |
Output is correct |
23 |
Correct |
6 ms |
892 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
6 ms |
1276 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
632 KB |
Output is correct |
30 |
Correct |
5 ms |
632 KB |
Output is correct |
31 |
Correct |
10 ms |
3448 KB |
Output is correct |
32 |
Correct |
12 ms |
4344 KB |
Output is correct |
33 |
Correct |
13 ms |
5240 KB |
Output is correct |
34 |
Correct |
12 ms |
4984 KB |
Output is correct |
35 |
Correct |
14 ms |
6136 KB |
Output is correct |
36 |
Correct |
13 ms |
6136 KB |
Output is correct |
37 |
Correct |
12 ms |
5112 KB |
Output is correct |
38 |
Correct |
11 ms |
4216 KB |
Output is correct |
39 |
Correct |
9 ms |
1144 KB |
Output is correct |
40 |
Correct |
14 ms |
5880 KB |
Output is correct |
41 |
Correct |
8 ms |
1144 KB |
Output is correct |
42 |
Correct |
9 ms |
2040 KB |
Output is correct |
43 |
Correct |
8 ms |
1016 KB |
Output is correct |
44 |
Correct |
10 ms |
2680 KB |
Output is correct |
45 |
Correct |
10 ms |
2684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
380 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 |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
6 ms |
1272 KB |
Output is correct |
19 |
Correct |
6 ms |
1144 KB |
Output is correct |
20 |
Correct |
6 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1272 KB |
Output is correct |
22 |
Correct |
6 ms |
1144 KB |
Output is correct |
23 |
Correct |
6 ms |
892 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
6 ms |
1276 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
632 KB |
Output is correct |
30 |
Correct |
5 ms |
632 KB |
Output is correct |
31 |
Correct |
10 ms |
3448 KB |
Output is correct |
32 |
Correct |
12 ms |
4344 KB |
Output is correct |
33 |
Correct |
13 ms |
5240 KB |
Output is correct |
34 |
Correct |
12 ms |
4984 KB |
Output is correct |
35 |
Correct |
14 ms |
6136 KB |
Output is correct |
36 |
Correct |
13 ms |
6136 KB |
Output is correct |
37 |
Correct |
12 ms |
5112 KB |
Output is correct |
38 |
Correct |
11 ms |
4216 KB |
Output is correct |
39 |
Correct |
9 ms |
1144 KB |
Output is correct |
40 |
Correct |
14 ms |
5880 KB |
Output is correct |
41 |
Correct |
8 ms |
1144 KB |
Output is correct |
42 |
Correct |
9 ms |
2040 KB |
Output is correct |
43 |
Correct |
8 ms |
1016 KB |
Output is correct |
44 |
Correct |
10 ms |
2680 KB |
Output is correct |
45 |
Correct |
10 ms |
2684 KB |
Output is correct |
46 |
Correct |
153 ms |
72020 KB |
Output is correct |
47 |
Correct |
186 ms |
81912 KB |
Output is correct |
48 |
Correct |
268 ms |
164600 KB |
Output is correct |
49 |
Correct |
283 ms |
176992 KB |
Output is correct |
50 |
Correct |
320 ms |
195960 KB |
Output is correct |
51 |
Correct |
284 ms |
192632 KB |
Output is correct |
52 |
Correct |
145 ms |
96760 KB |
Output is correct |
53 |
Correct |
92 ms |
58232 KB |
Output is correct |
54 |
Correct |
128 ms |
26544 KB |
Output is correct |
55 |
Correct |
300 ms |
186360 KB |
Output is correct |
56 |
Correct |
192 ms |
85752 KB |
Output is correct |
57 |
Correct |
174 ms |
71032 KB |
Output is correct |
58 |
Correct |
182 ms |
78172 KB |
Output is correct |
59 |
Correct |
220 ms |
110968 KB |
Output is correct |
60 |
Correct |
228 ms |
110456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
8 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 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 |
380 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 |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
760 KB |
Output is correct |
17 |
Correct |
6 ms |
1016 KB |
Output is correct |
18 |
Correct |
6 ms |
1272 KB |
Output is correct |
19 |
Correct |
6 ms |
1144 KB |
Output is correct |
20 |
Correct |
6 ms |
1272 KB |
Output is correct |
21 |
Correct |
7 ms |
1272 KB |
Output is correct |
22 |
Correct |
6 ms |
1144 KB |
Output is correct |
23 |
Correct |
6 ms |
892 KB |
Output is correct |
24 |
Correct |
6 ms |
504 KB |
Output is correct |
25 |
Correct |
6 ms |
1276 KB |
Output is correct |
26 |
Correct |
5 ms |
504 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
632 KB |
Output is correct |
30 |
Correct |
5 ms |
632 KB |
Output is correct |
31 |
Correct |
10 ms |
3448 KB |
Output is correct |
32 |
Correct |
12 ms |
4344 KB |
Output is correct |
33 |
Correct |
13 ms |
5240 KB |
Output is correct |
34 |
Correct |
12 ms |
4984 KB |
Output is correct |
35 |
Correct |
14 ms |
6136 KB |
Output is correct |
36 |
Correct |
13 ms |
6136 KB |
Output is correct |
37 |
Correct |
12 ms |
5112 KB |
Output is correct |
38 |
Correct |
11 ms |
4216 KB |
Output is correct |
39 |
Correct |
9 ms |
1144 KB |
Output is correct |
40 |
Correct |
14 ms |
5880 KB |
Output is correct |
41 |
Correct |
8 ms |
1144 KB |
Output is correct |
42 |
Correct |
9 ms |
2040 KB |
Output is correct |
43 |
Correct |
8 ms |
1016 KB |
Output is correct |
44 |
Correct |
10 ms |
2680 KB |
Output is correct |
45 |
Correct |
10 ms |
2684 KB |
Output is correct |
46 |
Correct |
153 ms |
72020 KB |
Output is correct |
47 |
Correct |
186 ms |
81912 KB |
Output is correct |
48 |
Correct |
268 ms |
164600 KB |
Output is correct |
49 |
Correct |
283 ms |
176992 KB |
Output is correct |
50 |
Correct |
320 ms |
195960 KB |
Output is correct |
51 |
Correct |
284 ms |
192632 KB |
Output is correct |
52 |
Correct |
145 ms |
96760 KB |
Output is correct |
53 |
Correct |
92 ms |
58232 KB |
Output is correct |
54 |
Correct |
128 ms |
26544 KB |
Output is correct |
55 |
Correct |
300 ms |
186360 KB |
Output is correct |
56 |
Correct |
192 ms |
85752 KB |
Output is correct |
57 |
Correct |
174 ms |
71032 KB |
Output is correct |
58 |
Correct |
182 ms |
78172 KB |
Output is correct |
59 |
Correct |
220 ms |
110968 KB |
Output is correct |
60 |
Correct |
228 ms |
110456 KB |
Output is correct |
61 |
Runtime error |
627 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
62 |
Halted |
0 ms |
0 KB |
- |