Exact hierarchical clustering in one dimension