Files
python/TangDou/LuoGuBook/P2866_3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
531 B
C++

#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;
}