Hi, I have this DnC problem, I would greatly appreciate any help, thank you!
Given an array a with n elements, calculate the sum of min * max over every ranges [l, r] (1 <= l <= r <= n)
Constraints:
1 <= n <= 1e5, 1 <= a_i <= 1e9
Sample test:
4
1 4 3 1
Answer: 58
In case anyone wants to submit their solution, the problem is here. (with n <= 3e5)
Here is a rough outline for how you can find the answer for all subsegments which cover index $$$m = (l + r)/2$$$ for fixed subsegment $$$[l, r]$$$ in your recursion:
Sorry, I'm a bit unclear on this part:
What if the prefix maximum is strictly increasing and always larger than the suffix that lies on [l, m] and the minimum of the right part is always smaller than that of the left side? won't this yield O((m — l + 1) * (r — m)) time complexity for that block alone?
Edit: I got it, since we already calculated prefix sum, we won't have this problem, thank you