Added a program fo Xsum all K Lonf Subarrays #233
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To maintain the x-sum for every sliding window, we use two ordered sets: large (which always stores the top-x most frequent elements) and small (which stores the rest). Each entry in these sets is a pair (frequency, number). When we add a number, we update its frequency and move it to the correct set. When we remove a number, we reduce its frequency and again update its place. If large has fewer than x elements, we always fill it first. If a new pair is too small for large, it goes to small. If it belongs in large, we move the smallest element of large to small. When we delete a pair from large, we replace it by moving the largest element from small. We also track the running sum of “frequency × number” for all elements in large, so the x-sum is always available instantly.