Editorial for DMPG '19 S6 - A Strange Array
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, it suffices to use two pointers for each query, which gives per query.
Time Complexity:
For the second subtask, we must make the following observation: suppose we have some subarray with sum . Observe that we can always remove some elements from the two ends of this subarray to obtain another subarray with sum . If either endpoint is a , we simply remove it and we are done. Otherwise, none of the two endpoints is a which means they are both , so we remove both and we are done.
Therefore, if we have a subarray with sum , we can form any subarray with sum if and has the same parity as . This means that we only care about the maximum subarray sum for each parity. This means each query is simplified down to checking for the maximum subarray sum of any subarray contained within to with the same parity as . For each index , we store the maximum prefix sum of any index before and likewise we store the minimum prefix sum of any index after . Then each query can be done in by simply subtracting the corresponding values to obtain the maximum subarray sum, , with the same parity as . If we can find a subarray with sum , the answer is YES
, otherwise, the answer is NO
.
Time Complexity:
Comments