Sandomhet

Cartesian Tree

It is uniquely defined as a min-heap whose symmetric (in-order) traversal returns the original sequence.

Thu, Feb 1, 2024 1 min read

Cartesian Tree

A BST on the index and a (min-)heap on the value pip_i. Built in O(n)O(n) with a monotonic stack holding the right chain: pop every node larger than pip_i; the last popped node becomes the left child of ii, and ii becomes the right child of the new stack top. The root is s[1].

Build

构建

//笛卡尔树
void build() {
    for (int i = 1, top = 0, pos = 0; i <= n; ++i) {
        top = pos;//pos为当前栈顶,top为操作前栈顶
        while (pos && p[s[pos]] > p[i]) pos--;//单调栈
        if (pos) r[s[pos]] = i;//找到一个比自己小的点,接到右儿子上
        if (pos < top) l[i] = s[pos + 1];//把中间比自己大的点作为左儿子
        s[++pos] = i;
    }
}
Search

Loading search…