The D. E. Shaw GroupOA #2
Calculate Region
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & Heaps
The heights of a row of students are given in standing order. For each index i, consider all contiguous subarrays containing i in which heights[i] equals the maximum of the subarray (other students may be equally tall). The region of i is the longest length among these subarrays. Return the sum of the region lengths over all i. This is not the number of qualifying subarrays.
Input
The first line contains n, and the second line contains n integers heights[i]. The original problem's own complete ranges: 1≤n≤100000, 1≤heights[i]≤10^9.
Output
Output the exact integer sum. It can reach 10000000000, so 64-bit or arbitrary-precision integers are required; no modulus is taken.
Example 1
Input:
3
3 5 6
Output:
6
Example 2
Input:
3
1 2 1
Output:
5
Example 3
Input:
2
2 2
Output:
4
Example notes
In the original example [3,5,6], the region lengths are 1, 2, and 3, for a total of 6. An original note claimed the missing source image showed 5, but the formal Output and the explicit rules both give 6; this site discloses the conflict and keeps the formal output, which can be recomputed. In the original example [1,2,1], the lengths are 1, 3, and 1, for a total of 5; the original explanation mistakenly dropped the trailing 1 from the middle range, which should be [1,2,1]. The third example [2,2] is added by this site: both people can take the whole array, for a total of 4.
Editorial
Loading solution…