#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = 5e5+7;
ll n, dp[mxn];
long long opt[mxn], a[mxn];
struct dynamic_segment_tree
{
struct node {ll v, l, r; node() {v = 0; l = -1, r = -1;}};
vector<node> st;
dynamic_segment_tree() {st.clear(); st.pb(node());};
void update(long long id, long long l, long long r, long long i, ll x)
{
if (l == r) {st[id].v = max(st[id].v,x); return;}
long long m = (r+l)>>1; st[id].v = max(st[id].v,x);
if (l <= i && i <= m)
{
if (st[id].l == -1) {st[id].l = st.size(); st.pb(node());}
update(st[id].l,l,m,i,x);
}
if (m < i && i <= r)
{
if (st[id].r == -1) {st[id].r = st.size(); st.pb(node());}
update(st[id].r,m+1,r,i,x);
}
}
ll get(long long id, long long l, long long r, long long u, long long v)
{
if (r < u || v < l) return 0;
if (u <= l && r <= v) return st[id].v;
long long m = (r+l)>>1; ll val = 0;
if (st[id].l != -1) val = max(val,get(st[id].l,l,m,u,v));
if (st[id].r != -1) val = max(val,get(st[id].r,m+1,r,u,v));
return val;
}
void update(long long i, ll x) {update(0,0,1e17,i,x);}
ll get(long long u, long long v) {return get(0,0,1e17,u,v);}
} st;
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
cin >> n;
for (ll i = 1; i <= n; i++)
{
cin >> a[i];
a[i] += a[i-1];
}
for (ll i = 1; i <= n; i++)
{
ll opt_pos = st.get(0,a[i]);
dp[i] = dp[opt_pos]+1;
opt[i] = a[i]-a[opt_pos];
st.update((long long)opt[i]+a[i],i);
}
cout << dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2844 KB |
Output is correct |
19 |
Correct |
1 ms |
2684 KB |
Output is correct |
20 |
Correct |
1 ms |
2844 KB |
Output is correct |
21 |
Correct |
1 ms |
2844 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2844 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2844 KB |
Output is correct |
19 |
Correct |
1 ms |
2684 KB |
Output is correct |
20 |
Correct |
1 ms |
2844 KB |
Output is correct |
21 |
Correct |
1 ms |
2844 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2844 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
3416 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
2 ms |
4048 KB |
Output is correct |
34 |
Correct |
3 ms |
4052 KB |
Output is correct |
35 |
Correct |
3 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4048 KB |
Output is correct |
37 |
Correct |
2 ms |
4052 KB |
Output is correct |
38 |
Correct |
2 ms |
3416 KB |
Output is correct |
39 |
Correct |
1 ms |
2712 KB |
Output is correct |
40 |
Correct |
3 ms |
4052 KB |
Output is correct |
41 |
Correct |
1 ms |
2844 KB |
Output is correct |
42 |
Correct |
2 ms |
3100 KB |
Output is correct |
43 |
Correct |
1 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
3416 KB |
Output is correct |
45 |
Correct |
2 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2844 KB |
Output is correct |
19 |
Correct |
1 ms |
2684 KB |
Output is correct |
20 |
Correct |
1 ms |
2844 KB |
Output is correct |
21 |
Correct |
1 ms |
2844 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2844 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
3416 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
2 ms |
4048 KB |
Output is correct |
34 |
Correct |
3 ms |
4052 KB |
Output is correct |
35 |
Correct |
3 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4048 KB |
Output is correct |
37 |
Correct |
2 ms |
4052 KB |
Output is correct |
38 |
Correct |
2 ms |
3416 KB |
Output is correct |
39 |
Correct |
1 ms |
2712 KB |
Output is correct |
40 |
Correct |
3 ms |
4052 KB |
Output is correct |
41 |
Correct |
1 ms |
2844 KB |
Output is correct |
42 |
Correct |
2 ms |
3100 KB |
Output is correct |
43 |
Correct |
1 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
3416 KB |
Output is correct |
45 |
Correct |
2 ms |
3416 KB |
Output is correct |
46 |
Correct |
36 ms |
32708 KB |
Output is correct |
47 |
Correct |
44 ms |
32692 KB |
Output is correct |
48 |
Correct |
57 ms |
57264 KB |
Output is correct |
49 |
Correct |
61 ms |
57272 KB |
Output is correct |
50 |
Correct |
70 ms |
57252 KB |
Output is correct |
51 |
Correct |
59 ms |
57416 KB |
Output is correct |
52 |
Correct |
28 ms |
30908 KB |
Output is correct |
53 |
Correct |
18 ms |
19532 KB |
Output is correct |
54 |
Correct |
40 ms |
13400 KB |
Output is correct |
55 |
Correct |
63 ms |
58040 KB |
Output is correct |
56 |
Correct |
48 ms |
34224 KB |
Output is correct |
57 |
Correct |
44 ms |
32944 KB |
Output is correct |
58 |
Correct |
47 ms |
33476 KB |
Output is correct |
59 |
Correct |
53 ms |
33724 KB |
Output is correct |
60 |
Correct |
47 ms |
32592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2844 KB |
Output is correct |
19 |
Correct |
1 ms |
2684 KB |
Output is correct |
20 |
Correct |
1 ms |
2844 KB |
Output is correct |
21 |
Correct |
1 ms |
2844 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2844 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
0 ms |
2396 KB |
Output is correct |
29 |
Correct |
0 ms |
2652 KB |
Output is correct |
30 |
Correct |
0 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
3416 KB |
Output is correct |
32 |
Correct |
2 ms |
3412 KB |
Output is correct |
33 |
Correct |
2 ms |
4048 KB |
Output is correct |
34 |
Correct |
3 ms |
4052 KB |
Output is correct |
35 |
Correct |
3 ms |
4188 KB |
Output is correct |
36 |
Correct |
2 ms |
4048 KB |
Output is correct |
37 |
Correct |
2 ms |
4052 KB |
Output is correct |
38 |
Correct |
2 ms |
3416 KB |
Output is correct |
39 |
Correct |
1 ms |
2712 KB |
Output is correct |
40 |
Correct |
3 ms |
4052 KB |
Output is correct |
41 |
Correct |
1 ms |
2844 KB |
Output is correct |
42 |
Correct |
2 ms |
3100 KB |
Output is correct |
43 |
Correct |
1 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
3416 KB |
Output is correct |
45 |
Correct |
2 ms |
3416 KB |
Output is correct |
46 |
Correct |
36 ms |
32708 KB |
Output is correct |
47 |
Correct |
44 ms |
32692 KB |
Output is correct |
48 |
Correct |
57 ms |
57264 KB |
Output is correct |
49 |
Correct |
61 ms |
57272 KB |
Output is correct |
50 |
Correct |
70 ms |
57252 KB |
Output is correct |
51 |
Correct |
59 ms |
57416 KB |
Output is correct |
52 |
Correct |
28 ms |
30908 KB |
Output is correct |
53 |
Correct |
18 ms |
19532 KB |
Output is correct |
54 |
Correct |
40 ms |
13400 KB |
Output is correct |
55 |
Correct |
63 ms |
58040 KB |
Output is correct |
56 |
Correct |
48 ms |
34224 KB |
Output is correct |
57 |
Correct |
44 ms |
32944 KB |
Output is correct |
58 |
Correct |
47 ms |
33476 KB |
Output is correct |
59 |
Correct |
53 ms |
33724 KB |
Output is correct |
60 |
Correct |
47 ms |
32592 KB |
Output is correct |
61 |
Correct |
182 ms |
111004 KB |
Output is correct |
62 |
Correct |
205 ms |
109720 KB |
Output is correct |
63 |
Correct |
222 ms |
109208 KB |
Output is correct |
64 |
Correct |
318 ms |
209596 KB |
Output is correct |
65 |
Correct |
356 ms |
209768 KB |
Output is correct |
66 |
Correct |
300 ms |
209764 KB |
Output is correct |
67 |
Correct |
241 ms |
208724 KB |
Output is correct |
68 |
Correct |
117 ms |
107820 KB |
Output is correct |
69 |
Correct |
202 ms |
34724 KB |
Output is correct |
70 |
Correct |
328 ms |
209792 KB |
Output is correct |
71 |
Correct |
228 ms |
111316 KB |
Output is correct |
72 |
Correct |
229 ms |
111256 KB |
Output is correct |
73 |
Correct |
290 ms |
210136 KB |
Output is correct |
74 |
Correct |
304 ms |
209768 KB |
Output is correct |
75 |
Correct |
308 ms |
209768 KB |
Output is correct |