#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define eb emplace_back
ll a[500005],qs[500005];
struct A{
int cnt;
ll val;
A(int cnt_=0,ll val_=0):cnt(cnt_),val(val_){}
bool operator<(const A&o)const{
if(cnt!=o.cnt) return cnt>o.cnt;
return val<o.val;
}
friend A operator+(A l,A r){
l.cnt+=r.cnt;
l.val+=r.val;
return l;
}
}dp[500005];
struct fenwick{
vector<A> bit;
void init(int n){bit=vector<A>(n);}
void upd(int i,A x){for(;i<bit.size();i+=i&-i) bit[i]=min(bit[i],x);}
A qr(int i){
A x(0,0);
for(;i>0;i-=i&-i) x=min(x,bit[i]);
return x;
}
}fw;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin>>n;
vector<ll> comp;
for(int i=1;i<=n;++i){
cin>>a[i];
qs[i]=qs[i-1]+a[i];
comp.eb(qs[i]);
}
comp.erase(unique(comp.begin(),comp.end()),comp.end());
fw.init(comp.size()+5);
for(int i=1;i<=n;++i){
dp[i]=fw.qr(lower_bound(comp.begin(),comp.end(),qs[i])-comp.begin()+1)+A(1,qs[i]);
fw.upd(lower_bound(comp.begin(),comp.end(),qs[i]+dp[i].val)-comp.begin()+1,A(dp[i].cnt,-qs[i]));
}
cout<<dp[n].cnt;
return 0;
}
Compilation message
segments.cpp: In member function 'void fenwick::upd(int, A)':
segments.cpp:24:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<A>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | void upd(int i,A x){for(;i<bit.size();i+=i&-i) bit[i]=min(bit[i],x);}
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10840 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10784 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10856 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10900 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10840 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10784 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10856 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10900 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10840 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10844 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
3 ms |
10856 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
2 ms |
10840 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
2 ms |
10900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10840 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10784 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10856 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10900 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10840 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10844 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
3 ms |
10856 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
2 ms |
10840 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
2 ms |
10900 KB |
Output is correct |
46 |
Correct |
17 ms |
17364 KB |
Output is correct |
47 |
Correct |
22 ms |
17880 KB |
Output is correct |
48 |
Correct |
26 ms |
18420 KB |
Output is correct |
49 |
Correct |
24 ms |
18436 KB |
Output is correct |
50 |
Correct |
24 ms |
18392 KB |
Output is correct |
51 |
Correct |
27 ms |
18384 KB |
Output is correct |
52 |
Correct |
14 ms |
12752 KB |
Output is correct |
53 |
Correct |
9 ms |
11896 KB |
Output is correct |
54 |
Correct |
22 ms |
17716 KB |
Output is correct |
55 |
Correct |
27 ms |
18388 KB |
Output is correct |
56 |
Correct |
22 ms |
17880 KB |
Output is correct |
57 |
Correct |
20 ms |
17692 KB |
Output is correct |
58 |
Correct |
21 ms |
17880 KB |
Output is correct |
59 |
Correct |
22 ms |
17880 KB |
Output is correct |
60 |
Correct |
24 ms |
18128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10840 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
10784 KB |
Output is correct |
21 |
Correct |
2 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10856 KB |
Output is correct |
23 |
Correct |
2 ms |
10840 KB |
Output is correct |
24 |
Correct |
3 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
2 ms |
10900 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10844 KB |
Output is correct |
33 |
Correct |
2 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
2 ms |
10840 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
2 ms |
10844 KB |
Output is correct |
38 |
Correct |
2 ms |
10844 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
3 ms |
10856 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
2 ms |
10840 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
2 ms |
10900 KB |
Output is correct |
46 |
Correct |
17 ms |
17364 KB |
Output is correct |
47 |
Correct |
22 ms |
17880 KB |
Output is correct |
48 |
Correct |
26 ms |
18420 KB |
Output is correct |
49 |
Correct |
24 ms |
18436 KB |
Output is correct |
50 |
Correct |
24 ms |
18392 KB |
Output is correct |
51 |
Correct |
27 ms |
18384 KB |
Output is correct |
52 |
Correct |
14 ms |
12752 KB |
Output is correct |
53 |
Correct |
9 ms |
11896 KB |
Output is correct |
54 |
Correct |
22 ms |
17716 KB |
Output is correct |
55 |
Correct |
27 ms |
18388 KB |
Output is correct |
56 |
Correct |
22 ms |
17880 KB |
Output is correct |
57 |
Correct |
20 ms |
17692 KB |
Output is correct |
58 |
Correct |
21 ms |
17880 KB |
Output is correct |
59 |
Correct |
22 ms |
17880 KB |
Output is correct |
60 |
Correct |
24 ms |
18128 KB |
Output is correct |
61 |
Correct |
92 ms |
29896 KB |
Output is correct |
62 |
Correct |
86 ms |
29940 KB |
Output is correct |
63 |
Correct |
101 ms |
30336 KB |
Output is correct |
64 |
Correct |
125 ms |
32704 KB |
Output is correct |
65 |
Correct |
113 ms |
32716 KB |
Output is correct |
66 |
Correct |
130 ms |
32640 KB |
Output is correct |
67 |
Correct |
102 ms |
28716 KB |
Output is correct |
68 |
Correct |
54 ms |
21756 KB |
Output is correct |
69 |
Correct |
111 ms |
28844 KB |
Output is correct |
70 |
Correct |
128 ms |
32880 KB |
Output is correct |
71 |
Correct |
104 ms |
29896 KB |
Output is correct |
72 |
Correct |
111 ms |
30640 KB |
Output is correct |
73 |
Correct |
102 ms |
30872 KB |
Output is correct |
74 |
Correct |
105 ms |
31272 KB |
Output is correct |
75 |
Correct |
109 ms |
31064 KB |
Output is correct |