#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
typedef long long ll;
struct Node{
pair<int,int>val;
Node*left;
Node*right;
Node() {
val={0,0};
left=nullptr;
right=nullptr;
}
};
pair<int,int>get_val(Node *root){
if(root==nullptr)return make_pair(0,0);
return root->val;
}
void update(Node *root, ll p, int x, int y, ll l, ll r) {
if(l==p && r==p){
root->val=max(make_pair(x,y),root->val);
return;
}
ll m=(l+r)/2;
if(l<=p&&p<=m){
if(root->left==nullptr){
root->left=new Node;
}
update(root->left,p,x,y,l,m);
}
if(m+1<=p&&p<=r){
if(root->right==nullptr){
root->right=new Node;
}
update(root->right,p,x,y,m+1,r);
}
root->val=max(get_val(root->left),get_val(root->right));
}
pair<int,int> get(Node *root, ll l, ll r, ll lx, ll rx){
if(l>rx || lx>r) {
return {0,0};
}
if(l<=lx && rx<=r){
return root->val;
}
ll mx=(lx+rx)/2;
pair<int,int> ans={0,0};
if(root->right!=nullptr){
ans=max(ans,get(root->right,l,r, mx+1, rx));
}
if(root->left!=nullptr){
ans=max(ans,get(root->left,l,r, lx, mx));
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector<ll>pr(n+1);
for(int i=0;i<n;i++)pr[i+1]=pr[i]+a[i];
vector<int>ans(n);
vector<ll> ans_sum(n);
ll sum=0;
for(int i=0;i<n;i++){
sum+=a[i];
ans_sum[i]=sum;
ans[i]=1;
}
Node *tree=new Node;
update(tree,ans_sum[0]+pr[1],ans[0],0, 0, 1e15);
for(int i=1;i<n;i++){
if(n<=100){
for(int j=i-1;j>=0;j--){
if(pr[i+1]>=ans_sum[j]+pr[j+1]){
if(ans[i]<ans[j]+1){
ans[i]=ans[j]+1;
ans_sum[i]=pr[i+1]-pr[j+1];
}
}
}
//cout<<ans[i]<<" ";
//cout<<ans_sum[i]<<" ";
//cout<<endl;
}
else{
auto t=get(tree,0,pr[i+1],0,1e15);
if(pr[i+1]>=ans_sum[t.second]+pr[t.second+1]){
if(ans[i]<t.first+1){
ans[i]=t.first+1;
ans_sum[i]=pr[i+1]-pr[t.second+1];
}
}
update(tree,ans_sum[i]+pr[i+1],ans[i],i,0,1e15);
}
//cout<<ans[i]<<" ";
//cout<<ans_sum[i]<<" ";
//cout<<endl;
}
cout<<ans[n-1];
return 0;
}
Compilation message
segments.cpp:2:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/STACK:36777216")
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
4 ms |
760 KB |
Output is correct |
21 |
Correct |
4 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
380 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
4 ms |
760 KB |
Output is correct |
21 |
Correct |
4 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
380 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
8 ms |
1912 KB |
Output is correct |
32 |
Correct |
9 ms |
2300 KB |
Output is correct |
33 |
Correct |
10 ms |
2808 KB |
Output is correct |
34 |
Correct |
10 ms |
2680 KB |
Output is correct |
35 |
Correct |
12 ms |
3320 KB |
Output is correct |
36 |
Correct |
11 ms |
3192 KB |
Output is correct |
37 |
Correct |
11 ms |
2808 KB |
Output is correct |
38 |
Correct |
8 ms |
2296 KB |
Output is correct |
39 |
Correct |
7 ms |
760 KB |
Output is correct |
40 |
Correct |
11 ms |
3032 KB |
Output is correct |
41 |
Correct |
6 ms |
760 KB |
Output is correct |
42 |
Correct |
7 ms |
1272 KB |
Output is correct |
43 |
Correct |
7 ms |
764 KB |
Output is correct |
44 |
Correct |
8 ms |
1528 KB |
Output is correct |
45 |
Correct |
8 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
4 ms |
760 KB |
Output is correct |
21 |
Correct |
4 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
380 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
8 ms |
1912 KB |
Output is correct |
32 |
Correct |
9 ms |
2300 KB |
Output is correct |
33 |
Correct |
10 ms |
2808 KB |
Output is correct |
34 |
Correct |
10 ms |
2680 KB |
Output is correct |
35 |
Correct |
12 ms |
3320 KB |
Output is correct |
36 |
Correct |
11 ms |
3192 KB |
Output is correct |
37 |
Correct |
11 ms |
2808 KB |
Output is correct |
38 |
Correct |
8 ms |
2296 KB |
Output is correct |
39 |
Correct |
7 ms |
760 KB |
Output is correct |
40 |
Correct |
11 ms |
3032 KB |
Output is correct |
41 |
Correct |
6 ms |
760 KB |
Output is correct |
42 |
Correct |
7 ms |
1272 KB |
Output is correct |
43 |
Correct |
7 ms |
764 KB |
Output is correct |
44 |
Correct |
8 ms |
1528 KB |
Output is correct |
45 |
Correct |
8 ms |
1528 KB |
Output is correct |
46 |
Correct |
168 ms |
36984 KB |
Output is correct |
47 |
Correct |
206 ms |
41976 KB |
Output is correct |
48 |
Correct |
285 ms |
83320 KB |
Output is correct |
49 |
Correct |
301 ms |
89364 KB |
Output is correct |
50 |
Correct |
340 ms |
98936 KB |
Output is correct |
51 |
Correct |
297 ms |
97244 KB |
Output is correct |
52 |
Correct |
149 ms |
48760 KB |
Output is correct |
53 |
Correct |
90 ms |
29432 KB |
Output is correct |
54 |
Correct |
154 ms |
14172 KB |
Output is correct |
55 |
Correct |
313 ms |
94120 KB |
Output is correct |
56 |
Correct |
218 ms |
43768 KB |
Output is correct |
57 |
Correct |
196 ms |
36336 KB |
Output is correct |
58 |
Correct |
210 ms |
40176 KB |
Output is correct |
59 |
Correct |
248 ms |
56412 KB |
Output is correct |
60 |
Correct |
238 ms |
56156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
632 KB |
Output is correct |
17 |
Correct |
3 ms |
632 KB |
Output is correct |
18 |
Correct |
3 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
4 ms |
760 KB |
Output is correct |
21 |
Correct |
4 ms |
892 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
3 ms |
380 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
504 KB |
Output is correct |
30 |
Correct |
3 ms |
504 KB |
Output is correct |
31 |
Correct |
8 ms |
1912 KB |
Output is correct |
32 |
Correct |
9 ms |
2300 KB |
Output is correct |
33 |
Correct |
10 ms |
2808 KB |
Output is correct |
34 |
Correct |
10 ms |
2680 KB |
Output is correct |
35 |
Correct |
12 ms |
3320 KB |
Output is correct |
36 |
Correct |
11 ms |
3192 KB |
Output is correct |
37 |
Correct |
11 ms |
2808 KB |
Output is correct |
38 |
Correct |
8 ms |
2296 KB |
Output is correct |
39 |
Correct |
7 ms |
760 KB |
Output is correct |
40 |
Correct |
11 ms |
3032 KB |
Output is correct |
41 |
Correct |
6 ms |
760 KB |
Output is correct |
42 |
Correct |
7 ms |
1272 KB |
Output is correct |
43 |
Correct |
7 ms |
764 KB |
Output is correct |
44 |
Correct |
8 ms |
1528 KB |
Output is correct |
45 |
Correct |
8 ms |
1528 KB |
Output is correct |
46 |
Correct |
168 ms |
36984 KB |
Output is correct |
47 |
Correct |
206 ms |
41976 KB |
Output is correct |
48 |
Correct |
285 ms |
83320 KB |
Output is correct |
49 |
Correct |
301 ms |
89364 KB |
Output is correct |
50 |
Correct |
340 ms |
98936 KB |
Output is correct |
51 |
Correct |
297 ms |
97244 KB |
Output is correct |
52 |
Correct |
149 ms |
48760 KB |
Output is correct |
53 |
Correct |
90 ms |
29432 KB |
Output is correct |
54 |
Correct |
154 ms |
14172 KB |
Output is correct |
55 |
Correct |
313 ms |
94120 KB |
Output is correct |
56 |
Correct |
218 ms |
43768 KB |
Output is correct |
57 |
Correct |
196 ms |
36336 KB |
Output is correct |
58 |
Correct |
210 ms |
40176 KB |
Output is correct |
59 |
Correct |
248 ms |
56412 KB |
Output is correct |
60 |
Correct |
238 ms |
56156 KB |
Output is correct |
61 |
Correct |
826 ms |
171056 KB |
Output is correct |
62 |
Correct |
890 ms |
164580 KB |
Output is correct |
63 |
Correct |
1029 ms |
208796 KB |
Output is correct |
64 |
Runtime error |
875 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |