#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 300005; int n, k, q, i, a, b, tab[N]; LL sum, res[N], maxx; int main() { scanf("%d %d %d", &n, &k, &q); for (i=1;i<=n;i++) scanf("%d", &tab[i]); while (q--) { scanf("%d %d", &a, &b); if (b - a + 1 < k) { printf("0\n"); continue; } sum = 0; for (i=a;i<a + k;i++) { sum += tab[i]; res[i] = 0; } maxx = res[a + k - 1] = max(0LL, sum); for (i=a+k;i<=b;i++) { sum += tab[i]; sum -= tab[i - k]; res[i] = max(res[i - 1], res[i - k] + sum); maxx = max(maxx, res[i]); } printf("%lld\n", maxx); } 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef pair <ii,int> iii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 300005; int n, k, q, i, a, b, tab[N]; LL sum, res[N], maxx; int main() { scanf("%d %d %d", &n, &k, &q); for (i=1;i<=n;i++) scanf("%d", &tab[i]); while (q--) { scanf("%d %d", &a, &b); if (b - a + 1 < k) { printf("0\n"); continue; } sum = 0; for (i=a;i<a + k;i++) { sum += tab[i]; res[i] = 0; } maxx = res[a + k - 1] = max(0LL, sum); for (i=a+k;i<=b;i++) { sum += tab[i]; sum -= tab[i - k]; res[i] = max(res[i - 1], res[i - k] + sum); maxx = max(maxx, res[i]); } printf("%lld\n", maxx); } return 0; } |