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;
typedef long long ll;
#define MP make_pair
struct SegmentTree{
int size = 1;
vector<pair<int, ll>> tree;
SegmentTree(int n){
size = n;
tree.resize((1 << 20) + 50);
};
void update(int v, int tl, int tr, int pos, pair<int, ll> val){
if (tl == tr){
tree[v] = max(tree[v], val);
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) update(v << 1, tl, tm, pos, val); else
update(v << 1 | 1, tm + 1, tr, pos, val);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
void update(int pos, pair<int, ll> val){
update(1, 0, size - 1, pos, val);
}
pair<int, ll> get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return MP(0, 0);
int tm = (tl + tr) >> 1;
return max(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
pair<int, ll> get(int l, int r){
return get(1, 0, size - 1, l, r);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
vector<ll> pref(n);
for (int i = 0; i < n; i++){
cin >> pref[i];
if (i) pref[i] += pref[i - 1];
}
vector<int> dp(n, 1);
vector<ll> sum = pref;
SegmentTree st(n);
for (int i = 0; i < n; i++){
auto p = st.get(0, i);
if (p.first > dp[i] || p.second == dp[i] || pref[i] - p.second < sum[i]){
dp[i] = p.first + 1;
sum[i] = pref[i] - p.second;
}
int pos = lower_bound(pref.begin(), pref.end(), pref[i] + sum[i]) - pref.begin();
if (pos < n) st.update(pos, MP(dp[i], pref[i]));
}
cout << dp[n - 1] << endl;
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... |