Running Max

Time limit: 1.28s Memory limit: 256MB Input: Output:

Task

Chimmy has an array of NN integers and QQ queries of the form a ba \ b, where for each query Chimmy wants to know, when traversing the subarray from position aa to position bb, how many times the running maximum changes. Chimmy, not knowing how to program, asks for your help for 100100 points!

Input data

The program reads from the keyboard the numbers NN and QQ, after which it reads NN numbers representing the array.
The next QQ lines will contain two numbers aa and bb, as described in the statement.

Output data

The answer for each query will be displayed on a separate line.

Restrictions and clarifications

  • It is recommended to use fastio.
  • 1N1 000 0001 ≤ N ≤ 1 \ 000 \ 000
  • 1Q1 000 0001 ≤ Q ≤ 1 \ 000 \ 000
  • 1a,bN1 ≤ a, b ≤ N
  • The numbers in the array can be represented on signed 32-bit integers.
  • Traversals from aa to bb will not always be from left to right. For example, if a=5a = 5 and b=3b = 3, the indices will be traversed in the order 5,4,35, 4, 3.

Example

stdin

7 5
4 6 1 3 5 2 8
3 7
4 6
1 7
6 1
5 5

stdout

4
2
3
3
1

Explanation

In the subarray [1,3,5,2,8][1, 3, 5, 2, 8], the maximum changes 44 times (11, 33, 55, 88).

In the subarray [3,5,2][3, 5, 2], the maximum changes 22 times (33, 55).

In the subarray [4,6,1,3,5,2,8][4, 6, 1, 3, 5, 2, 8], the maximum changes 33 times (44, 66, 88).

In the subarray [2,5,3,1,6,4][2, 5, 3, 1, 6, 4], the maximum changes 33 times (22, 55, 66) (the subarray [6,1][6, 1] was traversed in reverse).

In the subarray [5][5], the maximum only changes once (55).

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