#include <iostream> #include <algorithm> #include <stdio.h> #include <set> #include <vector> #include <map> #include <tuple> #include <deque> #include <string.h> using namespace std; #define LL long long LL a[300100]; LL l,r; LL z[300100]; LL p[300100]; LL n,k; #define Z -10000000000LL LL policz() { for(LL i = l;i<=r;i++) p[i]=0; if(l+k-1 <= r) p[l+k-1] = max(z[l+k-1],0LL); for(LL i=l+k;i<=r;i++) { if(i-k >= l) { if(p[i-k] + z[i] > p[i-1]) p[i] = p[i-k] + z[i]; else p[i] = p[i-1]; } } // for(LL i=0;i<n;i++) printf("%lld, ",p[i]); //printf("\n"); return p[r]; } int main() { LL q; scanf("%lld%lld%lld",&n,&k,&q); for(LL i=0;i<n;i++) scanf("%lld",a+i); LL s=0; for(LL i=0;i<k;i++) s+=a[i]; for(LL i=k-1;i<n;i++) {z[i]=s;s+=a[i+1]-a[i-k+1];}; //for(LL i=0;i<n;i++) printf("%lld, ",z[i]); //printf("\n"); while(q--) { scanf("%lld%lld",&l,&r); l--;r--; printf("%lld\n",policz()); } 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 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <iostream> #include <algorithm> #include <stdio.h> #include <set> #include <vector> #include <map> #include <tuple> #include <deque> #include <string.h> using namespace std; #define LL long long LL a[300100]; LL l,r; LL z[300100]; LL p[300100]; LL n,k; #define Z -10000000000LL LL policz() { for(LL i = l;i<=r;i++) p[i]=0; if(l+k-1 <= r) p[l+k-1] = max(z[l+k-1],0LL); for(LL i=l+k;i<=r;i++) { if(i-k >= l) { if(p[i-k] + z[i] > p[i-1]) p[i] = p[i-k] + z[i]; else p[i] = p[i-1]; } } // for(LL i=0;i<n;i++) printf("%lld, ",p[i]); //printf("\n"); return p[r]; } int main() { LL q; scanf("%lld%lld%lld",&n,&k,&q); for(LL i=0;i<n;i++) scanf("%lld",a+i); LL s=0; for(LL i=0;i<k;i++) s+=a[i]; for(LL i=k-1;i<n;i++) {z[i]=s;s+=a[i+1]-a[i-k+1];}; //for(LL i=0;i<n;i++) printf("%lld, ",z[i]); //printf("\n"); while(q--) { scanf("%lld%lld",&l,&r); l--;r--; printf("%lld\n",policz()); } return 0; } |