#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:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | 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:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
68 | int mid = tl + tr >> 1;
| ~~~^~~~
segments.cpp: In function 'void build(long long int, long long int, long long int)':
segments.cpp:82:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
82 | int mid = tl + tr >> 1;
| ~~~^~~~
segments.cpp: At global scope:
segments.cpp:87:10: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
87 | main() {
| ^
segments.cpp: In function 'int main()':
segments.cpp:101:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
101 | int mid = l + r >> 1;
| ~~^~~
segments.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
91 | scanf("%lld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
512 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
512 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 |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
3 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
636 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
512 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 |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
3 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
636 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
46 |
Correct |
31 ms |
4608 KB |
Output is correct |
47 |
Correct |
118 ms |
7492 KB |
Output is correct |
48 |
Correct |
134 ms |
8028 KB |
Output is correct |
49 |
Correct |
139 ms |
8092 KB |
Output is correct |
50 |
Correct |
117 ms |
8264 KB |
Output is correct |
51 |
Correct |
138 ms |
8188 KB |
Output is correct |
52 |
Correct |
86 ms |
4344 KB |
Output is correct |
53 |
Correct |
57 ms |
2680 KB |
Output is correct |
54 |
Correct |
149 ms |
7416 KB |
Output is correct |
55 |
Correct |
148 ms |
8184 KB |
Output is correct |
56 |
Correct |
115 ms |
7660 KB |
Output is correct |
57 |
Correct |
119 ms |
7544 KB |
Output is correct |
58 |
Correct |
130 ms |
7672 KB |
Output is correct |
59 |
Correct |
116 ms |
7800 KB |
Output is correct |
60 |
Correct |
130 ms |
7800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
512 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 |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
3 ms |
640 KB |
Output is correct |
33 |
Correct |
3 ms |
640 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
3 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
3 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
512 KB |
Output is correct |
39 |
Correct |
4 ms |
640 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
640 KB |
Output is correct |
43 |
Correct |
3 ms |
636 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
3 ms |
640 KB |
Output is correct |
46 |
Correct |
31 ms |
4608 KB |
Output is correct |
47 |
Correct |
118 ms |
7492 KB |
Output is correct |
48 |
Correct |
134 ms |
8028 KB |
Output is correct |
49 |
Correct |
139 ms |
8092 KB |
Output is correct |
50 |
Correct |
117 ms |
8264 KB |
Output is correct |
51 |
Correct |
138 ms |
8188 KB |
Output is correct |
52 |
Correct |
86 ms |
4344 KB |
Output is correct |
53 |
Correct |
57 ms |
2680 KB |
Output is correct |
54 |
Correct |
149 ms |
7416 KB |
Output is correct |
55 |
Correct |
148 ms |
8184 KB |
Output is correct |
56 |
Correct |
115 ms |
7660 KB |
Output is correct |
57 |
Correct |
119 ms |
7544 KB |
Output is correct |
58 |
Correct |
130 ms |
7672 KB |
Output is correct |
59 |
Correct |
116 ms |
7800 KB |
Output is correct |
60 |
Correct |
130 ms |
7800 KB |
Output is correct |
61 |
Correct |
156 ms |
20216 KB |
Output is correct |
62 |
Correct |
402 ms |
26488 KB |
Output is correct |
63 |
Correct |
627 ms |
31788 KB |
Output is correct |
64 |
Correct |
789 ms |
35112 KB |
Output is correct |
65 |
Correct |
736 ms |
35508 KB |
Output is correct |
66 |
Correct |
768 ms |
35512 KB |
Output is correct |
67 |
Correct |
542 ms |
29696 KB |
Output is correct |
68 |
Correct |
269 ms |
15856 KB |
Output is correct |
69 |
Correct |
707 ms |
31656 KB |
Output is correct |
70 |
Correct |
711 ms |
35524 KB |
Output is correct |
71 |
Correct |
653 ms |
32532 KB |
Output is correct |
72 |
Correct |
689 ms |
32964 KB |
Output is correct |
73 |
Correct |
707 ms |
33600 KB |
Output is correct |
74 |
Correct |
630 ms |
33952 KB |
Output is correct |
75 |
Correct |
659 ms |
34040 KB |
Output is correct |