提交时间:2024-08-21 16:54:15

运行 ID: 180640

#include<iostream> using namespace std; typedef long long lld; int a[500100],s[500100],top; lld ans; void Calc(int x){ int l=0,r=top,p=0,mid; while(l <= r){ mid = l+r>>1; if(a[s[mid]] > x){ p = mid; l = mid+1; }else{ r = mid-1; } } !p ? ans+=top : ans+=top-p+1; } int main(){ int n; cin>>n; for(int i = 1; i <= n; i++){ cin>>a[i]; } for(int i = 1; i <= n; i++){ Calc(a[i]); while(top>0 && a[i]>a[s[top]]){ --top; } s[++top] = i; } cout<<ans; return 0; }