#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
void chmax(pair<ll, ll> &a, pair<ll, ll> b){
if(a.first < b.first){
a = b;
}else if(a.first == b.first){
if(a.second > b.second){
a = b;
}
}
}
struct normalize{
vector<ll> poi, pot;
void add(ll x){
poi.push_back(x);
}
void start(){
sort(poi.begin(), poi.end());
if(poi.size() > 0) pot.push_back(poi[0]);
for(int i = 1; i < (int)poi.size(); i++){
if(poi[i] != poi[i-1]){
pot.push_back(poi[i]);
}
}
}
int encode(ll x){
return lower_bound(pot.begin(), pot.end(), x) - pot.begin()+1;
}
};
struct ImplicitSegmentTree{
struct Node{
pair<ll, ll> v = {-INF, 0};
int ln = 0, rn = 0;
};
ll n, N;
vector<Node> T;
int cnt;
ImplicitSegmentTree(ll n): n(n){
N = n;
T.push_back(Node());
T.push_back(Node());
cnt = 1;
};
int new_node(){
T.push_back(Node());
cnt++;
return cnt;
}
void down(int node, ll low, ll high){
}
void apply(ll i, pair<ll, ll> x){
auto apply = [&](auto apply, int node, ll low, ll high, ll i, pair<ll, ll> x) -> void{
down(node, low, high);
if(low == high){
chmax(T[node].v, x);
down(node, low, high);
return;
}
ll mid = (low+high)/2;
if(i <= mid){
if(T[node].ln == 0){
int x = new_node();
T[node].ln = x;
}
apply(apply, T[node].ln, low, mid, i, x);
}else{
if(T[node].rn == 0){
int x = new_node();
T[node].rn = x;
}
apply(apply, T[node].rn, mid+1, high, i, x);
}
chmax(T[node].v, T[T[node].ln].v);
chmax(T[node].v, T[T[node].rn].v);
};
apply(apply, 1, 0, N, i, x);
}
pair<ll, ll> get(ll l, ll r){
auto get = [&](auto self, int node, ll low, ll high, ll l, ll r) -> pair<ll, ll>{
down(node, low, high);
if(low > r || l > high) return {-INF, 0};
if(low >= l && r >= high){
return T[node].v;
}
ll mid = (low+high)/2;
if(T[node].ln == 0){
int x = new_node();
T[node].ln = x;
}
if(T[node].rn == 0){
int x = new_node();
T[node].rn = x;
}
pair<ll, ll> res = {-INF, 0};
chmax(res, self(self, T[node].ln, low, mid, l, r));
chmax(res, self(self, T[node].rn, mid+1, high, l, r));
chmax(T[node].v, T[T[node].ln].v);
chmax(T[node].v, T[T[node].rn].v);
return res;
};
return get(get, 1, 0, N, l, r);
}
};
void solve(){
int n;
cin >> n;
vector<ll> a(n+1);
for(int i = 1; i <= n; i++){
cin >> a[i];
}
vector<ll> pf(n+1, 0);
for(int i = 1; i <= n; i++){
pf[i] = pf[i-1]+a[i];
}
auto sum = [&](int l, int r) -> ll{
return pf[r]-pf[l-1];
};
vector<pair<ll, ll>> f(n+1, {-INF, 0});
f[0] = {0, 0};
ImplicitSegmentTree T(2e14);
T.apply(0, {0, 0});
for(int i = 1; i <= n; i++){
f[i] = T.get(0, pf[i]);
f[i].first++;
f[i].second += pf[i];
T.apply(pf[i]+f[i].second, {f[i].first, -pf[i]});
}
cout << f[n].first;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("main.inp", "r", stdin);
//freopen("main.out", "w", stdout);
solve();
}
Compilation message
segments.cpp: In function 'void solve()':
segments.cpp:119:10: warning: variable 'sum' set but not used [-Wunused-but-set-variable]
119 | auto sum = [&](int l, int r) -> ll{
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
3 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
3 ms |
1992 KB |
Output is correct |
22 |
Correct |
3 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
3 ms |
1992 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
520 KB |
Output is correct |
29 |
Correct |
2 ms |
784 KB |
Output is correct |
30 |
Correct |
2 ms |
784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
3 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
3 ms |
1992 KB |
Output is correct |
22 |
Correct |
3 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
3 ms |
1992 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
520 KB |
Output is correct |
29 |
Correct |
2 ms |
784 KB |
Output is correct |
30 |
Correct |
2 ms |
784 KB |
Output is correct |
31 |
Correct |
8 ms |
4036 KB |
Output is correct |
32 |
Correct |
8 ms |
7884 KB |
Output is correct |
33 |
Correct |
9 ms |
7632 KB |
Output is correct |
34 |
Correct |
9 ms |
7764 KB |
Output is correct |
35 |
Correct |
8 ms |
8036 KB |
Output is correct |
36 |
Correct |
9 ms |
7864 KB |
Output is correct |
37 |
Correct |
10 ms |
7628 KB |
Output is correct |
38 |
Correct |
8 ms |
7616 KB |
Output is correct |
39 |
Correct |
5 ms |
1356 KB |
Output is correct |
40 |
Correct |
9 ms |
7872 KB |
Output is correct |
41 |
Correct |
5 ms |
1356 KB |
Output is correct |
42 |
Correct |
6 ms |
1992 KB |
Output is correct |
43 |
Correct |
5 ms |
1040 KB |
Output is correct |
44 |
Correct |
6 ms |
4036 KB |
Output is correct |
45 |
Correct |
6 ms |
4036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
3 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
3 ms |
1992 KB |
Output is correct |
22 |
Correct |
3 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
3 ms |
1992 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
520 KB |
Output is correct |
29 |
Correct |
2 ms |
784 KB |
Output is correct |
30 |
Correct |
2 ms |
784 KB |
Output is correct |
31 |
Correct |
8 ms |
4036 KB |
Output is correct |
32 |
Correct |
8 ms |
7884 KB |
Output is correct |
33 |
Correct |
9 ms |
7632 KB |
Output is correct |
34 |
Correct |
9 ms |
7764 KB |
Output is correct |
35 |
Correct |
8 ms |
8036 KB |
Output is correct |
36 |
Correct |
9 ms |
7864 KB |
Output is correct |
37 |
Correct |
10 ms |
7628 KB |
Output is correct |
38 |
Correct |
8 ms |
7616 KB |
Output is correct |
39 |
Correct |
5 ms |
1356 KB |
Output is correct |
40 |
Correct |
9 ms |
7872 KB |
Output is correct |
41 |
Correct |
5 ms |
1356 KB |
Output is correct |
42 |
Correct |
6 ms |
1992 KB |
Output is correct |
43 |
Correct |
5 ms |
1040 KB |
Output is correct |
44 |
Correct |
6 ms |
4036 KB |
Output is correct |
45 |
Correct |
6 ms |
4036 KB |
Output is correct |
46 |
Correct |
154 ms |
104096 KB |
Output is correct |
47 |
Correct |
182 ms |
104076 KB |
Output is correct |
48 |
Correct |
232 ms |
202652 KB |
Output is correct |
49 |
Correct |
214 ms |
202628 KB |
Output is correct |
50 |
Correct |
213 ms |
202644 KB |
Output is correct |
51 |
Runtime error |
256 ms |
262144 KB |
Execution killed with signal 9 |
52 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
3 ms |
1356 KB |
Output is correct |
18 |
Correct |
3 ms |
1992 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
3 ms |
1992 KB |
Output is correct |
22 |
Correct |
3 ms |
1996 KB |
Output is correct |
23 |
Correct |
2 ms |
1360 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
3 ms |
1992 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
520 KB |
Output is correct |
29 |
Correct |
2 ms |
784 KB |
Output is correct |
30 |
Correct |
2 ms |
784 KB |
Output is correct |
31 |
Correct |
8 ms |
4036 KB |
Output is correct |
32 |
Correct |
8 ms |
7884 KB |
Output is correct |
33 |
Correct |
9 ms |
7632 KB |
Output is correct |
34 |
Correct |
9 ms |
7764 KB |
Output is correct |
35 |
Correct |
8 ms |
8036 KB |
Output is correct |
36 |
Correct |
9 ms |
7864 KB |
Output is correct |
37 |
Correct |
10 ms |
7628 KB |
Output is correct |
38 |
Correct |
8 ms |
7616 KB |
Output is correct |
39 |
Correct |
5 ms |
1356 KB |
Output is correct |
40 |
Correct |
9 ms |
7872 KB |
Output is correct |
41 |
Correct |
5 ms |
1356 KB |
Output is correct |
42 |
Correct |
6 ms |
1992 KB |
Output is correct |
43 |
Correct |
5 ms |
1040 KB |
Output is correct |
44 |
Correct |
6 ms |
4036 KB |
Output is correct |
45 |
Correct |
6 ms |
4036 KB |
Output is correct |
46 |
Correct |
154 ms |
104096 KB |
Output is correct |
47 |
Correct |
182 ms |
104076 KB |
Output is correct |
48 |
Correct |
232 ms |
202652 KB |
Output is correct |
49 |
Correct |
214 ms |
202628 KB |
Output is correct |
50 |
Correct |
213 ms |
202644 KB |
Output is correct |
51 |
Runtime error |
256 ms |
262144 KB |
Execution killed with signal 9 |
52 |
Halted |
0 ms |
0 KB |
- |