Submission #1118709

#TimeUsernameProblemLanguageResultExecution timeMemory
1118709HuyATBigger segments (IZhO19_segments)C++14
100 / 100
270 ms42316 KiB
#include<bits/stdc++.h> #define newl '\n' const int N = 5e5 + 10; const int V = 1e7 + 10; const long long S = 1e16; const long long INF = 1e18; const long long M = 1e9 + 7; struct Node{ int ans; long long s; Node() : ans(0),s(0){ } Node(int ans,long long s) : ans(ans),s(s){ } bool operator < (const Node &other){ return (ans < other.ans || (ans == other.ans && s < other.s)); } }; int a[N + 1],n; long long s[N + 1]; void readData(){ std::cin >> n; for(int i = 1;i <= n;++i){ std::cin >> a[i]; s[i] = s[i - 1] + a[i]; } } int solve(){ std::map<long long,Node> m; auto add = [&](long long pos,Node val){ auto it = m.lower_bound(pos); if(it != m.end() && val < it->second){ return; } it = m.insert(it,{pos,val}); it->second = val; while(next(it) != m.end() && next(it)->second < val){ m.erase(next(it)); } }; auto query = [&](long long pos)-> Node{ auto it = m.upper_bound(pos); return prev(it)->second; }; add(0,Node()); int ans = 0; for(int i = 1;i <= n;++i){ // std::cout << st.root->ans << newl; Node t = query(s[i]); // std::cerr << t.ans << " " << t.s << " " << s[i] - t.s + s[i] << " " << s[i] << newl; ans = std::max(ans,t.ans + 1); add(s[i] - t.s + s[i],Node(t.ans + 1,s[i])); } return ans; } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr);std::cout.tie(nullptr); readData(); std::cout << solve(); return 0; }
#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...