#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define getbit(x, i) (x >> i) & 1
#define TASK ""
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int N = 5e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
pair <int, int> dp[N];
pair <int, int> val[60 * N];
long long s[N];
int ptr[60 * N][2];
int a[N];
int n,cnt;
void add(long long x, pair <int, int> v) {
int cur = 0;
for (int i = 59; i >= 0; i--) {
int &p = ptr[cur][getbit(x, i)];
if (!p)
p = ++cnt;
cur = p;
maxi(val[cur], v);
}
}
pair <int, int> getmax(long long x) {
int cur = 0;
pair <int, int> res = mp(0, 0);
for (int i = 59; i >= 0; i--) {
int d = getbit(x, i);
if (d == 1)
maxi(res, val[ptr[cur][d ^ 1]]);
cur = ptr[cur][d];
if (cur == 0)
break;
}
maxi(res, val[cur]);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef ONLINE_JUDGE
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
add(0, mp(0, 0));
for (int i = 1; i <= n; i++) {
long long tot = 0;
pair <int, int> pir = getmax(s[i]);
pir.fi++;
dp[i] = pir;
int j = pir.se;
// cerr << i << " " << j << "\n";
add(s[i] - s[j] + s[i], mp(dp[i].fi, i));
}
cout << dp[n].fi;
return 0;
}
Compilation message
segments.cpp: In function 'int main()':
segments.cpp:71:13: warning: unused variable 'tot' [-Wunused-variable]
71 | long long tot = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1620 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
35 |
Correct |
3 ms |
1748 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
3 ms |
1624 KB |
Output is correct |
38 |
Correct |
2 ms |
1364 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1784 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
984 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1620 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
35 |
Correct |
3 ms |
1748 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
3 ms |
1624 KB |
Output is correct |
38 |
Correct |
2 ms |
1364 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1784 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
984 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
42 ms |
19696 KB |
Output is correct |
47 |
Correct |
52 ms |
22248 KB |
Output is correct |
48 |
Correct |
72 ms |
43224 KB |
Output is correct |
49 |
Correct |
71 ms |
46504 KB |
Output is correct |
50 |
Correct |
77 ms |
51196 KB |
Output is correct |
51 |
Correct |
65 ms |
50480 KB |
Output is correct |
52 |
Correct |
33 ms |
25292 KB |
Output is correct |
53 |
Correct |
20 ms |
15336 KB |
Output is correct |
54 |
Correct |
50 ms |
8172 KB |
Output is correct |
55 |
Correct |
70 ms |
48832 KB |
Output is correct |
56 |
Correct |
62 ms |
23240 KB |
Output is correct |
57 |
Correct |
55 ms |
19404 KB |
Output is correct |
58 |
Correct |
58 ms |
21320 KB |
Output is correct |
59 |
Correct |
63 ms |
29608 KB |
Output is correct |
60 |
Correct |
60 ms |
29524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1620 KB |
Output is correct |
34 |
Correct |
2 ms |
1492 KB |
Output is correct |
35 |
Correct |
3 ms |
1748 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
3 ms |
1624 KB |
Output is correct |
38 |
Correct |
2 ms |
1364 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
1784 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
984 KB |
Output is correct |
45 |
Correct |
2 ms |
980 KB |
Output is correct |
46 |
Correct |
42 ms |
19696 KB |
Output is correct |
47 |
Correct |
52 ms |
22248 KB |
Output is correct |
48 |
Correct |
72 ms |
43224 KB |
Output is correct |
49 |
Correct |
71 ms |
46504 KB |
Output is correct |
50 |
Correct |
77 ms |
51196 KB |
Output is correct |
51 |
Correct |
65 ms |
50480 KB |
Output is correct |
52 |
Correct |
33 ms |
25292 KB |
Output is correct |
53 |
Correct |
20 ms |
15336 KB |
Output is correct |
54 |
Correct |
50 ms |
8172 KB |
Output is correct |
55 |
Correct |
70 ms |
48832 KB |
Output is correct |
56 |
Correct |
62 ms |
23240 KB |
Output is correct |
57 |
Correct |
55 ms |
19404 KB |
Output is correct |
58 |
Correct |
58 ms |
21320 KB |
Output is correct |
59 |
Correct |
63 ms |
29608 KB |
Output is correct |
60 |
Correct |
60 ms |
29524 KB |
Output is correct |
61 |
Correct |
217 ms |
91364 KB |
Output is correct |
62 |
Correct |
233 ms |
88256 KB |
Output is correct |
63 |
Correct |
253 ms |
110348 KB |
Output is correct |
64 |
Correct |
346 ms |
238916 KB |
Output is correct |
65 |
Correct |
384 ms |
253044 KB |
Output is correct |
66 |
Correct |
319 ms |
250796 KB |
Output is correct |
67 |
Correct |
249 ms |
200720 KB |
Output is correct |
68 |
Correct |
136 ms |
100516 KB |
Output is correct |
69 |
Correct |
247 ms |
39624 KB |
Output is correct |
70 |
Correct |
357 ms |
245464 KB |
Output is correct |
71 |
Correct |
268 ms |
85624 KB |
Output is correct |
72 |
Correct |
272 ms |
109652 KB |
Output is correct |
73 |
Correct |
314 ms |
144292 KB |
Output is correct |
74 |
Correct |
316 ms |
165464 KB |
Output is correct |
75 |
Correct |
323 ms |
165660 KB |
Output is correct |