from typing import List
from typing import Tuple


def tau_decompose(taus: List[int]) -> Tuple[List[int], List[int], List[int], List[int]]:
    """
    From the list of tau_l's, calculate beggining and ending indices of each
    feature vector.

    Parameters
    ----------
    taus : 1-D array of integers
        [tau_0, tau_1, ..., tau_L]

    Returns
    -------
    mlos / mhis : list of integers
        [description]
    lbegin / lend : list of integers
        The result shows that the feature vectors with dim=l is in the range
        of [lbegin[l], lend[l]) for each valid l.

    Examples
    --------
    >>> tau = [1, 2]
    >>> tau_decompose(tau)
    ([0, 1, 4], [1, 4, 7], [0, 1], [1, 7])

    tau = [1, 2] means that there exists one l=0 feature vector and two l=1
    feature vectors. The total dimension is 1 * 0 + 2 * 3 = 7.
    mlos and mhis show that the l=0 vector is in [0, 1), and two l=1 vectors
    belong to [1, 4) and [4, 7), respectively.

    """
    mlos = []
    mhis = []
    l_begin = []
    l_end = []
    for l, t in enumerate(taus):
        l_begin.append(0 if not mhis else mhis[-1])
        for _ in range(t):
            mlos.append(0 if not mhis else mhis[-1])
            mhis.append(mlos[-1] + 2 * l + 1)
        l_end.append(0 if not mhis else mhis[-1])
    return mlos, mhis, l_begin, l_end