Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Range Deleting

Problem Statement
You are given an array A of length N and an integer K.

Let’s denote a function f (l, r) which deletes all elements with value between l and r (in-
clusive) from the array A and returns the resulting array (without changing the order
of other elements). For example, if A = [4, 1, 1, 4, 5, 2, 4, 3], then f (2, 4) = [1, 1, 5] and
f (6, 9) = [4, 1, 1, 4, 5, 2, 4, 3].

Count the number of pairs (l, r) such that 1 ≤ l ≤ r ≤ K and f (l, r) is sorted in non-
decreasing order. Note that the empty array is considered sorted.

Input Format
The first line of input contains two integers, N and K.

The second line of input contains N integers A1 , A2 , . . . , AN .

Note: the input size for subtask 4 is extremely large and the time it takes to read the input
may be very long. The attachments contains a template that uses C++ fast input to read
from the standard input. Note that other than subtask 4, using fast input decreases the time
negligibly (less than 0.05 seconds).

Output Format
Output the number of pairs (l, r) that satisfy the condition above.

Constraints
• 1 ≤ N, K ≤ 5 · 106

• 1 ≤ Ai ≤ K

Subtasks
1. (20 points) N, K ≤ 500
2. (30 points) N, K ≤ 3000
3. (30 points) N, K ≤ 5 · 105
4. (20 points) No additional constraints
5. Sample test cases
Sample Input And Output
Sample Input 1
33
231

Sample Output 1
4

Sample Input 2
74
1312243

Sample Output 2
6

Explanation
In sample input 1, the correct pairs are (1, 1), (1, 2), (1, 3) and (2, 3). For example, f (1, 1) =
[2, 3], which is sorted in non-decreasing order.
Note that (1, 3) is valid as the empty array is considered sorted.

In sample input 2, the correct pairs are (1, 3), (1, 4), (2, 3), (2, 4), (3, 3) and (3, 4). For
example, f (2, 3) = [1, 1, 4], which is sorted in non-decreasing order.
Note that (1, 4) is valid as the empty array is considered sorted.

You might also like