# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
885903 | dimashhh | Bigger segments (IZhO19_segments) | C++17 | 4 ms | 8796 KiB |
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;
const int N = 1e6 + 1,MOD = 998244353;
typedef long long ll;
int n,a[N];
ll dp[N],mn[N],pref[N];
mt19937 rng(123321);
struct node{
node *l = 0,*r = 0;
ll mx,val,prior,key;
node(ll key,ll val):key(key),val(val),mx(val),prior(rng()){}
};
using pnode = node *;
ll mx(pnode f){
return (f ? f->mx : 0);
}
void upd(pnode &f){
f->mx = max({f->val,mx(f->l),mx(f->r)});
}
pnode merge(pnode a,pnode b){
if(!a) return b;
if(!b) return a;
if(a->prior > b->prior){
a->r = merge(a->r,b);
upd(a);
return a;
}else{
b->l = merge(a,b->l);
upd(b);
return b;
}
}
pair<pnode,pnode> split(pnode x,int val){
if(!x) return {0,0};
if(x->key <= val){
auto [a,b] = split(x->r,val);
x->r = a;
upd(x);
return {x,b};
}else{
auto [a,b] = split(x->l,val);
x->l = b;
upd(x);
return {a,x};
}
}
pnode root = NULL;
void insert(ll key,ll val){
pnode nv = new node(key,val);
auto [x,y] = split(root,key);
root = merge(x,merge(nv,y));
}
void test(){
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
insert(0,0);
for(int i = 1;i <=n;i++){
ll cur = 0;
auto [x,y] = split(root,pref[i]);
// if(!x) continue;
ll val = x->mx;
dp[i] = dp[val] + 1;
mn[i] = pref[i] - pref[val];
root = merge(x,y);
insert(mn[i] + pref[i],i);
// for(int j = i;j >= 1;j--){
// if(mn[j - 1] + pref[j - 1] <= pref[i]){
// if(dp[j - 1] + 1 > dp[i]){
// dp[i] = dp[j - 1] +1;
// mn[i] = pref[i] - pref[j - 1];
// }
// }
// }
}
cout << dp[n];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
test();
}
Compilation message (stderr)
# | 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... |