Data Structures19 sections · 729 units
Open in Course

Problem - Count in Range

Count values in bounds

Given array AA, answer: "How many elements in A[l..r]A[l..r] have value in [a,b][a, b]?" Example: A=[1,5,2,6,3,7,4]A = [1, 5, 2, 6, 3, 7, 4] Query (1,5,2,5)(1, 5, 2, 5): elements in [5,2,6,3][5, 2, 6, 3] with value in [2,5][2, 5] are 5,2,35, 2, 3 = count 33.

Use the same persistent segment tree structure. Instead of finding kk-th, count elements in a value range.