def union_intervals(intervals):
    """Size of the union of a set of intervals

    :param intervals: list or set of integer pairs (left, right)
    :returns: size of the union of those intervals
    :complexity: :math:`O(n \\log n)`
    """
    furthest = float('-inf')  # furthest interval endpoint seen so far
    length = 0                              # size of union
    for left, right in sorted(intervals):
        if right > furthest:                # union increases
            if left > furthest:             # interval disjoint from
                length += right - left      #   current union
            else:                           # interval intersects the
                length += right - furthest  #   current union
        furthest = right                    # update
    return length