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

#LCA0000039. Truy vấn nhỏ nhất theo chỉ số 0 (Range Minimum Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn nhỏ nhất theo chỉ số 0 (Range Minimum Query)

Range Minimum Query

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a static list of nn integers indexed from 00 to n−1n-1. For each of qq queries, return the smallest value on the closed interval [i,j][i,j].

The array never changes. If i=ji=j, the answer equals aia_i.

Input

  • First line: nn.
  • Next line: a0,…,an−1a_0,\ldots,a_{n-1}.
  • Next line: qq.
  • Last qq lines: i,ji,j with i≤ji\le j.

Output

Print one minimum per query.

Subtasks

General constraints (all subtasks): 1≤n≤1051\le n\le 10^5; 1≤q≤1041\le q\le 10^4; 0≤i≤j<n0\le i\le j<n; each aia_i fits in a signed 32-bit integer.

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤501\le q\le 50; 0≤l≤r<n0\le l\le r<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\le r<n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤100001\le q\le 10000; 0≤l≤r<n0\le l\le r<n.

Examples

Input

3
1 4 1
2
1 1
1 2

Output

4
1

Explanation

Range [1,1][1,1] contains only 44; range [1,2][1,2] contains 4,14,1 and has minimum 11.