#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int MX = 5e5 + 5 ;
const pair<ll,ll> emp = {0,0} ;
pair<ll,ll> dp[MX] ;
struct lazytree
{
vector<pair<ll,ll> > t , lazy ;
void init(int n)
{
t = vector<pair<ll , ll> > (4*n + 4 , emp) ;
lazy = vector<pair<ll , ll> > (4*n + 4 , emp) ;
}
void upd(int node , pair<ll,ll> val)
{
t[node] = max(t[node],val) ;
lazy[node] = max(lazy[node],val) ;
}
void shift(int node , int low , int high)
{
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
upd(left,lazy[node]) ;
upd(right,lazy[node]) ;
lazy[node] = emp ;
}
void update(int node , int low , int high , int l , int r , pair<ll,ll> val)
{
if(l>r) return ;
if((r<low) || (l>high)) return ;
if((l<=low) && (high<=r))
{
upd(node,val) ;
return ;
}
shift(node,low,high) ;
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
update(left,low,mid,l,r,val) ;
update(right,mid+1,high,l,r,val) ;
t[node] = max(t[left],t[right]) ;
}
pair<ll,ll> query(int node , int low , int high , int l , int r)
{
if(l>r) return emp ;
if((r<low) || (l>high)) return emp ;
if((l<=low) && (high<=r)) return t[node] ;
shift(node,low,high) ;
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
return max(query(left,low,mid,l,r),query(right,mid+1,high,l,r)) ;
}
};
int main()
{
int n ;
scanf("%d",&n) ;
ll a[n+2] , pref[n+2] = {0} ;
for(int i = 1 ; i <= n ; ++i)
{
scanf("%lld",&a[i]) ;
pref[i] += (pref[i-1]+a[i]) ;
}
lazytree T ;
T.init(n) ;
for(int i = 1 ; i <= n ; ++i)
{
pair<ll,ll> cur = {1,pref[i]} , qc = T.query(1,1,n,i,i) ;
cur = max(cur,{qc.first,pref[i]-qc.second}) ;
int low = 0 , high = n ;
while(low<high)
{
int mid = (low+high)>>1 ;
if((pref[mid]-pref[i])>=cur.second) high = mid ;
else low = mid + 1 ;
}
if((pref[low]-pref[i])<cur.second) low = n + 1 ;
dp[i] = cur ;
T.update(1,1,n,low,n,{cur.first+1,pref[i]}) ;
}
printf("%lld\n",dp[n].first) ;
return 0 ;
}
Compilation message
segments.cpp: In member function 'void lazytree::shift(int, int, int)':
segments.cpp:27:13: warning: unused variable 'mid' [-Wunused-variable]
27 | int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
| ^~~
segments.cpp: In function 'int main()':
segments.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d",&n) ;
| ~~~~~^~~~~~~~~
segments.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%lld",&a[i]) ;
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
700 KB |
Output is correct |
32 |
Correct |
2 ms |
704 KB |
Output is correct |
33 |
Correct |
3 ms |
760 KB |
Output is correct |
34 |
Correct |
2 ms |
704 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
788 KB |
Output is correct |
37 |
Correct |
3 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
700 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
696 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
700 KB |
Output is correct |
44 |
Correct |
2 ms |
708 KB |
Output is correct |
45 |
Correct |
2 ms |
704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
700 KB |
Output is correct |
32 |
Correct |
2 ms |
704 KB |
Output is correct |
33 |
Correct |
3 ms |
760 KB |
Output is correct |
34 |
Correct |
2 ms |
704 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
788 KB |
Output is correct |
37 |
Correct |
3 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
700 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
696 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
700 KB |
Output is correct |
44 |
Correct |
2 ms |
708 KB |
Output is correct |
45 |
Correct |
2 ms |
704 KB |
Output is correct |
46 |
Correct |
42 ms |
16332 KB |
Output is correct |
47 |
Correct |
74 ms |
16364 KB |
Output is correct |
48 |
Correct |
82 ms |
16644 KB |
Output is correct |
49 |
Correct |
86 ms |
16972 KB |
Output is correct |
50 |
Correct |
86 ms |
16844 KB |
Output is correct |
51 |
Correct |
91 ms |
16824 KB |
Output is correct |
52 |
Correct |
40 ms |
8544 KB |
Output is correct |
53 |
Correct |
22 ms |
5196 KB |
Output is correct |
54 |
Correct |
78 ms |
16156 KB |
Output is correct |
55 |
Correct |
87 ms |
16924 KB |
Output is correct |
56 |
Correct |
86 ms |
16412 KB |
Output is correct |
57 |
Correct |
85 ms |
16296 KB |
Output is correct |
58 |
Correct |
78 ms |
16356 KB |
Output is correct |
59 |
Correct |
80 ms |
16504 KB |
Output is correct |
60 |
Correct |
82 ms |
16568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
700 KB |
Output is correct |
32 |
Correct |
2 ms |
704 KB |
Output is correct |
33 |
Correct |
3 ms |
760 KB |
Output is correct |
34 |
Correct |
2 ms |
704 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
4 ms |
788 KB |
Output is correct |
37 |
Correct |
3 ms |
724 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
4 ms |
700 KB |
Output is correct |
40 |
Correct |
2 ms |
724 KB |
Output is correct |
41 |
Correct |
2 ms |
696 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
700 KB |
Output is correct |
44 |
Correct |
2 ms |
708 KB |
Output is correct |
45 |
Correct |
2 ms |
704 KB |
Output is correct |
46 |
Correct |
42 ms |
16332 KB |
Output is correct |
47 |
Correct |
74 ms |
16364 KB |
Output is correct |
48 |
Correct |
82 ms |
16644 KB |
Output is correct |
49 |
Correct |
86 ms |
16972 KB |
Output is correct |
50 |
Correct |
86 ms |
16844 KB |
Output is correct |
51 |
Correct |
91 ms |
16824 KB |
Output is correct |
52 |
Correct |
40 ms |
8544 KB |
Output is correct |
53 |
Correct |
22 ms |
5196 KB |
Output is correct |
54 |
Correct |
78 ms |
16156 KB |
Output is correct |
55 |
Correct |
87 ms |
16924 KB |
Output is correct |
56 |
Correct |
86 ms |
16412 KB |
Output is correct |
57 |
Correct |
85 ms |
16296 KB |
Output is correct |
58 |
Correct |
78 ms |
16356 KB |
Output is correct |
59 |
Correct |
80 ms |
16504 KB |
Output is correct |
60 |
Correct |
82 ms |
16568 KB |
Output is correct |
61 |
Correct |
242 ms |
80484 KB |
Output is correct |
62 |
Correct |
320 ms |
80544 KB |
Output is correct |
63 |
Correct |
424 ms |
80800 KB |
Output is correct |
64 |
Correct |
472 ms |
83076 KB |
Output is correct |
65 |
Correct |
488 ms |
83360 KB |
Output is correct |
66 |
Correct |
475 ms |
83344 KB |
Output is correct |
67 |
Correct |
378 ms |
66828 KB |
Output is correct |
68 |
Correct |
179 ms |
33492 KB |
Output is correct |
69 |
Correct |
440 ms |
79532 KB |
Output is correct |
70 |
Correct |
506 ms |
83488 KB |
Output is correct |
71 |
Correct |
457 ms |
80584 KB |
Output is correct |
72 |
Correct |
458 ms |
80932 KB |
Output is correct |
73 |
Correct |
488 ms |
81468 KB |
Output is correct |
74 |
Correct |
432 ms |
81844 KB |
Output is correct |
75 |
Correct |
493 ms |
81808 KB |
Output is correct |