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;
int a[500009];
long long rs[500009];
pair<int,long long> dp[500009];
unordered_map<long long,pair<int,long long> >fw;
struct node{
long long s,e,m;
pair<int,long long> v;
node *l,*r;
node(long long _s,long long _e):s(_s),e(_e),m((_s+_e)/2),v({-1,-1}),l(NULL),r(NULL){}
void up(long long x,pair<int,long long> uv){
if(s==e){
v = max(v,uv);return;
}
if(x>m){
if(r==NULL)r = new node(m+1,e);
r->up(x,uv);
}
else{
if(l==NULL)l = new node(s,m);
l->up(x,uv);
}
v = {-1,-1};
if(r)v = max(v,r->v);
if(l)v = max(v,l->v);
}
pair<int,long long> qu(long long x,long long y){
if(s==x&&e==y)return v;
if(x>m){
if(r)return r->qu(x,y);
else return {-1,-1};
}
if(y<=m){
if(l)return l->qu(x,y);
else return {-1,-1};
}
pair<int,long long> qv = {-1,-1};
if(r)qv = max(qv,r->qu(m+1,y));
if(l)qv = max(qv,l->qu(x,m));
return qv;
}
}*root;
pair<int,long long> query(long long x){
pair<int,long long> ans = {-1,-1};
for(x++;x;x-=x&(-x)){
if(fw.find(x)==fw.end())continue;
ans = max(ans,fw[x]);
}
return ans;
}
void up(long long x,pair<int,long long> v ){
for(x++;x<1e15;x+=x&(-x)){
if(fw.find(x)==fw.end())fw[x] = v;
else fw[x] = max(fw[x],v);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
rs[i+1]=rs[i]+a[i];
}
root = new node(0,(long long)1e15);
root->up(0,{0,0});
for(int i=1;i<=n;i++){
pair<int,long long> res = root->qu(0,rs[i]);
dp[i] = {res.first,res.second - rs[i]};
dp[i].first++;
root->up(rs[i] - dp[i].second,{dp[i].first,rs[i]});
//cout << dp[i].first << ' ' << dp[i].second << '\n';
// cout << res.first << ' ' << res.second.first << ' ' << res.second.second << '\n';
}
printf("%d", dp[n].first);
}
Compilation message (stderr)
segments.cpp: In function 'int main()':
segments.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
segments.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
# | 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... |