Template Summary - Cartesian tree

Cartesian tree

O (n) establishing a node of the binary tree is the most current value subtree.

 

Code:

void Build(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), L[i] = R[i] = 0;
    top = 0;
    for(int i = 1; i <= n; ++i){
        while(top && a[sta[top]] > a[i]) L[i] = sta[top--];
        if(top)  R[sta[top]] = i;
        sta[++top] = i;
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/MingSD/p/11260795.html