Editorial for COCI '21 Contest 4 #3 Izbori
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem requires us to count the number of intervals in the array which contain a "dominant" element, that is, a number which appears more times than all the other numbers combined.
To solve the first subtask, it was sufficient to make a brute-force solution in which iterates over all the intervals and checks if there is a dominant element. Solving the second subtask requires speeding this solution up to work in . One way to do this was to fix the left end of the interval, and while the right end is moving to the right, we keep track of the number of elements in the segment and check to see if there is a dominant element.
To solve the problem entirely, for each value, we'll count the number of segments in which this value is dominant. Fix some value and make a new array which consists of the numbers and . Here, denotes that is located in the array at this position, and that it is not. Now, the number of intervals in which is dominant is equal to the number of intervals in which have a positive sum. This can be counted by making an array of prefix sums (call it ) and for each element of , we count how many elements in before it are smaller than it. However, we do not need to iterate over all elements in . For a block of 's in , we can calculate at once the number of intervals with positive sum for which the right end is in this block. This can be done with a segment tree with propagation, where the nodes store, apart from the usual sum, the sum of the elements multiplied by (), where is the number of elements covered by the segment tree node. The complexity of this solution is , but to score all of the points, it was possible to have other complexities, such as .
Comments