Submission #343207

#TimeUsernameProblemLanguageResultExecution timeMemory
343207NhatMinh0208Split the sequence (APIO14_sequence)C++14
71 / 100
574 ms131076 KiB
/* Normie's Template v2.0 */ // Standard library in one include. #include <bits/stdc++.h> using namespace std; // ordered_set library. #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update> // AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.) //#include <atcoder/all> //using namespace atcoder; //Pragmas (Comment out these three lines if you're submitting in szkopul.) #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //File I/O. #define FILE_IN "cseq.inp" #define FILE_OUT "cseq.out" #define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout) //Fast I/O. #define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define nfio cin.tie(0);cout.tie(0) #define endl "\n" //Order checking. #define ord(a,b,c) ((a>=b)and(b>=c)) //min/max redefines, so i dont have to resolve annoying compile errors. #define min(a,b) (((a)<(b))?(a):(b)) #define max(a,b) (((a)>(b))?(a):(b)) //Constants. #define MOD (ll(998244353)) #define MAX 300001 #define mag 320 //Pairs and 3-pairs. #define p1 first #define p2 second.first #define p3 second.second #define fi first #define se second #define pii(element_type) pair<element_type,element_type> #define piii(element_type) pair<element_type,pii(element_type)> //Quick power of 2. #define pow2(x) (ll(1)<<x) //Short for-loops. #define ff(i,__,___) for(int i=__;i<=___;i++) #define rr(i,__,___) for(int i=__;i>=___;i--) //Typedefs. #define bi BigInt typedef long long ll; typedef long double ld; typedef short sh; //---------END-------// struct node { node *nl=0, *nr=0; ll dom=0; ~node() { if (nl) delete(nl); if (nr) delete(nr); } }; struct dynamic_li_chao { vector<pii(ll)> func={{0,3e18+7}}; ll calc (ll cur, ll x) { return (func[cur].fi)*x+(func[cur].se); } node start; void add_line(ll a, ll b, ll l, ll r) { func.emplace_back(a,b); update(-1e9,1e9+1,&start,func.size()-1,l,r); } void update(ll l, ll r, node* cur, ll diff , ll tl, ll tr) { if ((l>=tl)and(r-1<=tr)) { if (cur->dom==0) { cur->dom=diff; } else if (l+1==r) { if (calc(cur->dom,l)>calc(diff,l)) cur->dom=diff; } else { ll mid=(l+r)/2; ll doml=(calc(cur->dom,l)>calc(diff,l)); ll domm=(calc(cur->dom,mid)>calc(diff,mid)); if (domm) { ll z=cur->dom; cur->dom=diff; diff=z; } if (doml==domm) { if (!cur->nr) cur->nr=new(node); update(mid,r,cur->nr,diff,tl,tr); } else { if (!cur->nl) cur->nl=new(node); update(l,mid,cur->nl,diff,tl,tr); } } } else if ((l>tr)or(r-1<tl)) return; else { ll mid=(l+r)/2; if (!cur->nr) cur->nr=new(node); update(mid,r,cur->nr,diff,tl,tr); if (!cur->nl) cur->nl=new(node); update(l,mid,cur->nl,diff,tl,tr); } } pii(ll) get(ll x) { return getK(-1e9,1e9+1,&start,x); } pii(ll) getK(ll l, ll r, node* cur, ll tar) { pii(ll) res1={calc(cur->dom,tar),cur->dom}; if (l+1==r) return res1; pii(ll) res2={3e18+7,0}; ll mid=(l+r)/2; if ((tar<mid)and(cur->nl)) res2=getK(l,mid,cur->nl,tar); else if ((tar>=mid)and(cur->nr)) res2=getK(mid,r,cur->nr,tar); if (res1.fi<res2.fi) return res1; else return res2; } }; ll n,k,m,i,j,t,t1,u,v,a,b; ll dp[100001][201]; int opt[100001][201]; ll pre[100001]; ll arr[100001]; vector<ll> res; int main() { fio; cin>>n>>m; m++; for (i=1;i<=n;i++) { cin>>arr[i]; pre[i]=pre[i-1]+arr[i]; } for (i=0;i<=n;i++) dp[i][0]=1e18; dp[0][0]=0; for (j=1;j<=m;j++) { dynamic_li_chao bruh; for (i=j;i<=n;i++) { bruh.add_line(-2*pre[i-1],dp[i-1][j-1]+pre[i-1]*pre[i-1],0,1e9); auto u=bruh.get(pre[i]); dp[i][j]=u.fi+pre[i]*pre[i]; opt[i][j]=u.se+(j-2); } } cout<<(pre[n]*pre[n]-dp[n][m])/2<<endl; i=n; for (j=m;j>=1;j--) { i=opt[i][j]; if (i) res.push_back(i); } for (i=m-2;i>=0;i--) cout<<res[i]<<' '; }

Compilation message (stderr)

sequence.cpp:20: warning: ignoring #pragma comment  [-Wunknown-pragmas]
   20 | #pragma comment(linker, "/stack:200000000")
      |
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...