#include <stdio.h> #include <algorithm> #define forn(i,p,k) for(int i=(p);i<=(k);++i) const int MM = 300013; long long T[MM]; long long dp[MM]; void read(int &x) { char c = '*'; bool f = 0; while(c < '0' || c > '9') {c=getchar(); f |= (c == '-');} for(x = c - '0', c = getchar(); '0'<=c && c<='9'; c=getchar()) x = (x<<3) + (x<<1) + c - '0'; x = f ? -x : x; } int main() { int n,k,q,l,r; read(n); read(k); read(q); forn(i,1,n) { read(l); T[i] = T[i-1] + l; } while(q--) { read(l); read(r); if(r-l+1 < k) { puts("0"); continue; } forn(i,l-1,l+k-2) dp[i] = 0; forn(i,l+k-1,r) dp[i] = std::max(T[i] - T[i-k] + dp[i-k], dp[i-1]); printf("%lld\n", dp[r]); } }
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 | #include <stdio.h> #include <algorithm> #define forn(i,p,k) for(int i=(p);i<=(k);++i) const int MM = 300013; long long T[MM]; long long dp[MM]; void read(int &x) { char c = '*'; bool f = 0; while(c < '0' || c > '9') {c=getchar(); f |= (c == '-');} for(x = c - '0', c = getchar(); '0'<=c && c<='9'; c=getchar()) x = (x<<3) + (x<<1) + c - '0'; x = f ? -x : x; } int main() { int n,k,q,l,r; read(n); read(k); read(q); forn(i,1,n) { read(l); T[i] = T[i-1] + l; } while(q--) { read(l); read(r); if(r-l+1 < k) { puts("0"); continue; } forn(i,l-1,l+k-2) dp[i] = 0; forn(i,l+k-1,r) dp[i] = std::max(T[i] - T[i-k] + dp[i-k], dp[i-1]); printf("%lld\n", dp[r]); } } |