#include <bits/stdc++.h>
using namespace std;
struct Node {
int mx;
Node() {
mx = -1e9;
}
Node(int val) {
mx = val;
}
};
struct Seg {
int n;
vector<Node> st;
Node merge(Node &l, Node &r) {
Node res;
res.mx = max(l.mx, r.mx);
return res;
}
Seg(int _n) {
this->n = _n;
st.assign(4 * _n, Node());
}
void update(int l, int r, int x, int val, int node) {
if (l > x || r < x) {
return;
}
if (l == r) {
st[node].mx = max(st[node].mx, val);
return;
}
int m = (l + r) / 2;
update(l, m, x, val, 2 * node + 1);
update(m + 1, r, x, val, 2 * node + 2);
st[node] = merge(st[2 * node + 1], st[2 * node + 2]);
}
void update(int x, int val) {
update(0, n - 1, x, val, 0);
}
Node query(int start, int end, int l, int r, int node) {
if (end < l || start > r) {
return Node();
}
if (start >= l && end <= r) {
return st[node];
}
int mid = (start + end) / 2;
Node left = query(start, mid, l, r, 2 * node + 1);
Node right = query(mid + 1, end, l, r, 2 * node + 2);
Node res = merge(left, right);
return res;
}
int query(int l, int r) {
return query(0, n - 1, l, r, 0).mx;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, x;
cin >> n;
vector<long long> pref(n + 1);
for (int i = 0; i < n; ++i) {
cin >> x;
pref[i + 1] = pref[i] + x;
}
auto mx = [&](int l, int r) -> long long {
return pref[r] - (l ? pref[l - 1] : 0);
};
vector<Seg> ds(n + 1, Seg(n + 1));
ds[0].update(0, 0);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
int low = 0, high = j - 1;
int best = -1;
while (low <= high) {
int mid = (low + high) / 2;
if (mx(mid, j - 1) <= mx(j, i)) {
best = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if (best != -1) {
ds[i].update(j, 1 + ds[j - 1].query(best, j - 1));
}
}
}
cout << ds[n].query(0, n) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
19 ms |
4420 KB |
Output is correct |
17 |
Correct |
15 ms |
4188 KB |
Output is correct |
18 |
Correct |
16 ms |
4188 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4420 KB |
Output is correct |
21 |
Correct |
17 ms |
4188 KB |
Output is correct |
22 |
Correct |
10 ms |
2908 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
16 ms |
4416 KB |
Output is correct |
25 |
Correct |
23 ms |
4188 KB |
Output is correct |
26 |
Correct |
16 ms |
4188 KB |
Output is correct |
27 |
Correct |
16 ms |
4188 KB |
Output is correct |
28 |
Correct |
19 ms |
4416 KB |
Output is correct |
29 |
Correct |
15 ms |
4184 KB |
Output is correct |
30 |
Correct |
16 ms |
4416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
19 ms |
4420 KB |
Output is correct |
17 |
Correct |
15 ms |
4188 KB |
Output is correct |
18 |
Correct |
16 ms |
4188 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4420 KB |
Output is correct |
21 |
Correct |
17 ms |
4188 KB |
Output is correct |
22 |
Correct |
10 ms |
2908 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
16 ms |
4416 KB |
Output is correct |
25 |
Correct |
23 ms |
4188 KB |
Output is correct |
26 |
Correct |
16 ms |
4188 KB |
Output is correct |
27 |
Correct |
16 ms |
4188 KB |
Output is correct |
28 |
Correct |
19 ms |
4416 KB |
Output is correct |
29 |
Correct |
15 ms |
4184 KB |
Output is correct |
30 |
Correct |
16 ms |
4416 KB |
Output is correct |
31 |
Correct |
1116 ms |
141628 KB |
Output is correct |
32 |
Correct |
934 ms |
141640 KB |
Output is correct |
33 |
Correct |
949 ms |
141652 KB |
Output is correct |
34 |
Correct |
998 ms |
141636 KB |
Output is correct |
35 |
Correct |
1002 ms |
141636 KB |
Output is correct |
36 |
Correct |
982 ms |
141632 KB |
Output is correct |
37 |
Correct |
669 ms |
98640 KB |
Output is correct |
38 |
Correct |
406 ms |
63320 KB |
Output is correct |
39 |
Correct |
984 ms |
141632 KB |
Output is correct |
40 |
Correct |
990 ms |
141860 KB |
Output is correct |
41 |
Correct |
924 ms |
141640 KB |
Output is correct |
42 |
Correct |
922 ms |
141652 KB |
Output is correct |
43 |
Correct |
1074 ms |
141396 KB |
Output is correct |
44 |
Correct |
905 ms |
141636 KB |
Output is correct |
45 |
Correct |
931 ms |
141640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
19 ms |
4420 KB |
Output is correct |
17 |
Correct |
15 ms |
4188 KB |
Output is correct |
18 |
Correct |
16 ms |
4188 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4420 KB |
Output is correct |
21 |
Correct |
17 ms |
4188 KB |
Output is correct |
22 |
Correct |
10 ms |
2908 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
16 ms |
4416 KB |
Output is correct |
25 |
Correct |
23 ms |
4188 KB |
Output is correct |
26 |
Correct |
16 ms |
4188 KB |
Output is correct |
27 |
Correct |
16 ms |
4188 KB |
Output is correct |
28 |
Correct |
19 ms |
4416 KB |
Output is correct |
29 |
Correct |
15 ms |
4184 KB |
Output is correct |
30 |
Correct |
16 ms |
4416 KB |
Output is correct |
31 |
Correct |
1116 ms |
141628 KB |
Output is correct |
32 |
Correct |
934 ms |
141640 KB |
Output is correct |
33 |
Correct |
949 ms |
141652 KB |
Output is correct |
34 |
Correct |
998 ms |
141636 KB |
Output is correct |
35 |
Correct |
1002 ms |
141636 KB |
Output is correct |
36 |
Correct |
982 ms |
141632 KB |
Output is correct |
37 |
Correct |
669 ms |
98640 KB |
Output is correct |
38 |
Correct |
406 ms |
63320 KB |
Output is correct |
39 |
Correct |
984 ms |
141632 KB |
Output is correct |
40 |
Correct |
990 ms |
141860 KB |
Output is correct |
41 |
Correct |
924 ms |
141640 KB |
Output is correct |
42 |
Correct |
922 ms |
141652 KB |
Output is correct |
43 |
Correct |
1074 ms |
141396 KB |
Output is correct |
44 |
Correct |
905 ms |
141636 KB |
Output is correct |
45 |
Correct |
931 ms |
141640 KB |
Output is correct |
46 |
Runtime error |
115 ms |
262144 KB |
Execution killed with signal 9 |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 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 |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
19 ms |
4420 KB |
Output is correct |
17 |
Correct |
15 ms |
4188 KB |
Output is correct |
18 |
Correct |
16 ms |
4188 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4420 KB |
Output is correct |
21 |
Correct |
17 ms |
4188 KB |
Output is correct |
22 |
Correct |
10 ms |
2908 KB |
Output is correct |
23 |
Correct |
7 ms |
1884 KB |
Output is correct |
24 |
Correct |
16 ms |
4416 KB |
Output is correct |
25 |
Correct |
23 ms |
4188 KB |
Output is correct |
26 |
Correct |
16 ms |
4188 KB |
Output is correct |
27 |
Correct |
16 ms |
4188 KB |
Output is correct |
28 |
Correct |
19 ms |
4416 KB |
Output is correct |
29 |
Correct |
15 ms |
4184 KB |
Output is correct |
30 |
Correct |
16 ms |
4416 KB |
Output is correct |
31 |
Correct |
1116 ms |
141628 KB |
Output is correct |
32 |
Correct |
934 ms |
141640 KB |
Output is correct |
33 |
Correct |
949 ms |
141652 KB |
Output is correct |
34 |
Correct |
998 ms |
141636 KB |
Output is correct |
35 |
Correct |
1002 ms |
141636 KB |
Output is correct |
36 |
Correct |
982 ms |
141632 KB |
Output is correct |
37 |
Correct |
669 ms |
98640 KB |
Output is correct |
38 |
Correct |
406 ms |
63320 KB |
Output is correct |
39 |
Correct |
984 ms |
141632 KB |
Output is correct |
40 |
Correct |
990 ms |
141860 KB |
Output is correct |
41 |
Correct |
924 ms |
141640 KB |
Output is correct |
42 |
Correct |
922 ms |
141652 KB |
Output is correct |
43 |
Correct |
1074 ms |
141396 KB |
Output is correct |
44 |
Correct |
905 ms |
141636 KB |
Output is correct |
45 |
Correct |
931 ms |
141640 KB |
Output is correct |
46 |
Runtime error |
115 ms |
262144 KB |
Execution killed with signal 9 |
47 |
Halted |
0 ms |
0 KB |
- |