// 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;
mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
struct Node {
Node *L, *R;
int y;
ll x;
pii val, mx;
Node() {
L = R = NULL;
x = 0;
y = rnd();
val = mx = mp(0, 0);
}
Node(ll x, pii val) : x(x), val(val), mx(val) {
L = R = NULL;
y = rnd();
}
} *T;
void show(Node *&v) {
if (!v) {
return;
}
if (v -> L) {
show(v -> L);
}
printf("(%lld - %d %d) ", v -> x, v -> mx.fi, v -> mx.se);
if (v -> R) {
show(v -> R);
}
}
void showln(Node *&v) {
show(v);
printf("\n");
}
void recalc(Node *&v) {
if (!v) {
return;
}
v -> mx = v -> val;
if (v -> L) {
v -> mx = max(v -> mx, v -> L -> mx);
}
if (v -> R) {
v -> mx = max(v -> mx, v -> R -> mx);
}
}
void Merge(Node *&T, Node *L, Node *R) {
recalc(L);
recalc(R);
if (!L) {
T = R;
recalc(T);
return;
}
if (!R) {
T = L;
recalc(T);
return;
}
if (L -> y > R -> y) {
T = L;
Merge(T -> R, T -> R, R);
}
else {
T = R;
Merge(T -> L, L, T -> L);
}
recalc(T);
}
void Split(Node *T, Node *&L, Node *&R, ll x) {
recalc(T);
if (!T) {
L = R = NULL;
return;
}
if (T -> x < x) {
Split(T -> R, T -> R, R, x);
L = T;
}
else {
Split(T -> L, L, T -> L, x);
R = T;
}
recalc(L);
recalc(R);
}
void addEl(ll x, pii val) {
//printf("ADD %lld - (%d %d)\n", x, val.fi, val.se);
Node *L, *R, *M;
L = R = M = NULL;
Split(T, L, R, x);
Split(R, M, R, x + 1);
if (!M) {
M = new Node(x, val);
}
else {
M -> val = max(M -> val, val);
recalc(M);
}
Merge(T, L, M);
Merge(T, T, R);
}
pii prefMax(ll x) {
Node *L, *R;
L = R = NULL;
Split(T, L, R, x + 1);
pii ret = L -> mx;
Merge(T, L, R);
return ret;
}
pair<int, ll> dp[MAXN];
ll pref[MAXN];
int arr[MAXN];
int n;
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);
addEl(0, mp(0, 0));
for (int i = 1; i <= n; ++i) {
dp[i] = prefMax(pref[i]);
dp[i].fi++;
dp[i].se = pref[i] - pref[dp[i].se];
addEl(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:163:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
segments.cpp:166:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
5 ms |
672 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
5 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
5 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
640 KB |
Output is correct |
40 |
Correct |
5 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
3 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
5 ms |
672 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
5 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
5 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
640 KB |
Output is correct |
40 |
Correct |
5 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
3 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
46 |
Correct |
91 ms |
9728 KB |
Output is correct |
47 |
Correct |
212 ms |
9848 KB |
Output is correct |
48 |
Correct |
180 ms |
9848 KB |
Output is correct |
49 |
Correct |
169 ms |
9848 KB |
Output is correct |
50 |
Correct |
93 ms |
9720 KB |
Output is correct |
51 |
Correct |
202 ms |
9772 KB |
Output is correct |
52 |
Correct |
106 ms |
5372 KB |
Output is correct |
53 |
Correct |
54 ms |
3320 KB |
Output is correct |
54 |
Correct |
206 ms |
9080 KB |
Output is correct |
55 |
Correct |
180 ms |
9720 KB |
Output is correct |
56 |
Correct |
91 ms |
9848 KB |
Output is correct |
57 |
Correct |
126 ms |
9848 KB |
Output is correct |
58 |
Correct |
129 ms |
9464 KB |
Output is correct |
59 |
Correct |
105 ms |
9852 KB |
Output is correct |
60 |
Correct |
146 ms |
9936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 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 |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
5 ms |
672 KB |
Output is correct |
33 |
Correct |
5 ms |
640 KB |
Output is correct |
34 |
Correct |
5 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
5 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
5 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
640 KB |
Output is correct |
40 |
Correct |
5 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
3 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
46 |
Correct |
91 ms |
9728 KB |
Output is correct |
47 |
Correct |
212 ms |
9848 KB |
Output is correct |
48 |
Correct |
180 ms |
9848 KB |
Output is correct |
49 |
Correct |
169 ms |
9848 KB |
Output is correct |
50 |
Correct |
93 ms |
9720 KB |
Output is correct |
51 |
Correct |
202 ms |
9772 KB |
Output is correct |
52 |
Correct |
106 ms |
5372 KB |
Output is correct |
53 |
Correct |
54 ms |
3320 KB |
Output is correct |
54 |
Correct |
206 ms |
9080 KB |
Output is correct |
55 |
Correct |
180 ms |
9720 KB |
Output is correct |
56 |
Correct |
91 ms |
9848 KB |
Output is correct |
57 |
Correct |
126 ms |
9848 KB |
Output is correct |
58 |
Correct |
129 ms |
9464 KB |
Output is correct |
59 |
Correct |
105 ms |
9852 KB |
Output is correct |
60 |
Correct |
146 ms |
9936 KB |
Output is correct |
61 |
Correct |
621 ms |
46188 KB |
Output is correct |
62 |
Correct |
880 ms |
45816 KB |
Output is correct |
63 |
Correct |
1210 ms |
45816 KB |
Output is correct |
64 |
Correct |
1067 ms |
49908 KB |
Output is correct |
65 |
Correct |
524 ms |
50168 KB |
Output is correct |
66 |
Correct |
1118 ms |
50424 KB |
Output is correct |
67 |
Correct |
823 ms |
40240 KB |
Output is correct |
68 |
Correct |
418 ms |
20344 KB |
Output is correct |
69 |
Correct |
1081 ms |
44408 KB |
Output is correct |
70 |
Correct |
950 ms |
50296 KB |
Output is correct |
71 |
Correct |
516 ms |
47224 KB |
Output is correct |
72 |
Correct |
678 ms |
47728 KB |
Output is correct |
73 |
Correct |
645 ms |
48376 KB |
Output is correct |
74 |
Correct |
494 ms |
48636 KB |
Output is correct |
75 |
Correct |
583 ms |
48632 KB |
Output is correct |