You could create an array "counts" of size Q. This is for counting and is filled with zeros. Now you select arr[i] for i=1..n and do: For j=1 to Q counts[j] += arr[i] >= queries[j] ? 1 : 0 This has O(n) or more exaktly O(n * Q) but Q should be rather low so isn't that important. You could work with balanced trees maybe sorted after choclet count. That are my quick ideas to that.
Обсуждают сегодня