Sheldon and Subarray Queries

0

0 votes
Medium
Problem

Barry Kripke has given Sheldon a problem, which he says is a "Very easy widdle". In the problem, there is an array of integers of length N and Q queries, each asking to calculate the number of continuous subarrays which are having their least element in the range of [L, R].

Formally speaking, for each query, calculate the total number of subarrays such that for each of those subarrays following condition is satisfied,

Lmin(Ai,Ai+1,Ai+2,...,Aj)R

for a subarray from indices i to j.

Since, Sheldon is busy writing his Nobel prize speech, he has given you the task of solving the problem.

Input Format:

First line contains N, length of the array.

Second line contains N integers denoting the given array.

Third line contains Q, number of queries.

Each of the next Q lines contains L and R denoting the query.

Output Format:

For each query, print the required answer in separate line.

Constraints:

1N106

1Ai109

1Q105

1LR109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Since minimum of all subarrays of given array lies in range [3, 4], both first and second queries will be satisfied by all 10 subarrays.

For third query, subarrays satisfying are {3}, {3, 4}, {3, 4, 4}, {3, 4, 4, 3}, {4, 4, 3}, {4, 3}, {3}.

For fourth query, subarrays satisfying are {4}, {4, 4}, {4}.

For fifth query, there are no subarrays satisfying the query.

Editor Image

?