#include <iostream>
#include <string>
#include <unordered_map>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
#define pii pair<long long, long long>
#define mp make_pair
#define f first
#define se second
#define max3(a, b, c) max(a, max(b, c))
#define int long long
const int inf = 2e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
node *l, *r;
int key, pr;
pii val, mx;
node(int _key, pii _val) {
pr = uniform_int_distribution<>(0, inf)(rng);
l = r = NULL;
key = _key, mx = val = _val;
}
};
typedef node* pnode;
inline pii val(pnode t) {
return t ? t->mx : mp(1LL, 0LL);
}
inline void upd(pnode &t) {
if(t)
t->mx = max3(t->val, val(t->l), val(t->r));
}
inline void reset(pnode &t) {
if(t)
t->mx = t->val;
}
void split(pnode t, pnode &l, pnode &r, int key) {
if(!t)
return void(l = r = t);
if(t->key <= key)
split(t->r, t->r, r, key), l = t;
else
split(t->l, l, t->l, key), r = t;
reset(l);
reset(r);
upd(l);
upd(r);
}
void merge(pnode &t, pnode l, pnode r) {
if(!l || !r)
t = l ? l : r;
else if(l->pr > r->pr)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
reset(t);
upd(t);
}
inline void insert(pnode &t, pnode nw) {
pnode t1, t2;
split(t, t1, t2, nw->key);
merge(t2, t2, nw);
merge(t, t1, t2);
}
pnode root = NULL;
const int mod = 1e9 + 7;
const int N = 5e5 + 15;
int n, a[N], p[N];
pii dp[N];
main() {
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
p[i] = p[i-1] + a[i];
}
root = new node(0, mp(1, 0));
for(int i = 1; i <= n; ++i) {
pnode t1, t2;
split(root, t1, t2, p[i]);
dp[i] = val(t1);
merge(root, t1, t2);
insert(root, new node(2 * p[i] - p[dp[i].se], mp(dp[i].f + 1, i)));
}
cout << dp[n].f << endl;
return 0;
}
Compilation message
segments.cpp:98:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |