AmazonOA #101
Calculate Beauty Values
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesBinary Search
Append each closed-interval subarray to the beautiful array in order; duplicate elements produced by overlapping intervals are kept. Positions of the original array never selected by any interval are called unused positions; for each unused value, count the elements of beautiful strictly smaller than it, and finally sum these counts.
Input
The first line contains n m; the second line contains n values arr; each of the next m lines contains a 0-based closed interval l r. The original gives no value ranges; this site sets 1≤n,m≤100000, −10⁹≤arr[i]≤10⁹, 0≤l≤r<n.
Output
Output a single integer answer.
Example 1
Input:
3 1
3 1 4
1 1
Output:
2
Example 2
Input:
3 2
2 5 1
0 0
0 0
Output:
2
Example 3
Input:
2 1
1 2
0 1
Output:
0
Example notes
Example 1: the concatenation is [1], and the unused 3 and 4 each contribute 1, for a total of 2. Example 2: the concatenation is [2,2], the unused 5 contributes 2 and 1 contributes 0, for a total of 2. Example 3: every position is used, so there is no contribution and the answer is 0.
Editorial
Loading solution…