AmazonOA #191
Find Sum of Beauties
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesBinary Search
Each closed interval is taken from the original array and the pieces are concatenated; repeated and overlapping intervals contribute their elements again. An original index covered by any interval has beauty 0; an uncovered index has beauty equal to the number of elements in the concatenated array strictly less than its value. Find the total beauty. The source example miscounted repeated elements; this site corrects it to 12.
Input
The first line contains n m, the second line the array, followed by m lines of 0-indexed closed intervals l r. The source had no numeric bounds; this site uses 1≤n,m≤200000, −10^9≤element≤10^9, 0≤l≤r<n.
Output
Output a single integer, the answer.
Example 1
Input:
6 4
1 2 3 2 4 5
0 1
3 4
0 0
3 4
Output:
12
Example 2
Input:
2 1
2 2
0 0
Output:
0
Example 3
Input:
3 2
1 3 5
0 0
0 0
Output:
4
Example notes
In the first example the uncovered 3 has 5 smaller elements and the 5 has 7, for a total of 12; the other two examples give 0 and 4.
Editorial
Loading solution…