Submission #842124

#TimeUsernameProblemLanguageResultExecution timeMemory
842124hentai_loverBigger segments (IZhO19_segments)C++14
0 / 100
1 ms352 KiB
#include <bits/stdc++.h> using namespace std; #define int long long //typedef long long int; const int oo = 1 * 1e9 * 5e5 + 322; struct kek{ int l = -1; int r = -1; pair<int, int> ans; } emp; struct ST{ vector<kek> t; void uni(){ t.push_back(emp); } void upd(int poz, pair<int,int> val, int v = 0, int l = 0, int r = oo){ if(l == r){t[v].ans = max(t[v].ans, val);return;} int c = (l + r)>>1; if(t[v].l < 0){t[v].l = t.size(); t.push_back(emp);} if(t[v].r < 0){t[v].r = t.size(); t.push_back(emp);} if(poz <= c)upd(poz, val, t[v].l, l, c); else upd(poz, val, t[v].r, c + 1, r); t[v].ans = max(t[t[v].l].ans, t[t[v].r].ans); } pair<int, int> get(int l, int r, int v = 0, int tl = 0, int tr = oo){ if(l > r)return {-228, -228}; if(tl == l && tr == r)return t[v].ans; int c = (tl + tr) >> 1; if(t[v].l < 0){t[v].l = t.size(); t.push_back(emp);} if(t[v].r < 0){t[v].r = t.size(); t.push_back(emp);} return max(get(l, min(r, c), t[v].l, tl, c), get(max(l, c + 1), r, t[v].r, c + 1, tr)); } }; int32_t main() { ios_base::sync_with_stdio(0); int n; cin>>n; int a[n]; for(int i = 0; i < n; i += 1)cin>>a[i]; int sf[n + 1] = {}; for(int i = n - 1; i >= 0; i -= 1)sf[i] = sf[i + 1] + a[i]; ///dp[p] = {ans, min_sum} pair<int,long long> dp[n]; long long sum = 0; for(int i = 0; i < n; i += 1){ sum += a[i]; dp[i] = {1, sum}; } ST tree; tree.uni(); tree.upd(sf[1] - dp[0].second, {1, 0}); for(int i = 1; i < n; i += 1){ sum = 0; pair<int,int>temp = tree.get(sf[i + 1], oo); //cerr<<"i = "<<i<<" temp: "<<temp.first<<" "<<temp.second<<endl; dp[i] = max(dp[i], {temp.first + 1, sf[temp.second + 1] - sf[i + 1]}); tree.upd(sf[i + 1] - dp[i].second, {dp[i].first, i}); } // cout<<"dp:"; // for(int i = 0; i < n; i += 1)cout<<dp[i].first<<' '<<dp[i].second<<endl; int ans = 0; for(int i = 0; i < n; i += 1){ ans = max(ans, dp[i].first); } cout<<ans; } /* 5 6 2 3 9 13 */
#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...