SAC '22 Code Challenge 4 P4 - Obligatory Subarray Problem

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 3.0s
Memory limit: 1G

Author:
Problem types

After setting all his ideas for bugaboos, Max has decided to blindly steal from others he saw online.

Recently, he read an interesting bugaboo on a new online judge called JOMD:

Given an array of length N, A, find the number of subarrays [L, R] 1 \le L \le R \le N such that B \le \max(A_{[L, R]}) - \min(A_{[L, R]}) \le T.

Note: \max(A_{[L, R]}) denotes the maximum value in the range [L, R] in A; likewise, \min(A_{[L, R]}) denotes the minimum value in the range [L, R] in A.

This bugaboo stumped Max, so he asked you for help.

Can you help him?

Constraints

-10^9 \le A_i \le 10^9

0 \le B \le T \le 2 \times 10^9

Subtask 1 [20%]

1 \le N \le 2\,000

Subtask 2 [80%]

1 \le N \le 1\,000\,000

Note: Fast I/O might be required to fully solve this problem (e.g., BufferedReader for Java).

Input Specification

The first line will contain N, B, and T, the number of elements in the array and the minimum and maximum difference between the maximum and minimum in the subarrays.

The next line will contain N space-separated integers, the elements of the array.

Output Specification

Output the number of distinct subarrays that have a difference between the maximum and minimum in [B, T].

Sample Input

8 3 3
1 4 5 2 2 -3 -5 -6

Sample Output

6

Explanation for Sample Output

There are 6 distinct subarrays that have a difference of exactly 3: [1, 2], [2, 4], [2, 5], [3, 4], [3, 5], [6, 8].


Comments

There are no comments at the moment.