#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = (int)5e5 + 10;
const int mod = (int)1e9 + 7;
const ll inf = (ll) 1e16 + 10;
int n;
int a[N];
ll pref[N];
ll get_sum(int l, int r) {
return pref[r] - pref[l - 1];
}
pair<int, int> dp[N];
pair<int, int> t[N];
int sz = 1;
vector<ll> ids;
int get_id(ll x) {
//cout << x << " -> id=" << lower_bound(ids.begin(), ids.end(), x) - ids.begin() - 1 << endl;;
return lower_bound(ids.begin(), ids.end(), x) - ids.begin() - 1;
}
pair<int, int> get(ll x) {
assert(x >= 0);
pair<int, int> ans = make_pair(0, 0);
while (x >= 0) {
ans = max(ans, t[x]);
x &= x + 1;
x--;
}
return ans;
}
void upd(ll x, pair<int, int> y) {
assert(x >= 0);
while (x <= ids.size()) {
t[x] = max(t[x], y);
x |= x + 1;
}
}
int main() {
// freopen("in.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pref[i] = pref[i - 1] + a[i];
ids.push_back(pref[i]);
}
ids.push_back(-inf);
ids.push_back(inf);
sort(ids.begin(), ids.end());
//for (auto it : ids) cout << it << ' '; cout << endl;
for (int i = 1; i <= n; i++) {
dp[i] = make_pair(1, 1);
dp[i] = max(dp[i], get(get_id(pref[i])));
upd(get_id(2 * pref[i] - pref[dp[i].second - 1]), make_pair(dp[i].first + 1, i + 1));
}
//cout << "\n";
cout << dp[n].first;
return 0;
}
Compilation message
segments.cpp: In function 'void upd(long long int, std::pair<int, int>)':
segments.cpp:43:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | while (x <= ids.size()) {
| ~~^~~~~~~~~~~~~
segments.cpp: In function 'int main()':
segments.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
52 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
segments.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
54 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
2 ms |
492 KB |
Output is correct |
44 |
Correct |
2 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
2 ms |
492 KB |
Output is correct |
44 |
Correct |
2 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
25 ms |
3296 KB |
Output is correct |
47 |
Correct |
32 ms |
4192 KB |
Output is correct |
48 |
Correct |
36 ms |
4192 KB |
Output is correct |
49 |
Correct |
37 ms |
4324 KB |
Output is correct |
50 |
Correct |
34 ms |
4192 KB |
Output is correct |
51 |
Correct |
37 ms |
4192 KB |
Output is correct |
52 |
Correct |
19 ms |
2532 KB |
Output is correct |
53 |
Correct |
12 ms |
1660 KB |
Output is correct |
54 |
Correct |
36 ms |
4064 KB |
Output is correct |
55 |
Correct |
37 ms |
4192 KB |
Output is correct |
56 |
Correct |
30 ms |
4192 KB |
Output is correct |
57 |
Correct |
29 ms |
3808 KB |
Output is correct |
58 |
Correct |
32 ms |
4064 KB |
Output is correct |
59 |
Correct |
31 ms |
4320 KB |
Output is correct |
60 |
Correct |
33 ms |
4192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
508 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
2 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
512 KB |
Output is correct |
43 |
Correct |
2 ms |
492 KB |
Output is correct |
44 |
Correct |
2 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
512 KB |
Output is correct |
46 |
Correct |
25 ms |
3296 KB |
Output is correct |
47 |
Correct |
32 ms |
4192 KB |
Output is correct |
48 |
Correct |
36 ms |
4192 KB |
Output is correct |
49 |
Correct |
37 ms |
4324 KB |
Output is correct |
50 |
Correct |
34 ms |
4192 KB |
Output is correct |
51 |
Correct |
37 ms |
4192 KB |
Output is correct |
52 |
Correct |
19 ms |
2532 KB |
Output is correct |
53 |
Correct |
12 ms |
1660 KB |
Output is correct |
54 |
Correct |
36 ms |
4064 KB |
Output is correct |
55 |
Correct |
37 ms |
4192 KB |
Output is correct |
56 |
Correct |
30 ms |
4192 KB |
Output is correct |
57 |
Correct |
29 ms |
3808 KB |
Output is correct |
58 |
Correct |
32 ms |
4064 KB |
Output is correct |
59 |
Correct |
31 ms |
4320 KB |
Output is correct |
60 |
Correct |
33 ms |
4192 KB |
Output is correct |
61 |
Correct |
129 ms |
14548 KB |
Output is correct |
62 |
Correct |
143 ms |
16340 KB |
Output is correct |
63 |
Correct |
163 ms |
17492 KB |
Output is correct |
64 |
Correct |
192 ms |
18644 KB |
Output is correct |
65 |
Correct |
173 ms |
19284 KB |
Output is correct |
66 |
Correct |
192 ms |
19156 KB |
Output is correct |
67 |
Correct |
160 ms |
16084 KB |
Output is correct |
68 |
Correct |
75 ms |
8156 KB |
Output is correct |
69 |
Correct |
169 ms |
18516 KB |
Output is correct |
70 |
Correct |
195 ms |
19540 KB |
Output is correct |
71 |
Correct |
154 ms |
19028 KB |
Output is correct |
72 |
Correct |
152 ms |
16980 KB |
Output is correct |
73 |
Correct |
157 ms |
18008 KB |
Output is correct |
74 |
Correct |
162 ms |
18900 KB |
Output is correct |
75 |
Correct |
167 ms |
19132 KB |
Output is correct |