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

#SRD0000062. Ghép cặp cùng màu (Range Pairing Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ghép cặp cùng màu (Range Pairing Query)

Range Pairing Query

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Among people at positions ll through rr, find the maximum number of disjoint pairs such that both people in each pair wear the same color. Each person can be used at most once.

Array positions are numbered from 11 to nn. Every query uses an inclusive interval [l,r][l,r] containing both endpoints. Queries do not modify the original array and are independent of one another.

Input

First line contains nn. The next line contains nn values aia_i. The following line contains qq. There follow qq lines l,rl,r.

Output

Print one integer per query line, preserving the original query order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤801\le n\le 80; 1≤q≤1001\le q\le 100; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (15 points): 1≤n≤5 0001\le n\le 5\,000; 1≤q≤15 0001\le q\le 15\,000; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (25 points): 1≤n≤35 0001\le n\le 35\,000; 1≤q≤120 0001\le q\le 120\,000; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (50 points): 1≤n≤100 0001\le n\le 100\,000; 1≤q≤1 000 0001\le q\le 1\,000\,000; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

10
1 2 3 2 3 1 3 1 2 3
6
6 10
5 8
3 6
4 4
1 6
1 10

Output

2
2
1
0
3
4

Explanation

In [6,10][6,10], colors 11 and 33 each appear twice and color 22 once, yielding two disjoint pairs. A singleton range [4,4][4,4] forms no pair. The full range allows four pairs. In [5,8][5,8], colors 33 and 11 each appear twice, yielding two pairs. In [3,6][3,6], only color 33 yields one pair. In [1,6][1,6], colors 11, 22, and 33 each occur twice, yielding three pairs.