#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <vector>
#include <bitset>
#include <queue>
#include <set>
typedef long long llong;
const int MAXN = 500000 + 10;
const llong INF = 1e18;
int n;
llong prefix[MAXN];
struct SegmentTree
{
struct Node
{
int maxDP;
int lazyDP;
llong minLast;
llong lazyLast;
Node()
{
maxDP = 0;
minLast = INF;
lazyDP = lazyLast = -1;
}
friend Node operator + (const Node &left, const Node &right)
{
Node res;
res.maxDP = std::max(left.maxDP, right.maxDP);
res.minLast = std::min(left.minLast, right.minLast);
return res;
}
};
Node tree[4*MAXN];
void push(int node, int l, int r)
{
if (tree[node].lazyDP != -1)
{
tree[node].maxDP = tree[node].lazyDP;
if (l < r)
{
tree[2*node].lazyDP = tree[node].lazyDP;
tree[2*node + 1].lazyDP = tree[node].lazyDP;
}
}
if (tree[node].lazyLast != -1)
{
tree[node].minLast = tree[node].lazyLast;
if (l < r)
{
tree[2*node].lazyLast = tree[node].lazyLast;
tree[2*node + 1].lazyLast = tree[node].lazyLast;
}
}
tree[node].lazyDP = -1;
tree[node].lazyLast = -1;
}
void update(int l, int r, int node, int queryL, int queryR, int dpValue, llong lastValue)
{
push(node, l, r);
if (queryR < l || r < queryL)
{
return;
}
if (queryL <= l && r <= queryR)
{
tree[node].lazyDP = dpValue;
tree[node].lazyLast = lastValue;
push(node, l, r);
return;
}
int mid = (l + r) / 2;
update(l, mid, 2*node, queryL, queryR, dpValue, lastValue);
update(mid + 1, r, 2*node + 1, queryL, queryR, dpValue, lastValue);
tree[node] = tree[2*node] + tree[2*node + 1];
}
int searchLastDP(int l, int r, int node, int queryL, int queryR, int value)
{
push(node, l, r);
if (queryR < l || r < queryL)
{
return -1;
}
if (queryL <= l && r <= queryR && tree[node].maxDP > value)
{
return -1;
}
if (l == r)
{
return l;
}
int mid = (l + r) / 2;
int res = searchLastDP(mid + 1, r, 2*node + 1, queryL, queryR, value);
if (res != -1) return res;
return searchLastDP(l, mid, 2*node, queryL, queryR, value);
}
int searchFirstLast(int l, int r, int node, int queryL, int queryR, llong value)
{
push(node, l, r);
if (queryR < l || r < queryL)
{
return -1;
}
if (queryL <= l && r <= queryR && tree[node].minLast > value)
{
return -1;
}
if (l == r)
{
return l;
}
int mid = (l + r) / 2;
int res = searchFirstLast(l, mid, 2*node, queryL, queryR, value);
if (res != -1) return res;
return searchFirstLast(mid + 1, r, 2*node + 1, queryL, queryR, value);
}
Node getNode(int l, int r, int node, int queryPos)
{
push(node, l, r);
if (l == r)
{
return tree[node];
}
int mid = (l + r) / 2;
if (queryPos <= mid) return getNode(l, mid, 2*node, queryPos);
else return getNode(mid + 1, r, 2*node + 1, queryPos);
}
void update(int l, int r, int dp, llong last)
{
update(1, n, 1, l, r, dp, last);
}
int searchLastDP(int l, int r, int value)
{
return searchLastDP(1, n, 1, l, r, value);
}
int searchFirstLast(int l, int r, llong value)
{
return searchFirstLast(1, n, 1, l, r, value);
}
Node getNode(int pos)
{
return getNode(1, n, 1, pos);
}
};
int a[MAXN];
SegmentTree tree;
int searchFirstPrefix(llong value)
{
int l = 0, r = n + 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (prefix[mid] < value) l = mid;
else r = mid;
}
return r;
}
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
prefix[i] = prefix[i - 1] + a[i];
}
for (int i = 0 ; i <= n ; ++i)
{
int dp = 0;
llong last = 0;
if (i > 0)
{
SegmentTree::Node curr = tree.getNode(i);
dp = curr.maxDP;
last = prefix[i] - curr.minLast;
}
int first = searchFirstPrefix(prefix[i] + last);
if (first > n) continue;
int equalDP = tree.searchLastDP(first, n, dp);
if (first <= equalDP)
{
tree.update(first, equalDP, dp + 1, prefix[i]);
}
equalDP = std::max(first, equalDP + 1);
int biggerDP = tree.searchLastDP(first, n, dp + 1);
if (equalDP <= biggerDP)
{
int firstLess = tree.searchFirstLast(equalDP, biggerDP, prefix[i]);
if (firstLess != -1 && firstLess <= biggerDP)
{
tree.update(firstLess, biggerDP, dp + 1, prefix[i]);
}
}
}
std::cout << tree.getNode(n).maxDP << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> a[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47756 KB |
Output is correct |
2 |
Correct |
7 ms |
47708 KB |
Output is correct |
3 |
Correct |
7 ms |
47708 KB |
Output is correct |
4 |
Correct |
7 ms |
47756 KB |
Output is correct |
5 |
Correct |
8 ms |
47708 KB |
Output is correct |
6 |
Correct |
7 ms |
47704 KB |
Output is correct |
7 |
Correct |
7 ms |
47704 KB |
Output is correct |
8 |
Correct |
8 ms |
47704 KB |
Output is correct |
9 |
Correct |
7 ms |
47708 KB |
Output is correct |
10 |
Correct |
8 ms |
47708 KB |
Output is correct |
11 |
Correct |
7 ms |
47708 KB |
Output is correct |
12 |
Correct |
8 ms |
47704 KB |
Output is correct |
13 |
Correct |
8 ms |
47708 KB |
Output is correct |
14 |
Correct |
7 ms |
47708 KB |
Output is correct |
15 |
Correct |
8 ms |
47784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47756 KB |
Output is correct |
2 |
Correct |
7 ms |
47708 KB |
Output is correct |
3 |
Correct |
7 ms |
47708 KB |
Output is correct |
4 |
Correct |
7 ms |
47756 KB |
Output is correct |
5 |
Correct |
8 ms |
47708 KB |
Output is correct |
6 |
Correct |
7 ms |
47704 KB |
Output is correct |
7 |
Correct |
7 ms |
47704 KB |
Output is correct |
8 |
Correct |
8 ms |
47704 KB |
Output is correct |
9 |
Correct |
7 ms |
47708 KB |
Output is correct |
10 |
Correct |
8 ms |
47708 KB |
Output is correct |
11 |
Correct |
7 ms |
47708 KB |
Output is correct |
12 |
Correct |
8 ms |
47704 KB |
Output is correct |
13 |
Correct |
8 ms |
47708 KB |
Output is correct |
14 |
Correct |
7 ms |
47708 KB |
Output is correct |
15 |
Correct |
8 ms |
47784 KB |
Output is correct |
16 |
Correct |
8 ms |
47708 KB |
Output is correct |
17 |
Correct |
8 ms |
47708 KB |
Output is correct |
18 |
Correct |
8 ms |
47708 KB |
Output is correct |
19 |
Correct |
8 ms |
47708 KB |
Output is correct |
20 |
Correct |
7 ms |
47708 KB |
Output is correct |
21 |
Correct |
8 ms |
47708 KB |
Output is correct |
22 |
Correct |
8 ms |
47708 KB |
Output is correct |
23 |
Correct |
8 ms |
47780 KB |
Output is correct |
24 |
Correct |
8 ms |
47704 KB |
Output is correct |
25 |
Correct |
8 ms |
47792 KB |
Output is correct |
26 |
Correct |
8 ms |
47708 KB |
Output is correct |
27 |
Correct |
8 ms |
47708 KB |
Output is correct |
28 |
Correct |
9 ms |
47708 KB |
Output is correct |
29 |
Correct |
7 ms |
47708 KB |
Output is correct |
30 |
Correct |
8 ms |
47780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47756 KB |
Output is correct |
2 |
Correct |
7 ms |
47708 KB |
Output is correct |
3 |
Correct |
7 ms |
47708 KB |
Output is correct |
4 |
Correct |
7 ms |
47756 KB |
Output is correct |
5 |
Correct |
8 ms |
47708 KB |
Output is correct |
6 |
Correct |
7 ms |
47704 KB |
Output is correct |
7 |
Correct |
7 ms |
47704 KB |
Output is correct |
8 |
Correct |
8 ms |
47704 KB |
Output is correct |
9 |
Correct |
7 ms |
47708 KB |
Output is correct |
10 |
Correct |
8 ms |
47708 KB |
Output is correct |
11 |
Correct |
7 ms |
47708 KB |
Output is correct |
12 |
Correct |
8 ms |
47704 KB |
Output is correct |
13 |
Correct |
8 ms |
47708 KB |
Output is correct |
14 |
Correct |
7 ms |
47708 KB |
Output is correct |
15 |
Correct |
8 ms |
47784 KB |
Output is correct |
16 |
Correct |
8 ms |
47708 KB |
Output is correct |
17 |
Correct |
8 ms |
47708 KB |
Output is correct |
18 |
Correct |
8 ms |
47708 KB |
Output is correct |
19 |
Correct |
8 ms |
47708 KB |
Output is correct |
20 |
Correct |
7 ms |
47708 KB |
Output is correct |
21 |
Correct |
8 ms |
47708 KB |
Output is correct |
22 |
Correct |
8 ms |
47708 KB |
Output is correct |
23 |
Correct |
8 ms |
47780 KB |
Output is correct |
24 |
Correct |
8 ms |
47704 KB |
Output is correct |
25 |
Correct |
8 ms |
47792 KB |
Output is correct |
26 |
Correct |
8 ms |
47708 KB |
Output is correct |
27 |
Correct |
8 ms |
47708 KB |
Output is correct |
28 |
Correct |
9 ms |
47708 KB |
Output is correct |
29 |
Correct |
7 ms |
47708 KB |
Output is correct |
30 |
Correct |
8 ms |
47780 KB |
Output is correct |
31 |
Correct |
8 ms |
47708 KB |
Output is correct |
32 |
Correct |
10 ms |
47708 KB |
Output is correct |
33 |
Correct |
9 ms |
47712 KB |
Output is correct |
34 |
Correct |
8 ms |
47744 KB |
Output is correct |
35 |
Correct |
11 ms |
47708 KB |
Output is correct |
36 |
Correct |
9 ms |
47708 KB |
Output is correct |
37 |
Correct |
9 ms |
47704 KB |
Output is correct |
38 |
Correct |
10 ms |
47708 KB |
Output is correct |
39 |
Correct |
9 ms |
47708 KB |
Output is correct |
40 |
Correct |
9 ms |
47752 KB |
Output is correct |
41 |
Correct |
9 ms |
47708 KB |
Output is correct |
42 |
Correct |
9 ms |
47708 KB |
Output is correct |
43 |
Correct |
9 ms |
47708 KB |
Output is correct |
44 |
Correct |
11 ms |
47704 KB |
Output is correct |
45 |
Correct |
9 ms |
47708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47756 KB |
Output is correct |
2 |
Correct |
7 ms |
47708 KB |
Output is correct |
3 |
Correct |
7 ms |
47708 KB |
Output is correct |
4 |
Correct |
7 ms |
47756 KB |
Output is correct |
5 |
Correct |
8 ms |
47708 KB |
Output is correct |
6 |
Correct |
7 ms |
47704 KB |
Output is correct |
7 |
Correct |
7 ms |
47704 KB |
Output is correct |
8 |
Correct |
8 ms |
47704 KB |
Output is correct |
9 |
Correct |
7 ms |
47708 KB |
Output is correct |
10 |
Correct |
8 ms |
47708 KB |
Output is correct |
11 |
Correct |
7 ms |
47708 KB |
Output is correct |
12 |
Correct |
8 ms |
47704 KB |
Output is correct |
13 |
Correct |
8 ms |
47708 KB |
Output is correct |
14 |
Correct |
7 ms |
47708 KB |
Output is correct |
15 |
Correct |
8 ms |
47784 KB |
Output is correct |
16 |
Correct |
8 ms |
47708 KB |
Output is correct |
17 |
Correct |
8 ms |
47708 KB |
Output is correct |
18 |
Correct |
8 ms |
47708 KB |
Output is correct |
19 |
Correct |
8 ms |
47708 KB |
Output is correct |
20 |
Correct |
7 ms |
47708 KB |
Output is correct |
21 |
Correct |
8 ms |
47708 KB |
Output is correct |
22 |
Correct |
8 ms |
47708 KB |
Output is correct |
23 |
Correct |
8 ms |
47780 KB |
Output is correct |
24 |
Correct |
8 ms |
47704 KB |
Output is correct |
25 |
Correct |
8 ms |
47792 KB |
Output is correct |
26 |
Correct |
8 ms |
47708 KB |
Output is correct |
27 |
Correct |
8 ms |
47708 KB |
Output is correct |
28 |
Correct |
9 ms |
47708 KB |
Output is correct |
29 |
Correct |
7 ms |
47708 KB |
Output is correct |
30 |
Correct |
8 ms |
47780 KB |
Output is correct |
31 |
Correct |
8 ms |
47708 KB |
Output is correct |
32 |
Correct |
10 ms |
47708 KB |
Output is correct |
33 |
Correct |
9 ms |
47712 KB |
Output is correct |
34 |
Correct |
8 ms |
47744 KB |
Output is correct |
35 |
Correct |
11 ms |
47708 KB |
Output is correct |
36 |
Correct |
9 ms |
47708 KB |
Output is correct |
37 |
Correct |
9 ms |
47704 KB |
Output is correct |
38 |
Correct |
10 ms |
47708 KB |
Output is correct |
39 |
Correct |
9 ms |
47708 KB |
Output is correct |
40 |
Correct |
9 ms |
47752 KB |
Output is correct |
41 |
Correct |
9 ms |
47708 KB |
Output is correct |
42 |
Correct |
9 ms |
47708 KB |
Output is correct |
43 |
Correct |
9 ms |
47708 KB |
Output is correct |
44 |
Correct |
11 ms |
47704 KB |
Output is correct |
45 |
Correct |
9 ms |
47708 KB |
Output is correct |
46 |
Correct |
25 ms |
50124 KB |
Output is correct |
47 |
Correct |
79 ms |
50268 KB |
Output is correct |
48 |
Correct |
84 ms |
50120 KB |
Output is correct |
49 |
Correct |
74 ms |
50264 KB |
Output is correct |
50 |
Correct |
71 ms |
50352 KB |
Output is correct |
51 |
Correct |
90 ms |
50516 KB |
Output is correct |
52 |
Correct |
47 ms |
47964 KB |
Output is correct |
53 |
Correct |
29 ms |
47964 KB |
Output is correct |
54 |
Correct |
83 ms |
50268 KB |
Output is correct |
55 |
Correct |
88 ms |
50348 KB |
Output is correct |
56 |
Correct |
63 ms |
50264 KB |
Output is correct |
57 |
Correct |
67 ms |
50264 KB |
Output is correct |
58 |
Correct |
69 ms |
50268 KB |
Output is correct |
59 |
Correct |
63 ms |
50264 KB |
Output is correct |
60 |
Correct |
87 ms |
50348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
47756 KB |
Output is correct |
2 |
Correct |
7 ms |
47708 KB |
Output is correct |
3 |
Correct |
7 ms |
47708 KB |
Output is correct |
4 |
Correct |
7 ms |
47756 KB |
Output is correct |
5 |
Correct |
8 ms |
47708 KB |
Output is correct |
6 |
Correct |
7 ms |
47704 KB |
Output is correct |
7 |
Correct |
7 ms |
47704 KB |
Output is correct |
8 |
Correct |
8 ms |
47704 KB |
Output is correct |
9 |
Correct |
7 ms |
47708 KB |
Output is correct |
10 |
Correct |
8 ms |
47708 KB |
Output is correct |
11 |
Correct |
7 ms |
47708 KB |
Output is correct |
12 |
Correct |
8 ms |
47704 KB |
Output is correct |
13 |
Correct |
8 ms |
47708 KB |
Output is correct |
14 |
Correct |
7 ms |
47708 KB |
Output is correct |
15 |
Correct |
8 ms |
47784 KB |
Output is correct |
16 |
Correct |
8 ms |
47708 KB |
Output is correct |
17 |
Correct |
8 ms |
47708 KB |
Output is correct |
18 |
Correct |
8 ms |
47708 KB |
Output is correct |
19 |
Correct |
8 ms |
47708 KB |
Output is correct |
20 |
Correct |
7 ms |
47708 KB |
Output is correct |
21 |
Correct |
8 ms |
47708 KB |
Output is correct |
22 |
Correct |
8 ms |
47708 KB |
Output is correct |
23 |
Correct |
8 ms |
47780 KB |
Output is correct |
24 |
Correct |
8 ms |
47704 KB |
Output is correct |
25 |
Correct |
8 ms |
47792 KB |
Output is correct |
26 |
Correct |
8 ms |
47708 KB |
Output is correct |
27 |
Correct |
8 ms |
47708 KB |
Output is correct |
28 |
Correct |
9 ms |
47708 KB |
Output is correct |
29 |
Correct |
7 ms |
47708 KB |
Output is correct |
30 |
Correct |
8 ms |
47780 KB |
Output is correct |
31 |
Correct |
8 ms |
47708 KB |
Output is correct |
32 |
Correct |
10 ms |
47708 KB |
Output is correct |
33 |
Correct |
9 ms |
47712 KB |
Output is correct |
34 |
Correct |
8 ms |
47744 KB |
Output is correct |
35 |
Correct |
11 ms |
47708 KB |
Output is correct |
36 |
Correct |
9 ms |
47708 KB |
Output is correct |
37 |
Correct |
9 ms |
47704 KB |
Output is correct |
38 |
Correct |
10 ms |
47708 KB |
Output is correct |
39 |
Correct |
9 ms |
47708 KB |
Output is correct |
40 |
Correct |
9 ms |
47752 KB |
Output is correct |
41 |
Correct |
9 ms |
47708 KB |
Output is correct |
42 |
Correct |
9 ms |
47708 KB |
Output is correct |
43 |
Correct |
9 ms |
47708 KB |
Output is correct |
44 |
Correct |
11 ms |
47704 KB |
Output is correct |
45 |
Correct |
9 ms |
47708 KB |
Output is correct |
46 |
Correct |
25 ms |
50124 KB |
Output is correct |
47 |
Correct |
79 ms |
50268 KB |
Output is correct |
48 |
Correct |
84 ms |
50120 KB |
Output is correct |
49 |
Correct |
74 ms |
50264 KB |
Output is correct |
50 |
Correct |
71 ms |
50352 KB |
Output is correct |
51 |
Correct |
90 ms |
50516 KB |
Output is correct |
52 |
Correct |
47 ms |
47964 KB |
Output is correct |
53 |
Correct |
29 ms |
47964 KB |
Output is correct |
54 |
Correct |
83 ms |
50268 KB |
Output is correct |
55 |
Correct |
88 ms |
50348 KB |
Output is correct |
56 |
Correct |
63 ms |
50264 KB |
Output is correct |
57 |
Correct |
67 ms |
50264 KB |
Output is correct |
58 |
Correct |
69 ms |
50268 KB |
Output is correct |
59 |
Correct |
63 ms |
50264 KB |
Output is correct |
60 |
Correct |
87 ms |
50348 KB |
Output is correct |
61 |
Correct |
80 ms |
53268 KB |
Output is correct |
62 |
Correct |
211 ms |
55120 KB |
Output is correct |
63 |
Correct |
405 ms |
55632 KB |
Output is correct |
64 |
Correct |
439 ms |
57680 KB |
Output is correct |
65 |
Correct |
344 ms |
58172 KB |
Output is correct |
66 |
Correct |
490 ms |
58032 KB |
Output is correct |
67 |
Correct |
360 ms |
56404 KB |
Output is correct |
68 |
Correct |
175 ms |
53840 KB |
Output is correct |
69 |
Correct |
491 ms |
54324 KB |
Output is correct |
70 |
Correct |
449 ms |
57940 KB |
Output is correct |
71 |
Correct |
328 ms |
55232 KB |
Output is correct |
72 |
Correct |
350 ms |
55616 KB |
Output is correct |
73 |
Correct |
348 ms |
56224 KB |
Output is correct |
74 |
Correct |
323 ms |
56596 KB |
Output is correct |
75 |
Correct |
377 ms |
56592 KB |
Output is correct |