- maxmmin
不用特意卡常吧
- @ 2026-8-27 18:24:39
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=9712176,inf=1e15;
int n,a[N],stk[N],sttop,retu[N],lmax[N],rmax[N],val[N],ANS,ltomin[N],rtomin[N];
int trq[N],trh[N],ctq[N],cth[N];
int lowbit(int x){
return x&-x;
}
void add(int * z,int x,int val){
while(x<=n){
z[x]+=val;
z[x]%=mod;
x+=lowbit(x);
}
}
int ask(int * z,int x){
int ans=0;
while(x){
ans+=z[x];
ans%=mod;
x-=lowbit(x);
}
return ans%mod;
}
signed main(){
freopen("maxmmin.in","r",stdin);
freopen("maxmmin.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
retu[a[i]]=i;
}
for(int i=n;i>=1;i--){
while(sttop&&stk[sttop]<a[i]){
lmax[retu[stk[sttop]]]=i+1;sttop--;
}
stk[++sttop]=a[i];
}
while(sttop){
lmax[retu[stk[sttop]]]=1;sttop--;
}
for(int i=1;i<=n;i++){
while(sttop&&stk[sttop]<a[i]){
rmax[retu[stk[sttop]]]=i-1;sttop--;
}
stk[++sttop]=a[i];
}
while(sttop){
rmax[retu[stk[sttop]]]=n;sttop--;
}
for(int i=n;i>=1;i--){
while(sttop&&stk[sttop]>a[i]){
ltomin[retu[stk[sttop]]]=i+1;sttop--;
}
stk[++sttop]=a[i];
}
while(sttop){
ltomin[retu[stk[sttop]]]=1;sttop--;
}
for(int i=1;i<=n;i++){
while(sttop&&stk[sttop]>a[i]){
rtomin[retu[stk[sttop]]]=i-1;sttop--;
}
stk[++sttop]=a[i];
}
while(sttop){
rtomin[retu[stk[sttop]]]=n;sttop--;
}
for(int i=1;i<=n;i++){
val[i]=(i-lmax[i]+1)*(rmax[i]-i+1)%mod;
}
for(int i=2;i<=n;i++){
add(trh,a[i],val[i]);
add(cth,a[i],val[i]*a[i]%mod);
}
for(int i=2;i<n;i++){
int tfri=(i-ltomin[i]+1)*(rtomin[i]-i+1)%mod;
add(trq,a[i-1],val[i-1]);
add(ctq,a[i-1],val[i-1]*a[i-1]%mod);
add(trh,a[i],-val[i]);
add(cth,a[i],-(val[i]*a[i]%mod));
int tsec=ask(trq,a[i])*a[i]%mod;
tsec=(tsec-ask(ctq,a[i])+mod)%mod;
int tthi=ask(trh,a[i])*a[i]%mod;
tthi=(tthi-ask(cth,a[i])+mod)%mod;
ANS=(ANS+tfri*tsec%mod*tthi%mod)%mod;
}
cout<<ANS;
return 0;
}
1 条评论
-
C2023YangYF LV 6 @ 2026-8-28 9:28:29
可是我的正解跑了 1987 ms(跟常规做法不同
@C2024ZhangJX
- 1
信息
- ID
- 17
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 96
- 已通过
- 16
- 上传者