buildrmq acc cs lvs x = acc'
    where
        updacc (lstseq, lstmap, ix) = (lstseq', lstmap', succ ix)
            where
                lstseq' = (ix, ((lvs ! x), x)) : lstseq
                lstmap' = (x, ix) : lstmap
        f acc c = updacc acc'
            where
                acc' = buildrmq acc cs lvs c
        acc' = foldl f (updacc acc) (cs ! x)
buildrmq acc cs lvs x = acc'
    where
        updacc (lstseq, lstmap, ix) = (lstseq', lstmap', succ ix)
            where
                lstseq' = (ix, ((lvs ! x), x)) : lstseq
                lstmap' = (x, ix) : lstmap
        f acc c = updacc acc'
            where
                acc' = buildrmq acc cs lvs c
        acc' = foldl f (updacc acc) (cs ! x)
buildrmq acc cs lvs x = acc'
    where
        updacc (lstseq, lstmap, ix) = (lstseq', lstmap', succ ix)
            where
                lstseq' = (ix, ((lvs ! x), x)) : lstseq
                lstmap' = (x, ix) : lstmap
        f acc c = updacc acc'
            where
                acc' = buildrmq acc cs lvs c
        acc' = foldl f (updacc acc) (cs ! x)