P14371 [GeeksforGeeks] Number of subarrays with maximum values in given range5949
普及
题目描述

Given an array of N elements and L and R, print the number of sub-arrays such that the value of the maximum array element in that subarray is at least L and at most R.
Example 1:
Input : Arr[] = {2, 0, 11, 3, 0}
L = 1 and R = 10
Output : 4
Explanation:
The sub-arrays {2}, {2, 0}, {3} and {3, 0}
have maximum in range 1-10.
Example 2:
Input : Arr[] = {3, 4, 1}
L = 2 and R = 4
Output : 5

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function countSubarrays() that takes an array (arr), sizeOfArray (n), element L, integer R, and return the number of subarray with the maximum in range L-R. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ L ≤ R ≤ 10^{6}

输入格式

见题面

输出格式

见题面

样例输入
Arr[] = {2, 0, 11, 3, 0} L = 1 and R = 10
样例输出
4