#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; #define st first #define nd second typedef pair<int,int> pun; typedef long long ll; int main() { ios_base::sync_with_stdio(false); int n, k, q; cin >> n >> k >> q; vector<int> v; v.push_back(0); vector<ll> pref; pref.push_back(0); for (int i = 0; i < n; i ++) { int x; cin >> x; v.push_back(x); pref.push_back(pref.back() + x); } vector<vector<pair<int,int>>> zap; zap.resize(n+1); for (int i = 0; i < q; i ++) { int a, b; cin >> a >> b; zap[a].emplace_back(b, i); } for (int i = 1; i <= n; i ++) { sort(zap[i].begin(), zap[i].end()); } vector<ll> ans; ans.resize(q); for (int i = 1; i <= n; i ++) { if (zap[i].empty()) continue; vector<ll> dp; dp.resize(n+1, 0); for (int j = i + k - 1; j <= zap[i].back().st; j ++) { dp[j] = max(dp[j-1], dp[j-k] + pref[j] - pref[j-k]); dp[j] = max(0LL, dp[j]); // cerr << "dp " << j << " " << dp[j] << "\n"; } for (auto p : zap[i]) { ans[p.nd] = dp[p.st]; } } for (auto x : ans) { cout << x << "\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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; #define st first #define nd second typedef pair<int,int> pun; typedef long long ll; int main() { ios_base::sync_with_stdio(false); int n, k, q; cin >> n >> k >> q; vector<int> v; v.push_back(0); vector<ll> pref; pref.push_back(0); for (int i = 0; i < n; i ++) { int x; cin >> x; v.push_back(x); pref.push_back(pref.back() + x); } vector<vector<pair<int,int>>> zap; zap.resize(n+1); for (int i = 0; i < q; i ++) { int a, b; cin >> a >> b; zap[a].emplace_back(b, i); } for (int i = 1; i <= n; i ++) { sort(zap[i].begin(), zap[i].end()); } vector<ll> ans; ans.resize(q); for (int i = 1; i <= n; i ++) { if (zap[i].empty()) continue; vector<ll> dp; dp.resize(n+1, 0); for (int j = i + k - 1; j <= zap[i].back().st; j ++) { dp[j] = max(dp[j-1], dp[j-k] + pref[j] - pref[j-k]); dp[j] = max(0LL, dp[j]); // cerr << "dp " << j << " " << dp[j] << "\n"; } for (auto p : zap[i]) { ans[p.nd] = dp[p.st]; } } for (auto x : ans) { cout << x << "\n"; } } |