#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb push_back
#define int long long
const int MAX = 300000+3,M=(1<<25)+1,mod=1e9+7;
void maxx(int &a,int b) {if(b>a) a=b;}
void minn(int &a,int b) {if(b<a) a=b;}
using ii = pair < int ,int > ;
using ll = long long;
int a[MAX],B[MAX][2];
int ST[MAX][20];
int minu(int i,int j){
if(a[i]<a[j]) return i;
else return j;
}
int n;
int LG[MAX];
int id(int u){
return (u+n-1)%n+1;
}
int get(int l,int r){
int u=r-l+1;
if(r<l)u+=n;
u=LG[u];
return minu(ST[l][u],ST[id(r-(1<<u)+1)][u]);
}
bool tt;
int getsum(bool x,int l,int r){
if(r>=l) return B[r][x]-B[l-1][x];
else return B[n][x]-B[l-1][x]+B[r][x^tt];
}
int getsum2(bool x,int l,int r){
if(r>=l) return B[r][x]-B[l-1][x];
else return B[n][x^tt]-B[l-1][x^tt]+B[r][x];
}
int ans[MAX];
int H;
int getdown(int u,int val){
//if(a[u]<val) return u;
for(int z=H;z>=0;z--)
if(a[ ST[id(u-(1<<z))][z] ]>=val ) {
u=id(u-(1<<z));
}
return u;
}
int getup(int u,int val){
//if(a[u]<val) return u;
for(int z=H;z>=0;z--)
if(a[ ST[id(u+1)][z] ]>=val ) {
u=id(u+(1<<z));
}
return u;
}
int dis(int L,int R){
if(R>=L) return R-L+1;
else return R-L+1+n;
}
int cos(int z,int L,int R){
int res=a[z];
if(dis(L,z)<dis(z,R)){
int i=id(z-1),j=id(z+1);
bool ok=1;
while(i!=L){
if(a[j]<a[i]) {if(!ok) res+=a[i];i=id(i-1);ok^=1;continue;}
int m=getup(j,a[i]);
res+=getsum((ok^j)&1,j,m);
//cout<<j<<' '<<m<<' '<<((ok^j)&1)<<' '<<getsum((ok^j)&1,j,m)<<'\n';
ok^=dis(j,m)&1;
j=id(m+1);
}
while(j!=R){
if(!ok) res+=a[j];
ok^=1;
j=id(j+1);
}
}
else{
//cout<<res<<'\n';
int i=id(z-1),j=id(z+1);
bool ok=1;
while(j!=R){
if(a[i]<a[j]) {if(!ok) res+=a[j];j=id(j+1);ok^=1;continue;}
int m=getdown(i,a[j]);
res+=getsum2((ok^i)&1,m,i);
ok^=dis(m,i)&1;
i=id(m-1);
}
while(i!=L){
if(!ok) res+=a[i];
ok^=1;
i=id(i-1);
}
}
//cout<<res;
return res;
}
void GODOWN(int L,int R,int sum){
//cout<<sum<<'\n';
if(R==id(L+1))return;
int z=get(id(L+1),id(R-1));
//cout<<z<<' '<<L<<' '<<R<<' '<<sum<<'\n';
ans[z]=cos(z,L,R)+sum;
//return;
GODOWN(L,z,sum+getsum( (z^tt^((L-z+1+n)%n))&1,z,id(R-1) ));
GODOWN(z,R,sum+getsum2( (z^tt^((z-R+1+n)%n))&1,id(L+1),z ));
}
signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("A.inp", "r", stdin);
//freopen("A.out", "w", stdout);
LG[1]=0;
for(int i=2;i<MAX;i++) LG[i]=LG[i/2]+1;
cin>>n;
H=LG[n];
for(int i=1;i<=n;i++){
cin>>a[i];
ST[i][0]=i;
B[i][0]=B[i-1][0];
B[i][1]=B[i-1][1];
B[i][i%2]+=a[i];
}
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
ST[i][j]=minu(ST[i][j-1],ST[(i+(1<<j-1)-1)%n+1][j-1]);
}
}
int u1=ST[1][19];
int u2=get(id(u1+1),id(u1-1));
int &res = ans[u1];
//cout<<u1<<' '<<u2<<'\n';
tt=n&1;
//res=a[u1];
if(tt) res+=a[u2];
//cout<<res<<'\n';
res+=cos(u1,u2,u2);
//cout<<res;
//return 0;
if(tt) GODOWN(u1,u1,a[u1]);
else GODOWN(u1,u1,0);
//GODOWN(u1,u2,getsum( (u2^tt^((u1-u2+1+n)%n))&1,u2,id(u1-1)));
//GODOWN(u2,u1,getsum( (u2^tt^((u2-u1+1+n)%n))&1,id(u1+1),u2));
for(int i=1;i<=n;i++) cout<<ans[i]<<'\n';
}
Compilation message
cake.cpp: In function 'int main()':
cake.cpp:132:49: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
132 | ST[i][j]=minu(ST[i][j-1],ST[(i+(1<<j-1)-1)%n+1][j-1]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3820 KB |
Output is correct |
2 |
Correct |
72 ms |
3820 KB |
Output is correct |
3 |
Correct |
19 ms |
3820 KB |
Output is correct |
4 |
Correct |
7 ms |
3820 KB |
Output is correct |
5 |
Correct |
5 ms |
4076 KB |
Output is correct |
6 |
Correct |
5 ms |
3820 KB |
Output is correct |
7 |
Correct |
8 ms |
3820 KB |
Output is correct |
8 |
Correct |
14 ms |
3884 KB |
Output is correct |
9 |
Correct |
2 ms |
2692 KB |
Output is correct |
10 |
Correct |
3 ms |
2672 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
702 ms |
24212 KB |
Output is correct |
2 |
Correct |
100 ms |
24144 KB |
Output is correct |
3 |
Correct |
744 ms |
24280 KB |
Output is correct |
4 |
Correct |
251 ms |
52828 KB |
Output is correct |
5 |
Correct |
248 ms |
45292 KB |
Output is correct |
6 |
Correct |
393 ms |
66636 KB |
Output is correct |
7 |
Execution timed out |
1567 ms |
59964 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |