单调栈

#include <iostream>
#include <bits/stdc++.h>
#include <stack>
using namespace std;

int main() {
    int n;
    cin>> n;
    vector<int> h(n);
    for(int i=0;i<n;i++){
        cin>> h[i];
    }
    stack<int> stk; // 单调减,保存当前能炸到i的点的个数
    vector<int> res(n);
    for(int i=0;i<n;i++){
        while(!stk.empty()&& h[i]>stk.top()){
            stk.pop();
        }
        res[i] += i - stk.size();
        stk.push(h[i]);
    }
    stk = stack<int>();
    for(int i=n-1;i>=0;i--){
        while(!stk.empty()&&h[i]>stk.top()){
            stk.pop();
        }
        res[i] += (n-1 -i) - stk.size();
        stk.push(h[i]);
    }
    for(int i=0;i<n;i++){
        cout << res[i] << " ";
    }
}
// 64 位输出请用 printf("%lld")