This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[300010],t[300010][2],st[300010],p;
long long b[2][300010],ans[300010];
void trav(int n,int s,int e){
int i=s,j=e,l=t[n][0],r=t[n][1];
long long sum=a[n];
if(s==e)ans[n]+=sum,ans[n+1]-=sum;
if(s>=e)return;
while(l||r){
if(a[l]<a[r])sum+=b[j%2][l]-b[j%2][i-1],i=l+1,l=t[l][1];
else sum+=b[i%2][j]-b[i%2][r-1],j=r-1,r=t[r][0];
}
ans[n]+=sum,ans[n+1]-=sum;
sum=b[s%2][e]-b[s%2][n-1];
ans[s]+=sum,ans[n]-=sum;
sum=b[e%2][n]-b[e%2][s-1];
ans[n+1]+=sum,ans[e+1]-=sum;
if(t[n][0])trav(t[n][0],s,n-1);
if(t[n][1])trav(t[n][1],n+1,e);
}
int main(){
int i,n,mn=2e9,mi,l=1,r,chk=0;
long long s=0;
scanf("%d",&n);
for(i=1;i<=n;++i){
scanf("%d",a+i);
if(mn>a[i])mn=a[i],mi=i;
}
rotate(a+1,a+mi%n+1,a+n+1);
r=n-1;
while(l<=r){
if(a[l]<a[r])s+=chk*a[r--];
else s+=chk*a[l++];
chk=!chk;
}
ans[n]=s;
b[1][1]=b[1][2]=a[1];
for(i=2;i<=n;++i)b[i%2][i+1]=b[i%2][i]=b[i%2][i-2]+a[i];
for(i=1;i<n;++i){
while(p&&a[st[p]]>a[i])--p;
t[i][0]=t[st[p]][1],t[st[p]][1]=i,st[++p]=i;
}
a[0]=2e9;
trav(st[1],1,n-1);
s=0;
if(n%2)ans[1]+=a[n];
for(i=2;i<=n;++i)ans[i]+=ans[i-1];
if(n%2==0)ans[n]+=a[n];
rotate(ans+1,ans+n-mi+1,ans+n+1);
for(i=1;i<=n;++i)printf("%lld\n",ans[i]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |