#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e5 + 1; int n, q, a, b, k; long long sum[N], res[N]; unordered_map<int, long long>ans[N]; int najw[N]; vector<pair<int, int>> v; void f(int p, int kon) { res[p - 1] = 0; for (int i = p; i <= kon; i++) { res[i] = res[i - 1]; if (i - k >= p - 1) { res[i] = max(res[i], res[i - k] + sum[i] - sum[i - k]); } if (ans[p].find(i) != ans[p].end()) { ans[p][i] = res[i]; } } } int main() { ios_base::sync_with_stdio(0); cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> a; sum[i] = sum[i - 1] + a; } for (int i = 0; i < q; i++) { cin >> a >> b; v.push_back(make_pair(a, b)); ans[a][b] = 0; najw[a] = max(najw[a], b); } for (int p = 1; p <= n; p++) { f(p, najw[a]); } for (int i = 0; i < q; i++) { cout << ans[v[i].first][v[i].second] << endl; } 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 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 = 3 * 1e5 + 1; int n, q, a, b, k; long long sum[N], res[N]; unordered_map<int, long long>ans[N]; int najw[N]; vector<pair<int, int>> v; void f(int p, int kon) { res[p - 1] = 0; for (int i = p; i <= kon; i++) { res[i] = res[i - 1]; if (i - k >= p - 1) { res[i] = max(res[i], res[i - k] + sum[i] - sum[i - k]); } if (ans[p].find(i) != ans[p].end()) { ans[p][i] = res[i]; } } } int main() { ios_base::sync_with_stdio(0); cin >> n >> k >> q; for (int i = 1; i <= n; i++) { cin >> a; sum[i] = sum[i - 1] + a; } for (int i = 0; i < q; i++) { cin >> a >> b; v.push_back(make_pair(a, b)); ans[a][b] = 0; najw[a] = max(najw[a], b); } for (int p = 1; p <= n; p++) { f(p, najw[a]); } for (int i = 0; i < q; i++) { cout << ans[v[i].first][v[i].second] << endl; } return 0; } |