CodeForces - 1208E
CodeForces - 1208E
经典滑动窗口问题,如果以物体为参考系的话。
前缀和优化区间操作
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2020.8.4 using namespace std; long long n,m,len,l,r,k,tmp; long long a[N],q[N],L[N],R[N],sum[N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } signed main(){ in(n);in(m); For(i,1,n){ in(len); For(j,1,len) in(a[j]); L[0]=-1e10; For(j,1,len) L[j]=max(L[j-1],a[j]); R[len+1]=-1e10; for(long long j=len;j;j--) R[j]=max(R[j+1],a[j]); k=m-len; if(k>=len-1){ l=len;r=m-len+1; if(l!=r) L[len]=max(L[len],(long long)0); sum[l]+=L[len]; sum[r+1]-=L[len]; For(j,1,l-1){ sum[j]+=max(L[j],(long long)0); sum[j+1]-=max(L[j],(long long)0); } For(j,r+1,m){ sum[j]+=max(R[j-r+1],(long long)0); sum[j+1]-=max(R[j-r+1],(long long)0); } }else{ l=1;r=0; For(j,1,len){ while(l<=r && q[r]-q[l]>=k) q[l++]=0; while(l<=r && a[j]>=a[q[r]]) r--; q[++r]=j; tmp=a[q[l]]; if(j<=k) tmp=max(tmp,(long long)0); sum[j]+=tmp; sum[j+1]-=tmp; } For(j,l,r) q[j]=0; For(j,len+1,m){ sum[j]+=max(R[j+len-m],(long long)0); sum[j+1]-=max(R[j+len-m],(long long)0); } } } For(i,1,m){ sum[i]+=sum[i-1]; o(sum[i]);p(' '); } return 0; }