#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k, q; cin >> n >> k >> q; vector<ll> a(n + 1); REP(i, n) { cin >> a[i + 1]; a[i + 1] += a[i]; } vector<ll> f(n + 1); REP(i, q) { int l, r; cin >> l >> r; if (r - l + 1 < k) { cout << 0 << '\n'; continue; } f[l - 1] = 0; for (int i = l; i <= r; ++i) { f[i] = f[i - 1]; if (i - k + 1 >= l) { f[i] = max(f[i], f[i - k] + a[i] - a[i - k]); } } cout << f[r] << '\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 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr int kMod = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k, q; cin >> n >> k >> q; vector<ll> a(n + 1); REP(i, n) { cin >> a[i + 1]; a[i + 1] += a[i]; } vector<ll> f(n + 1); REP(i, q) { int l, r; cin >> l >> r; if (r - l + 1 < k) { cout << 0 << '\n'; continue; } f[l - 1] = 0; for (int i = l; i <= r; ++i) { f[i] = f[i - 1]; if (i - k + 1 >= l) { f[i] = max(f[i], f[i - k] + a[i] - a[i - k]); } } cout << f[r] << '\n'; } } |