// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = (int)5e5 + 5;
const ll INF = (ll)1e16;
struct Node {
Node *c1, *c2;
pair<int, int> mx;
Node() {
c1 = c2 = NULL;
mx = mp(0, 0);
}
} *root;
pair<int, ll> dp[MAXN];
ll pref[MAXN];
int arr[MAXN];
int n;
void recalc(Node *&v) {
v -> mx = mp(0, 0);
if (v -> c1) {
v -> mx = max(v -> mx, v -> c1 -> mx);
}
if (v -> c2) {
v -> mx = max(v -> mx, v -> c2 -> mx);
}
}
void update(Node *&v, ll tl, ll tr, ll p, pii x) {
if (!v) {
v = new Node();
}
if (tl == tr) {
v -> mx = max(v -> mx, x);
return;
}
ll mid = (tl + tr) >> 1;
if (p <= mid) {
update(v -> c1, tl, mid, p, x);
}
else {
update(v -> c2, mid + 1, tr, p, x);
}
recalc(v);
}
pii segMax(Node *&v, ll tl, ll tr, ll l, ll r) {
if (!v || l > r || tl > r || tr < l) {
return mp(0, 0);
}
if (l <= tl && tr <= r) {
return v -> mx;
}
ll mid = (tl + tr) >> 1;
return max(segMax(v -> c1, tl, mid, l, r), segMax(v -> c2, mid + 1, tr, l, r));
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
pref[i] = pref[i - 1] + arr[i];
}
for (int i = 1; i <= n; ++i) {
dp[i] = mp(0, INF);
}
dp[0] = mp(0, 0);
update(root, 0, INF, 0, mp(0, 0));
for (int i = 1; i <= n; ++i) {
dp[i] = segMax(root, 0, INF, 0, pref[i]);
dp[i].fi++;
dp[i].se = pref[i] - pref[dp[i].se];
update(root, 0, INF, dp[i].se + pref[i], mp(dp[i].fi, i));
}
printf("%d\n", dp[n].fi);
}
int main() {
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
segments.cpp: In function 'void solve()':
segments.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
segments.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
2 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
768 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
2 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
768 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2688 KB |
Output is correct |
35 |
Correct |
9 ms |
3200 KB |
Output is correct |
36 |
Correct |
7 ms |
3200 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
5 ms |
2304 KB |
Output is correct |
39 |
Correct |
7 ms |
768 KB |
Output is correct |
40 |
Correct |
8 ms |
3072 KB |
Output is correct |
41 |
Correct |
4 ms |
768 KB |
Output is correct |
42 |
Correct |
5 ms |
1152 KB |
Output is correct |
43 |
Correct |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
6 ms |
1536 KB |
Output is correct |
45 |
Correct |
6 ms |
1536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
2 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
768 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2688 KB |
Output is correct |
35 |
Correct |
9 ms |
3200 KB |
Output is correct |
36 |
Correct |
7 ms |
3200 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
5 ms |
2304 KB |
Output is correct |
39 |
Correct |
7 ms |
768 KB |
Output is correct |
40 |
Correct |
8 ms |
3072 KB |
Output is correct |
41 |
Correct |
4 ms |
768 KB |
Output is correct |
42 |
Correct |
5 ms |
1152 KB |
Output is correct |
43 |
Correct |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
6 ms |
1536 KB |
Output is correct |
45 |
Correct |
6 ms |
1536 KB |
Output is correct |
46 |
Correct |
134 ms |
37344 KB |
Output is correct |
47 |
Correct |
175 ms |
42348 KB |
Output is correct |
48 |
Correct |
207 ms |
83576 KB |
Output is correct |
49 |
Correct |
233 ms |
89848 KB |
Output is correct |
50 |
Correct |
267 ms |
99064 KB |
Output is correct |
51 |
Correct |
228 ms |
97568 KB |
Output is correct |
52 |
Correct |
120 ms |
49144 KB |
Output is correct |
53 |
Correct |
67 ms |
29560 KB |
Output is correct |
54 |
Correct |
134 ms |
14584 KB |
Output is correct |
55 |
Correct |
249 ms |
94328 KB |
Output is correct |
56 |
Correct |
196 ms |
44280 KB |
Output is correct |
57 |
Correct |
174 ms |
36732 KB |
Output is correct |
58 |
Correct |
295 ms |
40440 KB |
Output is correct |
59 |
Correct |
213 ms |
56824 KB |
Output is correct |
60 |
Correct |
183 ms |
56568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
640 KB |
Output is correct |
17 |
Correct |
1 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
768 KB |
Output is correct |
19 |
Correct |
2 ms |
768 KB |
Output is correct |
20 |
Correct |
2 ms |
768 KB |
Output is correct |
21 |
Correct |
2 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
1920 KB |
Output is correct |
32 |
Correct |
6 ms |
2432 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2688 KB |
Output is correct |
35 |
Correct |
9 ms |
3200 KB |
Output is correct |
36 |
Correct |
7 ms |
3200 KB |
Output is correct |
37 |
Correct |
6 ms |
2816 KB |
Output is correct |
38 |
Correct |
5 ms |
2304 KB |
Output is correct |
39 |
Correct |
7 ms |
768 KB |
Output is correct |
40 |
Correct |
8 ms |
3072 KB |
Output is correct |
41 |
Correct |
4 ms |
768 KB |
Output is correct |
42 |
Correct |
5 ms |
1152 KB |
Output is correct |
43 |
Correct |
4 ms |
768 KB |
Output is correct |
44 |
Correct |
6 ms |
1536 KB |
Output is correct |
45 |
Correct |
6 ms |
1536 KB |
Output is correct |
46 |
Correct |
134 ms |
37344 KB |
Output is correct |
47 |
Correct |
175 ms |
42348 KB |
Output is correct |
48 |
Correct |
207 ms |
83576 KB |
Output is correct |
49 |
Correct |
233 ms |
89848 KB |
Output is correct |
50 |
Correct |
267 ms |
99064 KB |
Output is correct |
51 |
Correct |
228 ms |
97568 KB |
Output is correct |
52 |
Correct |
120 ms |
49144 KB |
Output is correct |
53 |
Correct |
67 ms |
29560 KB |
Output is correct |
54 |
Correct |
134 ms |
14584 KB |
Output is correct |
55 |
Correct |
249 ms |
94328 KB |
Output is correct |
56 |
Correct |
196 ms |
44280 KB |
Output is correct |
57 |
Correct |
174 ms |
36732 KB |
Output is correct |
58 |
Correct |
295 ms |
40440 KB |
Output is correct |
59 |
Correct |
213 ms |
56824 KB |
Output is correct |
60 |
Correct |
183 ms |
56568 KB |
Output is correct |
61 |
Correct |
660 ms |
174120 KB |
Output is correct |
62 |
Correct |
690 ms |
166520 KB |
Output is correct |
63 |
Correct |
796 ms |
212856 KB |
Output is correct |
64 |
Runtime error |
703 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Halted |
0 ms |
0 KB |
- |