#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = (int)1e6 + 5;
const int INF = (int)1e18;
int a[N];
int tree[N * 4], lz[N * 4];
int pref[N];
int dp[N];
void mk(int &a, int other) {
if (!a) {
a = other;
return;
}
if (dp[a - 1] == dp[other - 1]) {
a = max(a, other);
}
if (dp[a - 1] < dp[other - 1]) {
a = other;
}
}
int get_sum(int l, int r) {
return pref[r] - pref[l - 1];
}
void push(int v, int tl, int tr) {
if (!lz[v]) {
return;
}
if (tl != tr) {
mk(lz[v + v], lz[v]);
mk(lz[v + v + 1], lz[v]);
} else {
int x = lz[v];
if (dp[x - 1] + 1 == dp[tl]) {
tree[v] = max(tree[v], lz[v]);
}
if (dp[x - 1] + 1 > dp[tl]) {
tree[v] = x;
dp[tl] = dp[x - 1] + 1;
}
}
}
void upd(int l, int r, int val, int v = 1, int tl = 1, int tr = N) {
push(v, tl, tr);
if (l <= tl && tr <= r) {
lz[v] = val;
push(v, tl, tr);
return;
}
if (l > tr || tl > r) {
return;
}
int mid = tl + tr >> 1;
upd(l, r, val, v + v, tl, mid);
upd(l, r, val, v + v + 1, mid + 1, tr);
}
int get(int pos, int v = 1, int tl = 1, int tr = N) {
push(v, tl, tr);
if (tl == tr) {
return tree[v];
}
int mid = tl + tr >> 1;
if (pos <= mid) {
return get(pos, v + v, tl, mid);
} else {
return get(pos, v + v + 1, mid + 1, tr);
}
}
void build(int v, int tl, int tr) {
if (tl == tr) {
tree[v] = 1;
dp[tl] = 1;
return;
}
int mid = tl + tr >> 1;
build(v + v, tl, mid);
build(v + v + 1, mid + 1, tr);
}
main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
pref[i] = pref[i - 1] + a[i];
}
build(1, 1, n);
for (int i = 1; i <= n; i++) {
int best = get(i);
int l = i, r = n + 1;
while (r - l > 1) {
int mid = l + r >> 1;
if (get_sum(i + 1, mid) >= get_sum(best, i)) {
r = mid;
} else {
l = mid;
}
}
if (r <= n) {
upd(r, n, i + 1);
}
}
cout << dp[n];
}
Compilation message
segments.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int, long long int)':
segments.cpp:58:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
segments.cpp: In function 'long long int get(long long int, long long int, long long int, long long int)':
segments.cpp:68:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
segments.cpp: In function 'void build(long long int, long long int, long long int)':
segments.cpp:82:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
segments.cpp: At global scope:
segments.cpp:87:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
segments.cpp: In function 'int main()':
segments.cpp:101:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
segments.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &a[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
7 ms |
640 KB |
Output is correct |
36 |
Correct |
8 ms |
640 KB |
Output is correct |
37 |
Correct |
7 ms |
640 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
8 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
7 ms |
640 KB |
Output is correct |
44 |
Correct |
7 ms |
640 KB |
Output is correct |
45 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
7 ms |
640 KB |
Output is correct |
36 |
Correct |
8 ms |
640 KB |
Output is correct |
37 |
Correct |
7 ms |
640 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
8 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
7 ms |
640 KB |
Output is correct |
44 |
Correct |
7 ms |
640 KB |
Output is correct |
45 |
Correct |
7 ms |
640 KB |
Output is correct |
46 |
Correct |
32 ms |
4736 KB |
Output is correct |
47 |
Correct |
116 ms |
7548 KB |
Output is correct |
48 |
Correct |
129 ms |
7928 KB |
Output is correct |
49 |
Correct |
121 ms |
8184 KB |
Output is correct |
50 |
Correct |
121 ms |
8160 KB |
Output is correct |
51 |
Correct |
138 ms |
8184 KB |
Output is correct |
52 |
Correct |
69 ms |
4216 KB |
Output is correct |
53 |
Correct |
41 ms |
2680 KB |
Output is correct |
54 |
Correct |
122 ms |
7416 KB |
Output is correct |
55 |
Correct |
126 ms |
8184 KB |
Output is correct |
56 |
Correct |
116 ms |
7672 KB |
Output is correct |
57 |
Correct |
122 ms |
7672 KB |
Output is correct |
58 |
Correct |
118 ms |
7740 KB |
Output is correct |
59 |
Correct |
116 ms |
7800 KB |
Output is correct |
60 |
Correct |
119 ms |
7732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
7 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
640 KB |
Output is correct |
34 |
Correct |
7 ms |
640 KB |
Output is correct |
35 |
Correct |
7 ms |
640 KB |
Output is correct |
36 |
Correct |
8 ms |
640 KB |
Output is correct |
37 |
Correct |
7 ms |
640 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
8 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
7 ms |
640 KB |
Output is correct |
44 |
Correct |
7 ms |
640 KB |
Output is correct |
45 |
Correct |
7 ms |
640 KB |
Output is correct |
46 |
Correct |
32 ms |
4736 KB |
Output is correct |
47 |
Correct |
116 ms |
7548 KB |
Output is correct |
48 |
Correct |
129 ms |
7928 KB |
Output is correct |
49 |
Correct |
121 ms |
8184 KB |
Output is correct |
50 |
Correct |
121 ms |
8160 KB |
Output is correct |
51 |
Correct |
138 ms |
8184 KB |
Output is correct |
52 |
Correct |
69 ms |
4216 KB |
Output is correct |
53 |
Correct |
41 ms |
2680 KB |
Output is correct |
54 |
Correct |
122 ms |
7416 KB |
Output is correct |
55 |
Correct |
126 ms |
8184 KB |
Output is correct |
56 |
Correct |
116 ms |
7672 KB |
Output is correct |
57 |
Correct |
122 ms |
7672 KB |
Output is correct |
58 |
Correct |
118 ms |
7740 KB |
Output is correct |
59 |
Correct |
116 ms |
7800 KB |
Output is correct |
60 |
Correct |
119 ms |
7732 KB |
Output is correct |
61 |
Correct |
158 ms |
20216 KB |
Output is correct |
62 |
Correct |
352 ms |
26540 KB |
Output is correct |
63 |
Correct |
606 ms |
31736 KB |
Output is correct |
64 |
Correct |
699 ms |
35192 KB |
Output is correct |
65 |
Correct |
637 ms |
35576 KB |
Output is correct |
66 |
Correct |
707 ms |
35364 KB |
Output is correct |
67 |
Correct |
538 ms |
29688 KB |
Output is correct |
68 |
Correct |
247 ms |
15992 KB |
Output is correct |
69 |
Correct |
664 ms |
31736 KB |
Output is correct |
70 |
Correct |
691 ms |
35576 KB |
Output is correct |
71 |
Correct |
619 ms |
32504 KB |
Output is correct |
72 |
Correct |
630 ms |
33164 KB |
Output is correct |
73 |
Correct |
646 ms |
33528 KB |
Output is correct |
74 |
Correct |
621 ms |
34040 KB |
Output is correct |
75 |
Correct |
635 ms |
33912 KB |
Output is correct |