#include <cstdio> #define MAX_N 300005 long long a[MAX_N]; long long sumy[MAX_N]; long long dp[MAX_N]; long long fmax(long long a, long long b) { return a > b ? a : b; } int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (int i = 1; i <= k; i++) { sumy[1] += a[i]; } for (int i = 2; i <= n - k + 1; i++) { sumy[i] = sumy[i-1] + a[i+k-1] - a[i-1]; } for (int i = 0; i < q; i++) { int l, r; scanf("%d %d", &l, &r); dp[l-1] = 0; if (r - l + 1 < k) { printf("0\n"); } else { for (int i = l; i <= r - k + 1; i++) { if (i < l + k) { dp[i] = fmax(dp[i-1], sumy[i]); } else { dp[i] = fmax(dp[i-k] + sumy[i], dp[i-1]); } } printf("%lld\n", dp[r-k+1]); } } 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 | #include <cstdio> #define MAX_N 300005 long long a[MAX_N]; long long sumy[MAX_N]; long long dp[MAX_N]; long long fmax(long long a, long long b) { return a > b ? a : b; } int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (int i = 1; i <= k; i++) { sumy[1] += a[i]; } for (int i = 2; i <= n - k + 1; i++) { sumy[i] = sumy[i-1] + a[i+k-1] - a[i-1]; } for (int i = 0; i < q; i++) { int l, r; scanf("%d %d", &l, &r); dp[l-1] = 0; if (r - l + 1 < k) { printf("0\n"); } else { for (int i = l; i <= r - k + 1; i++) { if (i < l + k) { dp[i] = fmax(dp[i-1], sumy[i]); } else { dp[i] = fmax(dp[i-k] + sumy[i], dp[i-1]); } } printf("%lld\n", dp[r-k+1]); } } return 0; } |