#include <bits/stdc++.h> using namespace std; const int mx=3e5+5; int n,k,q,a,b; int t[mx]; long long pref[mx],dp[mx]; void solve(int a,int b){ for(int i=a-1;i<=b;++i){ dp[i]=0; } for(int i=a+k-1;i<=b;++i){ dp[i]=max(dp[i],dp[i-1]); dp[i]=max(dp[i],pref[i]-pref[i-k]+dp[i-k]); } cout<<dp[b]<<"\n"; } int main(){ ios::sync_with_stdio(false); cin>>n>>k>>q; for(int i=1;i<=n;++i){ cin>>t[i]; pref[i]=pref[i-1]+t[i]; } for(int i=0;i<q;++i){ cin>>a>>b; solve(a,b); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <bits/stdc++.h> using namespace std; const int mx=3e5+5; int n,k,q,a,b; int t[mx]; long long pref[mx],dp[mx]; void solve(int a,int b){ for(int i=a-1;i<=b;++i){ dp[i]=0; } for(int i=a+k-1;i<=b;++i){ dp[i]=max(dp[i],dp[i-1]); dp[i]=max(dp[i],pref[i]-pref[i-k]+dp[i-k]); } cout<<dp[b]<<"\n"; } int main(){ ios::sync_with_stdio(false); cin>>n>>k>>q; for(int i=1;i<=n;++i){ cin>>t[i]; pref[i]=pref[i-1]+t[i]; } for(int i=0;i<q;++i){ cin>>a>>b; solve(a,b); } return 0; } |