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;
#define int long long
#define fi first
#define se second
#define pb push_back
#define debug(x) cout<<#x<<": "<<x<<"\n"
struct node
{
int s,e,m,val;
node *l,*r;
node(int S,int E)
{
s=S;e=E;m=(s+e)/2;
val=0;
l=nullptr;r=nullptr;
}
void create()
{
if (s!=e)
{
l=new node(s,m);
r=new node(m+1,e);
}
}
void update(int X,int V)
{
if (l==nullptr) create();
if (s==e and s==X) val=max(val,V);
else
{
if (X<=m) l->update(X,V);
else r->update(X,V);
val=max(l->val,r->val);
}
}
int query(int S,int E)
{
if (l==nullptr) create();
if (s==S and e==E) return val;
else if (E<=m) return l->query(S,E);
else if (S>=m+1) return r->query(S,E);
else return max(l->query(S,m), r->query(m+1,E));
}
}*root;
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// freopen("input.000","r",stdin);
// freopen("output.000","w",stdout);
// srand((unsigned)time(NULL));
// rand()
int n,i,a[500005],pre[500005],opt;
pair<int,int> dp[500005];
cin>>n;
root=new node(0,1e15);
for (i=1;i<n+1;i++) cin>>a[i];
pre[1]=a[1];
for (i=2;i<n+1;i++) pre[i]=pre[i-1]+a[i];
dp[1]={1,-a[1]};
root->update(a[1]+pre[1], 1);
for (i=2;i<n+1;i++)
{
dp[i]={1,-pre[i]};
opt=root->query(0,pre[i]);
// cout<<i<<" "<<opt<<"\n";
if (opt!=0) dp[i]=max(dp[i], {dp[opt].fi+1, -pre[i]+pre[opt]});
root->update(-dp[i].se+pre[i], i);
}
// for (i=1;i<n+1;i++) cout<<dp[i].fi<<" "<<dp[i].se<<"\n";
cout<<dp[n].fi;
}
# | 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... |