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

#FT0000136. Tòa nhà gặp nhau sớm nhất (Find Building Where Alice and Bob Can Meet)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tòa nhà gặp nhau sớm nhất (Find Building Where Alice and Bob Can Meet)

Find Building Where Alice and Bob Can Meet

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn buildings, indexed from 00 to n−1n-1, arranged from left to right. Building ii has height hih_i.

A person standing at building ii may move to building jj if both i<ji<j and hi<hjh_i<h_j. The person may also remain at their starting building. Moving to a farther building does not require visiting the intervening buildings.

There are qq independent queries. In query (a,b)(a,b), Alice starts at building aa and Bob at building bb. Find the smallest index of a building reachable by both people, possibly using several valid moves. Return −1-1 if no such building exists.

They may meet at one person's starting building if the other can move there. If a=ba=b, they already meet there. All indices in the input and output are zero-based.

Input

The first line contains n,qn,q. The second line contains h0,…,hn−1h_0,\ldots,h_{n-1}. Each of the next qq lines contains a query pair a,ba,b; queries are independent.

Output

Print exactly qq lines. Line kk contains the smallest possible meeting building index for query kk, or −1-1 if no meeting is possible.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤301\le q\le 30; 1≤hi≤1091\le h_i\le 10^9 for every 0≤i<n0\le i<n; 0≤a,b<n0\le a,b<n for each query.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤hi≤1091\le h_i\le 10^9 for every 0≤i<n0\le i<n; 0≤a,b<n0\le a,b<n for each query.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 1≤q≤500001\le q\le 50000; 1≤hi≤1091\le h_i\le 10^9 for every 0≤i<n0\le i<n; 0≤a,b<n0\le a,b<n for each query.

Examples

Example 1

Input

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

Output

2
5
-1
5
2

Explanation

For (0,1)(0,1), starting heights are 66 and 44; building 22 has height 88 and is reachable by both, and is the first such building. For (0,3)(0,3), building 55 (height 77) is the first meeting point taller than both starting heights. For (2,4)(2,4), Alice at height 88 cannot move to a taller building to her right, so the result is −1-1. For (3,4)(3,4), both reach building 55. For (2,2)(2,2), they meet immediately at building 22.

Example 2

Input

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

Output

7
6
-1
4
6

Explanation

For (0,7)(0,7), h0=5<h7=6h_0=5<h_7=6, so Alice can reach Bob directly at 77. For (3,5)(3,5), the people start at heights 22 and 11, and building 66 (height 44) is the first common destination. For (5,2)(5,2), the person at index 22 has height 88 and cannot move farther right, so the answer is −1-1. For (3,0)(3,0), index 44 has height 66, exceeding both starting heights, so the answer is 44. For (1,6)(1,6), Bob is already at index 66 and Alice can move there, giving 66.