PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000153. Nhìn thấy các tòa nhà 2 (Buildings 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhìn thấy các tòa nhà 2 (Buildings 2)

Buildings 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn buildings from west to east with heights H1,…,HnH_1,\ldots,H_n. Building j>ij>i is visible from building ii if there is no index kk with i<k<ji<k<j and Hk>HjH_k>H_j. For each query (l,r)(l,r), count buildings j>rj>r that are visible from both buildings ll and rr. Building rr itself and all buildings to its west are excluded.

Input

The first line contains n,qn,q. The next line contains nn heights. Each of the next qq lines contains l,rl,r.

Output

Print one integer per query, the number of qualifying buildings.

Subtasks

  • Subtask 1 (20 points): 2≤n≤122\le n\le 12; 1≤q≤121\le q\le12; Heights are a permutation of 1,2,…,n1,2,\ldots,n; all heights are distinct, and each query satisfies 1≤l<r≤n1\le l<r\le n.
  • Subtask 2 (30 points): 2≤n≤30002\le n\le 3000; 1≤q≤30001\le q\le3000; Heights are a permutation of 1,2,…,n1,2,\ldots,n; all heights are distinct, and each query satisfies 1≤l<r≤n1\le l<r\le n.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 1≤q≤2000001\le q\le200000; Heights are a permutation of 1,2,…,n1,2,\ldots,n; all heights are distinct, and each query satisfies 1≤l<r≤n1\le l<r\le n.

Examples

Example 1

Input

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

Output

2
0
1

Explanation

For (1,2)(1,2), buildings 3 and 5 are visible from both endpoints; building 4 is blocked by 3. For (3,5)(3,5) there are no buildings beyond 5. For (1,4)(1,4), only building 5 lies to the east and is visible.

Example 2

Input

4 2
1 4 2 3
1 2
2 3

Output

0
1

Explanation

For (1,2)(1,2) the tall building 2 blocks both later buildings from 1. For (2,3)(2,3), building 4 has height 3 and no intervening building is taller than 3, so it is visible from both.