The meaning of problems
https://codeforces.com/contest/1037/problem/F
Think
Excerpt from a more interesting approach. We divide and conquer sequence, each statistical contribution across the mid section of. Which is to ensure the accuracy: only each interval corresponds to a mid. For the left interval, calculate its maximum extension.
Next, from left to right and right to enumerate every point range. For point $ i $, the right end of the span section above it is a $ k-1 $. Therefore, in addition to the right of the maximum value of the same left block intervals, the remaining prefix contribution to span (maximum extension) to (k-1) determined by the left and interval.
Code is small, but a lot of details. Although excellent algorithm complexity is $ O (nlogn) $, but after all, to solve this kind of problem.
Code
1 #include<bits/stdc++.h> 2 #define mod 1000000007 3 using namespace std; 4 typedef long long int ll; 5 const int maxn=1E6+5; 6 int n,k; 7 ll ans,a[maxn],maxx[maxn],sum[maxn]; 8 inline ll max(ll x,ll y) 9 { 10 return x>y?x:y; 11 } 12 void solve(int l,int r) 13 { 14 if(r-l+1<k) 15 return; 16 int mid=(l+r)>>1; 17 maxx[mid]=a[mid]; 18 for(int i=mid-1;i>=l;--i) 19 maxx[i]=max(maxx[i+1],a[i]); 20 sum[l-1]=0; 21 sum[l]=maxx[l]; 22 for(int i=l+1;i<=mid;++i) 23 { 24 sum[i]=0; 25 int pos=i-k+1; 26 if(pos>=l) 27 sum[i]=sum[pos]; 28 sum[i]=(sum[i]+maxx[i])%mod; 29 } 30 ll now=a[mid+1]; 31 for(int i=mid+1,j=mid;i<=r;++i,now=max(now,a[i])) 32 { 33 if(i-l+1<k) 34 continue; 35 while(j>=l&&maxx[j]<now) 36 --j; 37 int len=i-j+1; 38 int block=(len-2)/(k-1); 39 int out=block+1; 40 block-=(i-mid-1)/(k-1); 41 ans=(ans+block*now%mod+sum[max(l-1,i-out*(k-1))])%mod; 42 } 43 solve(l,mid),solve(mid+1,r); 44 } 45 int main() 46 { 47 ios::sync_with_stdio(false); 48 cin>>n>>k; 49 for(int i=1;i<=n;++i) 50 cin>>a[i]; 51 solve(1,n); 52 cout<<ans<<endl; 53 return 0; 54 }