#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; typedef long long ll; const int MAXN = 300010; ll vs[MAXN], lp[MAXN], dp[MAXN]; ll n, k, q; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k >> q; ll left = 0; for(int i = 1; i <= n; i++) { cin >> vs[i]; left += vs[i]; if (i >= k) { left -= vs[i-k]; lp[i] += left; } // cout << i << ' ' << lp[i] << endl; } ll x, y; for(int i = 0; i < q; i++) { cin >> x >> y; memset(dp, 0, (n+1) * sizeof(ll)); // cout << x << ' ' << y << endl; for(int j = x+k-1; j <= y; j++) { dp[j] = max(dp[j-1], dp[j-k] + lp[j]); // cout << j << ' ' << dp[j-1] << ' ' << dp[j-k] << ' ' << lp[j] << endl; } cout << dp[y] << endl; // cout << endl; } }
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 41 42 43 44 45 | #include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; typedef long long ll; const int MAXN = 300010; ll vs[MAXN], lp[MAXN], dp[MAXN]; ll n, k, q; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k >> q; ll left = 0; for(int i = 1; i <= n; i++) { cin >> vs[i]; left += vs[i]; if (i >= k) { left -= vs[i-k]; lp[i] += left; } // cout << i << ' ' << lp[i] << endl; } ll x, y; for(int i = 0; i < q; i++) { cin >> x >> y; memset(dp, 0, (n+1) * sizeof(ll)); // cout << x << ' ' << y << endl; for(int j = x+k-1; j <= y; j++) { dp[j] = max(dp[j-1], dp[j-k] + lp[j]); // cout << j << ' ' << dp[j-1] << ' ' << dp[j-k] << ' ' << lp[j] << endl; } cout << dp[y] << endl; // cout << endl; } } |