Optimal histograms for hierarchical range queries