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>
#define newl '\n'
const int N = 5e5 + 10;
const int V = 1e7 + 10;
const long long S = 1e16;
const long long INF = 1e18;
const long long M = 1e9 + 7;
struct Node{
int ans;
long long s;
Node *l,*r;
Node() : ans(0),s(0),l(nullptr),r(nullptr){
}
Node(int ans,long long s) : ans(ans),s(s),l(nullptr),r(nullptr){
}
friend Node operator + (const Node &a,const Node &b){
if(a.ans > b.ans){
return a;
}
if(a.ans < b.ans){
return b;
}
return Node(a.ans,std::max(a.s,b.s));
}
Node operator = (const Node &other){
ans = other.ans;
s = other.s;
return *this;
}
};
typedef Node* pNode;
struct SparseSeg{
pNode root;
SparseSeg(){
root = nullptr;
}
private:
Node get_val(const pNode &t){
return (!t ? Node() : *t);
}
void update(long long l,long long r,long long pos,Node val,pNode &t){
// std::cout << l << " " << r << newl;
if(t == nullptr){
t = new Node();
}
if(l == r){
*t = *t + val;
return;
}
long long mid = (l + r) / 2;
if(pos <= mid){
update(l,mid,pos,val,t->l);
}else{
update(mid + 1,r,pos,val,t->r);
}
*t = get_val(t->l) + get_val(t->r);
}
Node query(long long l,long long r,long long u,long long v,pNode &t){
if(!t){
return Node();
}
if(u <= l && r <= v){
return *t;
}
long long mid = (l + r) / 2;
if(v <= mid){
return query(l,mid,u,v,t->l);
}else if(u > mid){
return query(mid + 1,r,u,v,t->r);
}
return query(l,mid,u,v,t->l) + query(mid + 1,r,u,v,t->r);
}
public:
void update(long long l,long long r,long long pos,Node val){
update(l,r,pos,val,root);
}
Node query(long long l,long long r,long long u,long long v){
return query(l,r,u,v,root);
}
};
int a[N + 1],s[N + 1],n;
void readData(){
std::cin >> n;
for(int i = 1;i <= n;++i){
std::cin >> a[i];
s[i] = s[i - 1] + a[i];
}
}
int solve(){
SparseSeg st;
st.update(0,S,0,Node());
int ans = 0;
for(int i = 1;i <= n;++i){
// std::cout << st.root->ans << newl;
Node t = st.query(0,S,0,s[i]);
ans = std::max(ans,t.ans + 1);
st.update(0,S,s[i] - t.s + s[i],Node(t.ans + 1,s[i]));
}
return ans;
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);std::cout.tie(nullptr);
readData();
std::cout << solve();
return 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... |