可用于将序列问题转换为树上问题,以简化解题思路。
笛卡尔树
+ tarjan-lca
可实现线性时间复杂度的 RMQ 算法。
struct Node
{
int L, R;
}tr[N];
int build(int n) //返回树根
{
stack<int> stk;
int root = 0;
for (int i = 1; i <= n; i++)
{
int last = 0;
while (!stk.empty() && a[stk.top()] > a[i])
{
last = stk.top();
stk.pop();
}
if (!stk.empty()) tr[stk.top()].R = i;
else root = i;
tr[i].L = last;
stk.push(i);
}
return root;
}