#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=301000; int n,k,q,a[N],l[N],r[N]; ll s[N],val[N],ans[N],dp[N]; void solve(int l,int r,vector<int> id) { if (r-l<k) { return; } else { int mid=(l+r)>>1; VI ql,qr,qmid; for (auto x:id) { if (::r[x]<=mid) ql.pb(x); else if (::l[x]>=mid) qr.pb(x); else qmid.pb(x); } solve(l,mid,ql); solve(mid,r,qr); dp[mid]=0; for (int j=mid-1;j>=l;j--) { dp[j]=dp[j+1]; if (j+k<=mid) dp[j]=max(dp[j],dp[j+k]+val[j]); } for (int j=mid+1;j<=r;j++) { dp[j]=dp[j-1]; if (j-k>=mid) dp[j]=max(dp[j],dp[j-k]+val[j-k]); } for (auto y:qmid) ans[y]=max(ans[y],dp[::l[y]]+dp[::r[y]]); rep(i,l,r+1) dp[i]=0; for (int x=mid-1;x>mid-k;x--) { dp[x]=0; if (x-k+1>=l) dp[x-k+1]=0; for (int j=x-k;j>=l;j--) { dp[j]=max(dp[j+1],dp[j+k]+val[j]); } dp[x+k]=0; for (int j=x+k+k;j<=r;j++) { dp[j]=max(dp[j-1],dp[j-k]+val[j-k]); } for (auto y:qmid) { if (::l[y]<=x&&x+k<=::r[y]) ans[y]=max(ans[y],dp[::l[y]]+dp[::r[y]]+val[x]); } } } } const int T=2521,T2=300000/T+5; ll pd[T2][N]; int ty[N],nxt[N]; VI que[N]; int main() { scanf("%d%d%d",&n,&k,&q); rep(i,1,n+1) scanf("%d",a+i),s[i]=a[i]+s[i-1]; for (int i=0;i+k<=n;i++) { val[i]=max(s[i+k]-s[i],0ll); } VI v; rep(i,0,q) { scanf("%d%d",l+i,r+i); --l[i]; que[r[i]].pb(i); v.pb(i); } if (k<=T) { solve(0,n,v); } else { int B=100; for (int rem=0;rem<k;rem+=B) { int np=n+1; per(i,0,n+1) { if (i%k==rem) ty[i]=1; else if (i%k>rem&&i%k<rem+B) ty[i]=-1; else ty[i]=-2; nxt[i]=np; if (ty[i]!=-2) np=i; } int v=0; for (int x=rem;x<=n;x+=k) { pd[v][x]=0; per(j,0,x) { pd[v][j]=pd[v][j+1]; if (j+k<=x) pd[v][j]=max(pd[v][j],val[j]+pd[v][j+k]); } ty[x]=v; v++; } for (int z=rem;z<rem+B&&z<k;z++) { for (int y=z;y<=n;y+=k) { for (auto q:que[y]) { int vl=l[q],px=vl; if (ty[px]==-2) px=nxt[px]; while (px<=y) { if (ty[px]>=0) dp[px]=pd[ty[px]][vl]; else { if (px==vl) { dp[px]=0; } else { dp[px]=dp[px-1]; if (px-k>=vl) { dp[px]=max(dp[px],dp[px-k]+val[px-k]); } } } px=nxt[px]; } ans[q]=dp[y]; } } } } } rep(i,0,q) printf("%lld\n",ans[i]); }
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=301000; int n,k,q,a[N],l[N],r[N]; ll s[N],val[N],ans[N],dp[N]; void solve(int l,int r,vector<int> id) { if (r-l<k) { return; } else { int mid=(l+r)>>1; VI ql,qr,qmid; for (auto x:id) { if (::r[x]<=mid) ql.pb(x); else if (::l[x]>=mid) qr.pb(x); else qmid.pb(x); } solve(l,mid,ql); solve(mid,r,qr); dp[mid]=0; for (int j=mid-1;j>=l;j--) { dp[j]=dp[j+1]; if (j+k<=mid) dp[j]=max(dp[j],dp[j+k]+val[j]); } for (int j=mid+1;j<=r;j++) { dp[j]=dp[j-1]; if (j-k>=mid) dp[j]=max(dp[j],dp[j-k]+val[j-k]); } for (auto y:qmid) ans[y]=max(ans[y],dp[::l[y]]+dp[::r[y]]); rep(i,l,r+1) dp[i]=0; for (int x=mid-1;x>mid-k;x--) { dp[x]=0; if (x-k+1>=l) dp[x-k+1]=0; for (int j=x-k;j>=l;j--) { dp[j]=max(dp[j+1],dp[j+k]+val[j]); } dp[x+k]=0; for (int j=x+k+k;j<=r;j++) { dp[j]=max(dp[j-1],dp[j-k]+val[j-k]); } for (auto y:qmid) { if (::l[y]<=x&&x+k<=::r[y]) ans[y]=max(ans[y],dp[::l[y]]+dp[::r[y]]+val[x]); } } } } const int T=2521,T2=300000/T+5; ll pd[T2][N]; int ty[N],nxt[N]; VI que[N]; int main() { scanf("%d%d%d",&n,&k,&q); rep(i,1,n+1) scanf("%d",a+i),s[i]=a[i]+s[i-1]; for (int i=0;i+k<=n;i++) { val[i]=max(s[i+k]-s[i],0ll); } VI v; rep(i,0,q) { scanf("%d%d",l+i,r+i); --l[i]; que[r[i]].pb(i); v.pb(i); } if (k<=T) { solve(0,n,v); } else { int B=100; for (int rem=0;rem<k;rem+=B) { int np=n+1; per(i,0,n+1) { if (i%k==rem) ty[i]=1; else if (i%k>rem&&i%k<rem+B) ty[i]=-1; else ty[i]=-2; nxt[i]=np; if (ty[i]!=-2) np=i; } int v=0; for (int x=rem;x<=n;x+=k) { pd[v][x]=0; per(j,0,x) { pd[v][j]=pd[v][j+1]; if (j+k<=x) pd[v][j]=max(pd[v][j],val[j]+pd[v][j+k]); } ty[x]=v; v++; } for (int z=rem;z<rem+B&&z<k;z++) { for (int y=z;y<=n;y+=k) { for (auto q:que[y]) { int vl=l[q],px=vl; if (ty[px]==-2) px=nxt[px]; while (px<=y) { if (ty[px]>=0) dp[px]=pd[ty[px]][vl]; else { if (px==vl) { dp[px]=0; } else { dp[px]=dp[px-1]; if (px-k>=vl) { dp[px]=max(dp[px],dp[px-k]+val[px-k]); } } } px=nxt[px]; } ans[q]=dp[y]; } } } } } rep(i,0,q) printf("%lld\n",ans[i]); } |