#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 5e5 + 5;
using pli = pair<i64, int>;
const i64 oo64 = 6e14 + 5;
#define left ___left
#define right ___right
struct node {
pli x = {0, 0};
node *left = nullptr, *right = nullptr;
node(pli x = {0, 0}, node *left = nullptr, node *right = nullptr) : x(x), left(left), right(right) {}
};
pli combine(const pli a, const pli b) {
if(a.second > b.second)
return a;
if(a.second < b.second)
return b;
return {max(a.first, b.first), a.second};
}
struct segment_tree {
node *root = new node();
void update(node *cur, i64 l, i64 r, i64 p, pli x) {
if(r < p or l > p) return;
if(l == r) {
cur -> x = combine(cur -> x, x);
return;
}
i64 m = (l + r) / 2;
if(cur -> left == nullptr) cur -> left = new node();
if(cur -> right == nullptr) cur -> right = new node();
if(p <= m)
update(cur -> left, l, m, p, x);
else
update(cur -> right, m + 1, r, p, x);
cur -> x = combine(cur -> left -> x, cur -> right -> x);
}
pli get(node *cur, i64 l, i64 r, i64 u, i64 v) {
if(r < u or l > v) return {0, 0};
if(u <= l and r <= v) return cur -> x;
i64 m = (l + r) / 2;
pli res = {0, 0};
if(cur -> left != nullptr)
res = combine(res, get(cur -> left, l, m, u, v));
if(cur -> right != nullptr)
res = combine(res, get(cur -> right, m + 1, r, u, v));
return res;
}
void update(i64 p, pli x) {
return update(root, 1, oo64, p, x);
}
pli get(i64 l, i64 r) {
return get(root, 1, oo64, l, r);
}
} ST;
int N;
int a[MAXN];
i64 b[MAXN];
pli dp[MAXN];
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 1; i <= N; i++) b[i] = b[i - 1] + a[i];
// for(int i = 1; i <= N; i++) {
// for(int j = i - 1; j >= 0; j--) {
// if(b[i] >= dp[j].first + b[j]) {
// dp[i] = {b[i] - b[j], dp[j].second + 1};
// break;
// }
// }
// }
// for(int i = 1; i <= N; i++)
// printf("%d %d %d\n", i, dp[i].first, dp[i].second);
ST.update(0, {0, 0});
for(int i = 1; i <= N; i++) {
pli res = ST.get(0, b[i]);
dp[i] = {b[i] - res.first, res.second + 1};
ST.update(dp[i].first + b[i], {b[i], dp[i].second});
}
// for(int i = 1; i <= N; i++)
// printf("%d %d %d\n", i, dp[i].first, dp[i].second);
cout << dp[N].second;
return (0 ^ 0);
}
/**
4
2 3 1 7
**/
/**
5
6 2 3 9 13
**/
/**
3
3 1 2
**/
Compilation message
segments.cpp: In function 'int main()':
segments.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(TASK ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
segments.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(TASK ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1624 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1624 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
6 ms |
5980 KB |
Output is correct |
33 |
Correct |
7 ms |
7256 KB |
Output is correct |
34 |
Correct |
6 ms |
7004 KB |
Output is correct |
35 |
Correct |
8 ms |
8796 KB |
Output is correct |
36 |
Correct |
7 ms |
8796 KB |
Output is correct |
37 |
Correct |
7 ms |
7256 KB |
Output is correct |
38 |
Correct |
5 ms |
5980 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
8 ms |
8284 KB |
Output is correct |
41 |
Correct |
3 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
2444 KB |
Output is correct |
43 |
Correct |
2 ms |
1232 KB |
Output is correct |
44 |
Correct |
4 ms |
3676 KB |
Output is correct |
45 |
Correct |
4 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1624 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
6 ms |
5980 KB |
Output is correct |
33 |
Correct |
7 ms |
7256 KB |
Output is correct |
34 |
Correct |
6 ms |
7004 KB |
Output is correct |
35 |
Correct |
8 ms |
8796 KB |
Output is correct |
36 |
Correct |
7 ms |
8796 KB |
Output is correct |
37 |
Correct |
7 ms |
7256 KB |
Output is correct |
38 |
Correct |
5 ms |
5980 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
8 ms |
8284 KB |
Output is correct |
41 |
Correct |
3 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
2444 KB |
Output is correct |
43 |
Correct |
2 ms |
1232 KB |
Output is correct |
44 |
Correct |
4 ms |
3676 KB |
Output is correct |
45 |
Correct |
4 ms |
3676 KB |
Output is correct |
46 |
Correct |
112 ms |
96760 KB |
Output is correct |
47 |
Correct |
132 ms |
111696 KB |
Output is correct |
48 |
Correct |
245 ms |
235748 KB |
Output is correct |
49 |
Correct |
239 ms |
254548 KB |
Output is correct |
50 |
Runtime error |
256 ms |
262144 KB |
Execution killed with signal 9 |
51 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1112 KB |
Output is correct |
17 |
Correct |
2 ms |
1372 KB |
Output is correct |
18 |
Correct |
2 ms |
1624 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
8 ms |
4700 KB |
Output is correct |
32 |
Correct |
6 ms |
5980 KB |
Output is correct |
33 |
Correct |
7 ms |
7256 KB |
Output is correct |
34 |
Correct |
6 ms |
7004 KB |
Output is correct |
35 |
Correct |
8 ms |
8796 KB |
Output is correct |
36 |
Correct |
7 ms |
8796 KB |
Output is correct |
37 |
Correct |
7 ms |
7256 KB |
Output is correct |
38 |
Correct |
5 ms |
5980 KB |
Output is correct |
39 |
Correct |
2 ms |
1116 KB |
Output is correct |
40 |
Correct |
8 ms |
8284 KB |
Output is correct |
41 |
Correct |
3 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
2444 KB |
Output is correct |
43 |
Correct |
2 ms |
1232 KB |
Output is correct |
44 |
Correct |
4 ms |
3676 KB |
Output is correct |
45 |
Correct |
4 ms |
3676 KB |
Output is correct |
46 |
Correct |
112 ms |
96760 KB |
Output is correct |
47 |
Correct |
132 ms |
111696 KB |
Output is correct |
48 |
Correct |
245 ms |
235748 KB |
Output is correct |
49 |
Correct |
239 ms |
254548 KB |
Output is correct |
50 |
Runtime error |
256 ms |
262144 KB |
Execution killed with signal 9 |
51 |
Halted |
0 ms |
0 KB |
- |