def union_contiguous_intervals(arr):
    """ Union contiguous intervals.
    arr = list of [(st,ed),...]
    """
    arr = sorted(arr)
    def _gen():
        st0,ed0 = arr[0]
        for st1,ed1 in arr[1:]:
            if ed0 < st1: # ed1<st0 new interval
                yield [st0,ed0]
                st0,ed0 = st1,ed1
            else: # overlapping/contiguous
                ed0 = max(ed0, ed1)
        yield [st0,ed0]
    return [x for x in _gen()]