Files
teamnote/2025fall/source/Technic/CartesianTree.cpp
2026-06-03 09:36:52 +09:00

13 lines
282 B
C++

int A[N], par[N];
stack<int> st;
forr(i, n) {
int l = -1;
while(!st.empty() && A[st.top()] < A[i])
l = st.top(), st.pop();
if(l != -1) par[l] = i;
if(!st.empty()) par[i] = st.top();
st.push(i);
}
while(st.size() != 1) st.pop();
par[st.top()] = st.top();