#include <bits/stdc++.h> using namespace std; int n, k, q; int l, r; long long skill[300007]; long long prefix_skill[300007]; long long squad_skill[300007]; long long dp_skill[300007]; long long compute_result() { if (r - l + 1 < k) { return 0LL; } for (int i = l - 1; i <= r + 1; i++) { dp_skill[i] = 0LL; } // the score is one index after the end of last company for (int i = l; i <= r + 1; i++) { dp_skill[i] = max(dp_skill[i], dp_skill[i - 1]); if (squad_skill[i] > 0 && i <= r - k + 1) { dp_skill[i + k] = max(dp_skill[i + k], dp_skill[i] + squad_skill[i]); } } return dp_skill[r + 1]; } long long ins() { long long a = 0; char c; bool minus = false; do { c = getchar_unlocked(); if (c == '-') { minus = true; } } while (c < '0' || c > '9'); do { a = 10LL * a + c - '0'; c = getchar_unlocked(); } while (c >= '0' && c <= '9'); return minus ? -a : a; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); n = ins(); k = ins(); q = ins(); for (int i = 1; i <= n; i++) { skill[i] = ins(); prefix_skill[i] = prefix_skill[i - 1] + skill[i]; } for (int i = 1; i <= n - k + 1; i++) { squad_skill[i] = prefix_skill[i + k - 1] - prefix_skill[i - 1]; } for (int querry = 0; querry < q; querry++) { l = ins(); r = ins(); cout << compute_result() << "\n"; } 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <bits/stdc++.h> using namespace std; int n, k, q; int l, r; long long skill[300007]; long long prefix_skill[300007]; long long squad_skill[300007]; long long dp_skill[300007]; long long compute_result() { if (r - l + 1 < k) { return 0LL; } for (int i = l - 1; i <= r + 1; i++) { dp_skill[i] = 0LL; } // the score is one index after the end of last company for (int i = l; i <= r + 1; i++) { dp_skill[i] = max(dp_skill[i], dp_skill[i - 1]); if (squad_skill[i] > 0 && i <= r - k + 1) { dp_skill[i + k] = max(dp_skill[i + k], dp_skill[i] + squad_skill[i]); } } return dp_skill[r + 1]; } long long ins() { long long a = 0; char c; bool minus = false; do { c = getchar_unlocked(); if (c == '-') { minus = true; } } while (c < '0' || c > '9'); do { a = 10LL * a + c - '0'; c = getchar_unlocked(); } while (c >= '0' && c <= '9'); return minus ? -a : a; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); n = ins(); k = ins(); q = ins(); for (int i = 1; i <= n; i++) { skill[i] = ins(); prefix_skill[i] = prefix_skill[i - 1] + skill[i]; } for (int i = 1; i <= n - k + 1; i++) { squad_skill[i] = prefix_skill[i + k - 1] - prefix_skill[i - 1]; } for (int querry = 0; querry < q; querry++) { l = ins(); r = ins(); cout << compute_result() << "\n"; } return 0; } |