Submission #200542

#TimeUsernameProblemLanguageResultExecution timeMemory
200542a1_NBigger segments (IZhO19_segments)C++17
37 / 100
1593 ms148932 KiB
#include <bits/stdc++.h> #define F first #define S second using namespace std; const int N = 5e5 + 5; const long long M = (long long)1e15; int n,a[N]; long long pref[N]; pair<int,long long> dp[N]; unordered_map< long long,pair<int,long long> > t; void upd(long long pos,pair<long long,long long> val,long long v = 1,long long tl = 1,long long tr = M){ if(tl == tr){ t[v] = val; return; } long long tm = (tl + tr) >> 1; if(pos <= tm) upd(pos,val,v << 1,tl,tm); else upd(pos,val,v << 1 | 1,tm + 1,tr); t[v] = max(t[v << 1],t[v << 1 | 1]); } pair<int,long long> get(long long l,long long r,long long v = 1,long long tl = 1,long long tr = M){ if(l <= tl && tr <= r) return t[v]; if(tl > r || tr < l) return make_pair(0,0); long long tm = (tl + tr) >> 1; return max(get(l,r,v << 1,tl,tm),get(l,r,v << 1 | 1,tm + 1,tr)); } int main(){ //ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); scanf("%d",&n); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); pref[i] = pref[i - 1] + a[i]; } dp[1] = make_pair(1,a[1]); upd(pref[1] + dp[1].S,make_pair(dp[1].F,1)); for(int i = 2; i <= n; i++){ dp[i] = make_pair(dp[i - 1].F,dp[i - 1].S + a[i]); pair<int,long long> cur = get(1,pref[i]); if(cur.F > 0) dp[i] = make_pair(cur.F + 1,pref[i] - pref[cur.S]); upd(pref[i] + dp[i].S,make_pair(dp[i].F,i)); } printf("%d",dp[n].F); return 0; }

Compilation message (stderr)

segments.cpp: In function 'int main()':
segments.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
segments.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);
   ~~~~~^~~~~~~~~~~~
#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...