https://blog.csdn.net/zuzhiang/article/details/78134247
http://poj.org/problem?id=3250
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
const ll inf=1e18;
ll read()
{
char ch=' ';
ll f=1;ll x=0;
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';ch=getchar();
}
return x*f;
}
const ll N=80100;
ll z[N];
stack<ll> s;
ll ans=0;
int main()
{
ll n;
n=read();
ll i,j;
for(i=1;i<=n;i++)
{
z[i]=read();
}
z[n+1]=inf;
for(i=1;i<=n+1;i++)
{
while(!s.empty()&&z[i]>=z[s.top()])
{
ans+=i-s.top()-1;
s.pop();
}
s.push(i);
}
cout<<ans<<endl;
return 0;
}