#include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; int a[300000]; LL s[300001], res1[300001]; const int N = 11000; LL cache[N + 1][N + 1]; int c[N + 1]; int main() { INT(n); INT(k); INT(q); REP(i,n) scanf("%d", &a[i]); s[0] = 0; REP(i,n) s[i + 1] = s[i] + a[i]; if (n <= N) REP(i,n+1) c[i] = 0; REP(qq,q) { INT(l); INT(r); --l; LL *res = 0; int cc = 0; if (n <= N) { res = cache[l]; cc = c[l]; } else res = res1; if (!cc) { res[0] = 0; ++cc; } FOR(i,cc,r-l+1) { res[i] = res[i - 1]; if (i >= k) res[i] = max(res[i], res[i - k] + s[i + l] - s[i - k + l]); } if (n <= N) c[l] = r - l + 1; printf("%lld\n", res[r - l]); } }
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 | #include <algorithm> #include <cstdio> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define INT(x) int x; scanf("%d", &x) typedef long long LL; int a[300000]; LL s[300001], res1[300001]; const int N = 11000; LL cache[N + 1][N + 1]; int c[N + 1]; int main() { INT(n); INT(k); INT(q); REP(i,n) scanf("%d", &a[i]); s[0] = 0; REP(i,n) s[i + 1] = s[i] + a[i]; if (n <= N) REP(i,n+1) c[i] = 0; REP(qq,q) { INT(l); INT(r); --l; LL *res = 0; int cc = 0; if (n <= N) { res = cache[l]; cc = c[l]; } else res = res1; if (!cc) { res[0] = 0; ++cc; } FOR(i,cc,r-l+1) { res[i] = res[i - 1]; if (i >= k) res[i] = max(res[i], res[i - k] + s[i + l] - s[i - k + l]); } if (n <= N) c[l] = r - l + 1; printf("%lld\n", res[r - l]); } } |