This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
long long s, e;
ll mx;
bool lset;
ll set_val;
node *l, *r;
node (long long _s, long long _e, int A[] = NULL): s(_s), e(_e), mx(0), lset(0), set_val(0), l(NULL), r(NULL) {
if (A == NULL) return;
if (s == e) mx = A[s];
else {
l = new node(s, (s+e)>>1, A), r = new node((s+e+2)>>1, e, A);
combine();
}
}
void create_children() {
if (s == e) return;
if (l != NULL) return;
int m = (s+e)>>1;
l = new node(s, m);
r = new node(m+1, e);
}
void self_set(ll v) {
lset = 1;
mx = set_val = v;
}
void lazy_propagate() {
if (s == e) return;
if (lset) {
l->self_set(set_val), r->self_set(set_val);
lset = set_val = 0;
}
}
void combine() {
if (l == NULL) return;
mx = max(l->mx, r->mx);
}
void set(int x, int y, ll v) {
if (s == x && e == y) { self_set(v); return; }
int m = (s+e)>>1;
create_children(); lazy_propagate();
if (x <= m) l->set(x, min(y, m), v);
if (y > m) r->set(max(x, m+1), y, v);
combine();
}
ll range_max(int x, int y) {
if (s == x && e == y) return mx;
if (l == NULL || lset) return mx;
int m = (s+e)>>1;
lazy_propagate();
if (y <= m) return l->range_max(x, y);
if (x > m) return r->range_max(x, y);
return max(l->range_max(x, m), r->range_max(m+1, y));
}
~node() {
if (l != NULL) delete l;
if (r != NULL) delete r;
}
} *root;
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
long long arr[n],pref[n+1];
for(int i=0; i<n; i++) cin >> arr[i];
pref[0]=0;
for(int i=0; i<n; i++){
pref[i+1]=pref[i]+arr[i];
}
pair<int,long long> dp[n+1];
dp[0]={0,0};
root=new node(0,(long long)1e15);
for(int i=1; i<=n; i++){
int x=root->range_max(0,pref[i]);
dp[i]={dp[x].first+1LL,pref[i]-pref[x]};
root->set(dp[i].second+pref[i],dp[i].second+pref[i],i);
}
cout << dp[n].first;
return 0;
}
Compilation message (stderr)
segments.cpp: In member function 'void node::lazy_propagate()':
segments.cpp:33:28: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
33 | lset = set_val = 0;
| ~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |