Submission #885904

#TimeUsernameProblemLanguageResultExecution timeMemory
885904dimashhhBigger segments (IZhO19_segments)C++17
100 / 100
262 ms58240 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 1,MOD = 998244353; typedef long long ll; #define int long long 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)

segments.cpp: In constructor 'node::node(ll, ll)':
segments.cpp:13:21: warning: 'node::key' will be initialized after [-Wreorder]
   13 |     ll mx,val,prior,key;
      |                     ^~~
segments.cpp:13:11: warning:   'll node::val' [-Wreorder]
   13 |     ll mx,val,prior,key;
      |           ^~~
segments.cpp:14:5: warning:   when initialized here [-Wreorder]
   14 |     node(ll key,ll val):key(key),val(val),mx(val),prior(rng()){}
      |     ^~~~
segments.cpp:13:11: warning: 'node::val' will be initialized after [-Wreorder]
   13 |     ll mx,val,prior,key;
      |           ^~~
segments.cpp:13:8: warning:   'll node::mx' [-Wreorder]
   13 |     ll mx,val,prior,key;
      |        ^~
segments.cpp:14:5: warning:   when initialized here [-Wreorder]
   14 |     node(ll key,ll val):key(key),val(val),mx(val),prior(rng()){}
      |     ^~~~
segments.cpp: In function 'void test()':
segments.cpp:64:12: warning: unused variable 'cur' [-Wunused-variable]
   64 |         ll cur = 0;
      |            ^~~
segments.cpp: In function 'int main()':
segments.cpp:87:9: warning: unused variable 'T' [-Wunused-variable]
   87 |     int T = 1;
      |         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...