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

#BS0000103. Truy vấn đếm trong khoảng (Fast Search)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn đếm trong khoảng (Fast Search)

Fast Search

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer array with possible duplicates, for each query [l,r][l,r] count positions ii satisfying l≤ai≤rl\le a_i\le r, counting repeated values separately.

Input

The first line contains nn, the next line contains nn integers, followed by qq and then qq lines each containing l,rl,r.

Output

For each query, print the count on its own line.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤201\le n,q\le 20; −109≤ai≤109-10^9\le a_i\le10^9; −109≤lj≤rj≤109-10^9\le l_j\le r_j\le10^9.
  • Subtask 2 (30 points): 1≤n,q≤20001\le n,q\le 2000; −109≤ai≤109-10^9\le a_i\le10^9; −109≤lj≤rj≤109-10^9\le l_j\le r_j\le10^9.
  • Subtask 3 (50 points): 1≤n,q≤1000001\le n,q\le 100000; −109≤ai≤109-10^9\le a_i\le10^9; −109≤lj≤rj≤109-10^9\le l_j\le r_j\le10^9.

Examples

Input

5
10 1 10 3 4
4
1 10
2 9
3 4
2 2

Output

5
2
2
0

Explanation

Array [10,1,10,3,4][10,1,10,3,4] contains five values in [1,10][1,10]. Only 3,43,4 lie in [2,9][2,9], and also in [3,4][3,4]. No entry equals 22. The four counts are 5, 2, 2, 0.