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 ll long long
using namespace std;
const int N = 500500;
const int mod = 1e9 + 7;
const ll inf = 1e17 + 10;
int n, a[N], sol, dp[N];
ll pref[N];
ll t[N * 4];
void build(int u,int l,int r) {
t[u] = inf;
if(l == r) return;
int m = l + r >> 1;
build(u << 1,l,m);
build(u << 1 | 1,m + 1,r);
}
void get(int u,int l,int r,ll val) {
if(l == r) {
if(t[u] <= val) {
sol = l;
}
return;
}
int m = l + r >> 1;
if(t[u << 1 | 1] <= val) get(u << 1 | 1,m + 1,r,val);
else get(u << 1,l,m,val);
}
void upd(int p,ll val,int u,int l,int r) {
if(l == r) {
t[u] = val;
return;
}
int m = l + r >> 1;
if(p <= m) {
upd(p,val,u << 1,l,m);
} else upd(p,val,u << 1 | 1,m + 1,r);
t[u] = min(t[u << 1],t[u << 1 | 1]);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
build(1,1,n);
for(int i = 1; i <= n; i++) {
sol = 0;
get(1,1,n,pref[i]);
dp[i] = dp[sol] + 1;
upd(i,2 * pref[i] - pref[sol],1,1,n);
}
cout << dp[n];
}
Compilation message (stderr)
segments.cpp: In function 'void build(int, int, int)':
segments.cpp:18:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int m = l + r >> 1;
| ~~^~~
segments.cpp: In function 'void get(int, int, int, long long int)':
segments.cpp:30:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | int m = l + r >> 1;
| ~~^~~
segments.cpp: In function 'void upd(int, long long int, int, int, int)':
segments.cpp:40:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int m = l + r >> 1;
| ~~^~~
# | 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... |