#include<bits/stdc++.h> #define maxn 300010 #define inf 1000000007 using namespace std; long long tab[maxn]; long long dp[maxn]; int k; long long odp(int a, int b) { for (int i = a; i <= b; ++i) { if (i-a + 1 < k) dp[i] = 0; else if (i-a + 1 == k) dp[i] = max(tab[i]-tab[i-k], (long long)0); else dp[i] = max(tab[i]-tab[i-k] + dp[i-k], dp[i-1]); //cout << dp[i] << " "; } //cout << endl; return dp[b]; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n,q; cin >> n >> k >> q; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i-1]; } int a, b; for (int i = 0; i < q; ++i) { cin >> a >> b; cout << odp(a,b) << "\n"; } }
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 30 31 32 33 34 35 36 37 38 39 40 | #include<bits/stdc++.h> #define maxn 300010 #define inf 1000000007 using namespace std; long long tab[maxn]; long long dp[maxn]; int k; long long odp(int a, int b) { for (int i = a; i <= b; ++i) { if (i-a + 1 < k) dp[i] = 0; else if (i-a + 1 == k) dp[i] = max(tab[i]-tab[i-k], (long long)0); else dp[i] = max(tab[i]-tab[i-k] + dp[i-k], dp[i-1]); //cout << dp[i] << " "; } //cout << endl; return dp[b]; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n,q; cin >> n >> k >> q; for (int i = 1; i <= n; ++i) { cin >> tab[i]; tab[i] += tab[i-1]; } int a, b; for (int i = 0; i < q; ++i) { cin >> a >> b; cout << odp(a,b) << "\n"; } } |