lca (st, lm, rm) a b =
    if al <= bl && br <= ar
        then a
        else
            if bl <= al && ar <= br
                then b
                else
                    if al < bl
                        then snd $ lcast st ar bl
                        else snd $ lcast st br al
    where
        al = lm ! a
        ar = rm ! a
        bl = lm ! b
        br = rm ! b

-- PTree
lca (st, lm, rm) a b =
    if al <= bl && br <= ar
        then a
        else
            if bl <= al && ar <= br
                then b
                else
                    if al < bl
                        then snd $ lcast st ar bl
                        else snd $ lcast st br al
    where
        al = lm ! a
        ar = rm ! a
        bl = lm ! b
        br = rm ! b

-- PTree
lca (st, lm, rm) a b =
    if al <= bl && br <= ar
        then a
        else
            if bl <= al && ar <= br
                then b
                else
                    if al < bl
                        then snd $ lcast st ar bl
                        else snd $ lcast st br al
    where
        al = lm ! a
        ar = rm ! a
        bl = lm ! b
        br = rm ! b

-- PTree