跳转至

单调栈

模版
C++
// 求 i 后面一个比 a[i] 大的数的下标
stack<int> st;
vector<int> ans(n + 1);
for (int i = 1 ; i <= n ; i++) {
    while (!st.empty() && a[st.top()] < a[i]) {
        ans[st.top()] = i;
        st.pop();
    }
    st.emplace(i);
}
例题