#include <iostream> #include <numeric> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin >> n >> k >> q; vector<ll> V(n), P(n + 1); for (auto& v : V) cin >> v; partial_sum(V.begin(), V.end(), P.begin() + 1); vector<ll> O(n - k + 1); for (int i = 0; i < O.size(); i++) O[i] = max(0LL, P[k + i] - P[i]); while (q--) { int l, r; cin >> l >> r; int size = r - l + 1; if (size < k) cout << 0 << '\n'; else { vector<ll> D(size); D[k - 1] = O[l - 1]; for (int i = k; i < size; i++) D[i] = max(D[i - 1], O[l + i - k] + D[i - k]); cout << D.back() << '\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 | #include <iostream> #include <numeric> #include <vector> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, q; cin >> n >> k >> q; vector<ll> V(n), P(n + 1); for (auto& v : V) cin >> v; partial_sum(V.begin(), V.end(), P.begin() + 1); vector<ll> O(n - k + 1); for (int i = 0; i < O.size(); i++) O[i] = max(0LL, P[k + i] - P[i]); while (q--) { int l, r; cin >> l >> r; int size = r - l + 1; if (size < k) cout << 0 << '\n'; else { vector<ll> D(size); D[k - 1] = O[l - 1]; for (int i = k; i < size; i++) D[i] = max(D[i - 1], O[l + i - k] + D[i - k]); cout << D.back() << '\n'; } } } |