#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N = (int)5e5 + 5;
const long long M = (long long)5e14;
int n,a[N];
long long pref[N];
pair<int,long long> dp[N];
map<int,int> Pos;
struct tree{
tree *left,*right;
int mx;
tree(): left(NULL), right(NULL), mx(0) {}
};
tree *root = new tree();
int get_mx(tree *t){
return (t != NULL) ? (t->mx) : 0;
}
void upd(tree *t,long long tl,long long tr,long long pos,int val){
if(tl == tr){
t->mx = val;
return;
}
long long tm = (tl + tr) / 2;
if(pos <= tm){
if(t->left == NULL) t->left = new tree();
upd(t->left,tl,tm,pos,val);
}
else{
if(t->right == NULL) t->right = new tree();
upd(t->right,tm + 1,tr,pos,val);
}
t->mx = max(get_mx(t->left),get_mx(t->right));
}
int get(tree *t,long long tl,long long tr,long long l,long long r){
if(l <= tl && tr <= r) return t->mx;
if(l > tr || r < tl) return 0;
long long tm = (tl + tr) / 2;
int mx1 = (t->left != NULL) ? get(t->left,tl,tm,l,r) : 0;
int mx2 = (t->right != NULL) ? get(t->right,tm + 1,tr,l,r) : 0;
return max(mx1,mx2);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
dp[1] = {1,a[1]};
upd(root,1,M,pref[1] + dp[1].S,dp[1].F);
Pos[dp[1].F] = 1;
for(int i = 2; i <= n; i++){
dp[i] = {dp[i - 1].F,dp[i - 1].S + a[i]};
int cur = get(root,1,M,1,pref[i]);
if(cur > 0) dp[i] = {cur + 1,pref[i] - pref[Pos[cur]]};
/* pref[i] - pref[j] >= dp[j].S
pref[i] >= pref[j] + dp[j].S
1) we update value in position pref[j] + dp[j].S
2) when we want to find answer satisfying condition for this position we use segment tree
*/
upd(root,1,M,pref[i] + dp[i].S,dp[i].F);
Pos[dp[i].F] = i;
}
cout << dp[n].F;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |