Editorial for DMOPC '19 Contest 6 P4 - Grade 12 Math


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Tzak

For the first subtask, it suffices to maintain an array a, where a[i] represents the opinion of the i^\text{th} classmate. To handle updates, simply increment/decrement the given index, and to handle range queries, loop over the range and count how many of the specified values exist in that range.

Time complexity: \mathcal O(NQ)

For full marks, we maintain a segment tree where each node contains a map which maps the opinions in that range to their count. Because all values are initialized to 0, and we only have to handle increment and decrement updates, there can be at most 1413 distinct values in the whole segment tree at any given time, which is enough to justify the overhead of having a map at each node. However, to leverage this, special attention must be given to delete keys in the map which are not in use.

Time complexity: \mathcal O((N+Q) \log N)


Comments

There are no comments at the moment.