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.
Please format the code you posted, by wrapping it in triple backticks. -> `
Обсуждают сегодня