#include<bits/stdc++.h>
#define ll long long
#define pii pair<ll,ll>
using namespace std;
const int mxn=1e6+5;
const int inf=1e9;
pii dp[mxn];
ll a[mxn], pre[mxn];
int n;
struct segtree {
pii lazy[4 * mxn] = {};
pii tree[4 * mxn] = {};
void propogate(int node) {
int left=node<<1;
int right=left|1;
tree[left] = max(tree[left], lazy[node]);
tree[right] = max(tree[right], lazy[node]);
lazy[left] = max(lazy[left], lazy[node]);
lazy[right] = max(lazy[right], lazy[node]);
lazy[node] = {0, 0};
}
void update(int node,int b,int e,int l,int r,pii v) {
if(e<l || b>r)return;
if(b>=l && e<=r) {
tree[node] = max(tree[node], v);
lazy[node] = max(lazy[node], v);
return;
}
if(lazy[node]!= make_pair(0ll, 0ll)) propogate(node);
int mid=b+e>>1;
int left=node<<1;
int right=left|1;
update(left,b,mid,l,r,v);
update(right,mid+1,e,l,r,v);
tree[node]=max(tree[left],tree[right]);
}
pii query(int node, int b, int e, int p) {
if(b == e) return tree[node];
int mid = b + e >> 1;
int left = node << 1;
int right = left | 1;
if(lazy[node] != make_pair(0ll, 0ll)) propogate(node);
if(p <= mid) return query(left, b, mid, p);
return query(right, mid + 1, e, p);
}
} st;
int main() {
cin >> n;
pre[0] = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
dp[0] = {0, 0};
for(int i = 1; i <= n; i++) {
dp[i] = {1, pre[i]};
auto x = st.query(1, 1, n, i);
dp[i] = max(dp[i], {x.first, pre[i] - x.second});
int lo = i + 1;
int hi = n;
int ind = n + 1;
while(lo <= hi) {
int mid = lo + hi >> 1;
if(pre[mid] >= dp[i].second + pre[i]) {
ind = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
st.update(1, 1, n, ind, n, make_pair(dp[i].first + 1ll, pre[i]));
}
cout<<dp[n].first<<endl;
}
Compilation message
segments.cpp: In member function 'void segtree::update(int, int, int, int, int, std::pair<long long int, long long int>)':
segments.cpp:37:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid=b+e>>1;
| ~^~
segments.cpp: In member function 'std::pair<long long int, long long int> segtree::query(int, int, int, int)':
segments.cpp:49:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int mid = b + e >> 1;
| ~~^~~
segments.cpp: In function 'int main()':
segments.cpp:77:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int mid = lo + hi >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
700 KB |
Output is correct |
33 |
Correct |
3 ms |
692 KB |
Output is correct |
34 |
Correct |
2 ms |
692 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
616 KB |
Output is correct |
38 |
Correct |
2 ms |
436 KB |
Output is correct |
39 |
Correct |
2 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
2 ms |
588 KB |
Output is correct |
42 |
Correct |
2 ms |
588 KB |
Output is correct |
43 |
Correct |
3 ms |
588 KB |
Output is correct |
44 |
Correct |
2 ms |
588 KB |
Output is correct |
45 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
700 KB |
Output is correct |
33 |
Correct |
3 ms |
692 KB |
Output is correct |
34 |
Correct |
2 ms |
692 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
616 KB |
Output is correct |
38 |
Correct |
2 ms |
436 KB |
Output is correct |
39 |
Correct |
2 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
2 ms |
588 KB |
Output is correct |
42 |
Correct |
2 ms |
588 KB |
Output is correct |
43 |
Correct |
3 ms |
588 KB |
Output is correct |
44 |
Correct |
2 ms |
588 KB |
Output is correct |
45 |
Correct |
2 ms |
588 KB |
Output is correct |
46 |
Correct |
28 ms |
3764 KB |
Output is correct |
47 |
Correct |
57 ms |
11712 KB |
Output is correct |
48 |
Correct |
79 ms |
12392 KB |
Output is correct |
49 |
Correct |
72 ms |
12500 KB |
Output is correct |
50 |
Correct |
69 ms |
12568 KB |
Output is correct |
51 |
Correct |
72 ms |
12564 KB |
Output is correct |
52 |
Correct |
35 ms |
6420 KB |
Output is correct |
53 |
Correct |
22 ms |
3600 KB |
Output is correct |
54 |
Correct |
51 ms |
11900 KB |
Output is correct |
55 |
Correct |
79 ms |
12620 KB |
Output is correct |
56 |
Correct |
55 ms |
12044 KB |
Output is correct |
57 |
Correct |
53 ms |
12024 KB |
Output is correct |
58 |
Correct |
57 ms |
11984 KB |
Output is correct |
59 |
Correct |
57 ms |
12212 KB |
Output is correct |
60 |
Correct |
61 ms |
12164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
3 ms |
700 KB |
Output is correct |
33 |
Correct |
3 ms |
692 KB |
Output is correct |
34 |
Correct |
2 ms |
692 KB |
Output is correct |
35 |
Correct |
2 ms |
716 KB |
Output is correct |
36 |
Correct |
2 ms |
716 KB |
Output is correct |
37 |
Correct |
3 ms |
616 KB |
Output is correct |
38 |
Correct |
2 ms |
436 KB |
Output is correct |
39 |
Correct |
2 ms |
716 KB |
Output is correct |
40 |
Correct |
3 ms |
588 KB |
Output is correct |
41 |
Correct |
2 ms |
588 KB |
Output is correct |
42 |
Correct |
2 ms |
588 KB |
Output is correct |
43 |
Correct |
3 ms |
588 KB |
Output is correct |
44 |
Correct |
2 ms |
588 KB |
Output is correct |
45 |
Correct |
2 ms |
588 KB |
Output is correct |
46 |
Correct |
28 ms |
3764 KB |
Output is correct |
47 |
Correct |
57 ms |
11712 KB |
Output is correct |
48 |
Correct |
79 ms |
12392 KB |
Output is correct |
49 |
Correct |
72 ms |
12500 KB |
Output is correct |
50 |
Correct |
69 ms |
12568 KB |
Output is correct |
51 |
Correct |
72 ms |
12564 KB |
Output is correct |
52 |
Correct |
35 ms |
6420 KB |
Output is correct |
53 |
Correct |
22 ms |
3600 KB |
Output is correct |
54 |
Correct |
51 ms |
11900 KB |
Output is correct |
55 |
Correct |
79 ms |
12620 KB |
Output is correct |
56 |
Correct |
55 ms |
12044 KB |
Output is correct |
57 |
Correct |
53 ms |
12024 KB |
Output is correct |
58 |
Correct |
57 ms |
11984 KB |
Output is correct |
59 |
Correct |
57 ms |
12212 KB |
Output is correct |
60 |
Correct |
61 ms |
12164 KB |
Output is correct |
61 |
Correct |
158 ms |
18048 KB |
Output is correct |
62 |
Correct |
220 ms |
34352 KB |
Output is correct |
63 |
Correct |
320 ms |
48064 KB |
Output is correct |
64 |
Correct |
362 ms |
53296 KB |
Output is correct |
65 |
Correct |
399 ms |
53644 KB |
Output is correct |
66 |
Correct |
371 ms |
53576 KB |
Output is correct |
67 |
Correct |
314 ms |
49440 KB |
Output is correct |
68 |
Correct |
145 ms |
24852 KB |
Output is correct |
69 |
Correct |
285 ms |
49892 KB |
Output is correct |
70 |
Correct |
389 ms |
53636 KB |
Output is correct |
71 |
Correct |
301 ms |
50692 KB |
Output is correct |
72 |
Correct |
289 ms |
51188 KB |
Output is correct |
73 |
Correct |
316 ms |
51628 KB |
Output is correct |
74 |
Correct |
327 ms |
52168 KB |
Output is correct |
75 |
Correct |
330 ms |
52096 KB |
Output is correct |