You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
531 B

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=80010;
int a[N];
int res[N];
stack<int> stk;
int n;
LL ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
while (!stk.empty() && a[stk.top()] <= a[i]) {
res[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for (int i = 1; i <= n; i++) {
if (res[i] == 0) ans += n - i;
else ans += res[i] - i - 1;
}
cout << ans;
}