#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 5e5 + 10;
ll pref[MAXN];
ll a[MAXN];
pll dp[MAXN];
int n;
pii t[MAXN * 40];
int ls[MAXN * 40];
int rs[MAXN * 40];
int sz = 1;
void upd(ll pos, pll val, int v = 1, ll tl = 1, ll tr = 1e15) {
if(tl == tr) {
t[v] = val;
return;
}
ll tm = tl + tr >> 1;
if(pos <= tm) {
if(!ls[v]) ls[v] = ++sz;
upd(pos, val, ls[v], tl, tm);
} else {
if(!rs[v]) rs[v] = ++sz;
upd(pos, val, rs[v], tm + 1, tr);
}
t[v] = max(t[ls[v]], t[rs[v]]);
}
pll get(ll l, ll r, int v = 1, ll tl = 1, ll tr = 1e15) {
if(l <= tl && tr <= r) return t[v];
if(l > tr || r < tl) return mp(0, 0);
ll tm = tl + tr >> 1;
return max(get(l, r, ls[v], tl, tm), get(l, r, rs[v], tm + 1, tr));
}
void solve() {
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i], pref[i] = pref[i - 1] + a[i];
for(int i = 1; i <= n; ++i) {
upd(pref[i - 1] * 2 - pref[dp[i - 1].s], mp(dp[i - 1].f, i - 1));
dp[i] = get(0, pref[i]);
dp[i].f++;
}
cout << dp[n].f;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}
Compilation message
segments.cpp: In function 'void upd(long long int, std::pair<long long int, long long int>, int, long long int, long long int)':
segments.cpp:40:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | ll tm = tl + tr >> 1;
| ~~~^~~~
segments.cpp: In function 'std::pair<long long int, long long int> get(long long int, long long int, int, long long int, long long int)':
segments.cpp:54:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | ll tm = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
620 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 |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
620 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 |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
6 ms |
1260 KB |
Output is correct |
32 |
Correct |
6 ms |
1516 KB |
Output is correct |
33 |
Correct |
6 ms |
1644 KB |
Output is correct |
34 |
Correct |
6 ms |
1644 KB |
Output is correct |
35 |
Correct |
6 ms |
1900 KB |
Output is correct |
36 |
Correct |
6 ms |
1900 KB |
Output is correct |
37 |
Correct |
5 ms |
1644 KB |
Output is correct |
38 |
Correct |
4 ms |
1388 KB |
Output is correct |
39 |
Correct |
5 ms |
620 KB |
Output is correct |
40 |
Correct |
6 ms |
1772 KB |
Output is correct |
41 |
Correct |
4 ms |
620 KB |
Output is correct |
42 |
Correct |
5 ms |
876 KB |
Output is correct |
43 |
Correct |
5 ms |
620 KB |
Output is correct |
44 |
Correct |
5 ms |
1004 KB |
Output is correct |
45 |
Correct |
6 ms |
1004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
620 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 |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
6 ms |
1260 KB |
Output is correct |
32 |
Correct |
6 ms |
1516 KB |
Output is correct |
33 |
Correct |
6 ms |
1644 KB |
Output is correct |
34 |
Correct |
6 ms |
1644 KB |
Output is correct |
35 |
Correct |
6 ms |
1900 KB |
Output is correct |
36 |
Correct |
6 ms |
1900 KB |
Output is correct |
37 |
Correct |
5 ms |
1644 KB |
Output is correct |
38 |
Correct |
4 ms |
1388 KB |
Output is correct |
39 |
Correct |
5 ms |
620 KB |
Output is correct |
40 |
Correct |
6 ms |
1772 KB |
Output is correct |
41 |
Correct |
4 ms |
620 KB |
Output is correct |
42 |
Correct |
5 ms |
876 KB |
Output is correct |
43 |
Correct |
5 ms |
620 KB |
Output is correct |
44 |
Correct |
5 ms |
1004 KB |
Output is correct |
45 |
Correct |
6 ms |
1004 KB |
Output is correct |
46 |
Correct |
156 ms |
20716 KB |
Output is correct |
47 |
Correct |
158 ms |
23148 KB |
Output is correct |
48 |
Correct |
181 ms |
43756 KB |
Output is correct |
49 |
Correct |
180 ms |
46956 KB |
Output is correct |
50 |
Correct |
186 ms |
51564 KB |
Output is correct |
51 |
Correct |
190 ms |
50796 KB |
Output is correct |
52 |
Correct |
96 ms |
25580 KB |
Output is correct |
53 |
Correct |
59 ms |
15468 KB |
Output is correct |
54 |
Correct |
139 ms |
9324 KB |
Output is correct |
55 |
Correct |
195 ms |
49260 KB |
Output is correct |
56 |
Correct |
150 ms |
24044 KB |
Output is correct |
57 |
Correct |
148 ms |
20460 KB |
Output is correct |
58 |
Correct |
155 ms |
22124 KB |
Output is correct |
59 |
Correct |
186 ms |
30316 KB |
Output is correct |
60 |
Correct |
169 ms |
30316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
768 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
1 ms |
620 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
620 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 |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
6 ms |
1260 KB |
Output is correct |
32 |
Correct |
6 ms |
1516 KB |
Output is correct |
33 |
Correct |
6 ms |
1644 KB |
Output is correct |
34 |
Correct |
6 ms |
1644 KB |
Output is correct |
35 |
Correct |
6 ms |
1900 KB |
Output is correct |
36 |
Correct |
6 ms |
1900 KB |
Output is correct |
37 |
Correct |
5 ms |
1644 KB |
Output is correct |
38 |
Correct |
4 ms |
1388 KB |
Output is correct |
39 |
Correct |
5 ms |
620 KB |
Output is correct |
40 |
Correct |
6 ms |
1772 KB |
Output is correct |
41 |
Correct |
4 ms |
620 KB |
Output is correct |
42 |
Correct |
5 ms |
876 KB |
Output is correct |
43 |
Correct |
5 ms |
620 KB |
Output is correct |
44 |
Correct |
5 ms |
1004 KB |
Output is correct |
45 |
Correct |
6 ms |
1004 KB |
Output is correct |
46 |
Correct |
156 ms |
20716 KB |
Output is correct |
47 |
Correct |
158 ms |
23148 KB |
Output is correct |
48 |
Correct |
181 ms |
43756 KB |
Output is correct |
49 |
Correct |
180 ms |
46956 KB |
Output is correct |
50 |
Correct |
186 ms |
51564 KB |
Output is correct |
51 |
Correct |
190 ms |
50796 KB |
Output is correct |
52 |
Correct |
96 ms |
25580 KB |
Output is correct |
53 |
Correct |
59 ms |
15468 KB |
Output is correct |
54 |
Correct |
139 ms |
9324 KB |
Output is correct |
55 |
Correct |
195 ms |
49260 KB |
Output is correct |
56 |
Correct |
150 ms |
24044 KB |
Output is correct |
57 |
Correct |
148 ms |
20460 KB |
Output is correct |
58 |
Correct |
155 ms |
22124 KB |
Output is correct |
59 |
Correct |
186 ms |
30316 KB |
Output is correct |
60 |
Correct |
169 ms |
30316 KB |
Output is correct |
61 |
Correct |
728 ms |
95468 KB |
Output is correct |
62 |
Correct |
744 ms |
92268 KB |
Output is correct |
63 |
Correct |
803 ms |
114412 KB |
Output is correct |
64 |
Correct |
914 ms |
240500 KB |
Output is correct |
65 |
Correct |
907 ms |
259564 KB |
Output is correct |
66 |
Correct |
947 ms |
257388 KB |
Output is correct |
67 |
Correct |
756 ms |
205932 KB |
Output is correct |
68 |
Correct |
377 ms |
103148 KB |
Output is correct |
69 |
Correct |
688 ms |
45804 KB |
Output is correct |
70 |
Correct |
950 ms |
252012 KB |
Output is correct |
71 |
Correct |
731 ms |
91884 KB |
Output is correct |
72 |
Correct |
755 ms |
115820 KB |
Output is correct |
73 |
Correct |
802 ms |
150508 KB |
Output is correct |
74 |
Correct |
816 ms |
171756 KB |
Output is correct |
75 |
Correct |
826 ms |
171756 KB |
Output is correct |