XYQueries

Time limit: 2s
Memory limit: 128MB
Input:
Output:

Task

You are given an array AA with NN integers. Let min(i,j)\min(i, j) be the minimum value in the subarray Ai,Ai+1,,AjA_i, A_{i+1}, \dots, A_j, and max(i,j)\max(i, j) the maximum value in the subarray Ai,Ai+1,,AjA_i, A_{i+1}, \dots, A_j. You are also given QQ queries, for the ithi^{th} query you are required to count the number of pairs (l,r)(l, r), 1lrN1 \leq l \leq r \leq N where min(l,r)=Xi\min(l, r) = X_i and max(l,r)=Yi\max(l, r) = Y_i.

Input data

The first line of the input contains two integers NN and QQ. The second line contains NN integers, A1,A2,,ANA_1, A_2, \dots, A_N. The next QQ lines describe the queries, the ithi^{th} of them contains two integers Xi,YiX_i, Y_i.

Output data

The output will consist of QQ lines, the ithi^{th} line containing the answer to the ithi^{th} query.

Constraints and clarifications

  • 1N,Q1051 \leq N , Q \leq 10^{5}
  • 1AiN, i{1,2,,N}1 \leq A_i \leq N , \forall \ i \in \{1, 2, \dots, N \}
# Points Constraints
1 9 1N,Q1001 \leq N,Q \leq 100
2 23 1Q1001 \leq Q \leq 100
3 31 The test cases are randomly generated.
4 37 No additional constraints.

Example 1

stdin

7 5
3 5 3 4 2 1 5
2 5
3 5
1 2
2 4
2 4

stdout

2
5
1
2
2

Example 2

stdin

15 10
7 9 2 13 12 13 11 13 13 13 10 5 13 7 2
5 13
7 13
11 13
5 10
7 13
2 9
7 9
2 9
11 13
11 13

stdout

25
1
15
1
1
2
1
2
15
15

Log in or sign up to be able to send submissions!