题目描述
题解
单调栈裸题,自底向上单调递减。
代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
const int max_n=8e4+5;
int n,x,temp,a[max_n],strack[max_n];
LL ans;
struct hp{int r;}f[max_n];
int main(){
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%d",&a[i]);
for (int i=1;i<=n;++i) f[i].r=i;
for (int i=1;i<=n;++i){
x=a[i];
while (temp&&a[strack[temp]]<=x){
f[strack[temp-1]].r=f[strack[temp]].r;
temp--;
}
f[strack[temp]].r=f[i].r;
strack[++temp]=i;
}
while (temp){
f[strack[temp-1]].r=f[strack[temp]].r;
temp--;
}
for (int i=1;i<=n;++i)
ans+=(LL)f[i].r-i;
printf("%lld\n",ans);
}