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

#LCA0000037. Giá trị nhỏ nhất trên đoạn nửa mở (Static RMQ)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giá trị nhỏ nhất trên đoạn nửa mở (Static RMQ)

Static RMQ

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a static array of nn nonnegative integers. For each query (l,r)(l,r), find the minimum among al,al+1,…,ar−1a_l,a_{l+1},\ldots,a_{r-1}.

Important: The interval is half-open [l,r)[l,r): index ll is included but rr is excluded. Array indices start from 00.

Input

  • First line: n,qn,q.
  • Second line: nn values a0,…,an−1a_0,\ldots,a_{n-1}.
  • Next qq lines: l,rl,r with l<rl<r.

Output

Print the minimum for each query on a separate line.

Subtasks

General constraints (all subtasks): 1≤n,q≤5⋅1051\le n,q\le 5\cdot 10^5; 0≤ai≤1090\le a_i\le 10^9; 0≤l<r≤n0\le l<r\le n.

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤501\le q\le 50; 0≤l<r≤n0\le l<r\le n.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000; 0≤l<r≤n0\le l<r\le n.
  • Subtask 3 (50 points): 1≤n≤5000001\le n\le 500000; 1≤q≤5000001\le q\le 500000; 0≤l<r≤n0\le l<r\le n.

Examples

Input

4 4
2 10 1 100
0 1
0 3
1 4
3 4

Output

2
1
1
100

Explanation

Range [0,1)[0,1) contains only 22; [0,3)[0,3) contains 2,10,12,10,1 with minimum 11; [1,4)[1,4) also has minimum 11.