跳到正文

个人记录

77

wanghaoran000约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月27日
更新
2026年2月28日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node{
    int x,y;
};
stack<node> st;
ll a[3000005],ans;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        while(!st.empty()&&st.top().y<=a[i]){
            ans+=i-st.top().x+1;
            st.pop();
        }
        if(!st.empty()){
            ans+=i-st.top().x+1;
        }
        st.push(node{i,a[i]});
    }
    cout<<ans;
    return 0;
}
原文