#SRD0000062. Ghép cặp cùng màu (Range Pairing Query)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 through , 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 to . Every query uses an inclusive interval containing both endpoints. Queries do not modify the original array and are independent of one another.
Input
First line contains . The next line contains values . The following line contains . There follow lines .
Output
Print one integer per query line, preserving the original query order.
Subtasks
- Subtask 1 (10 points): ; ; ; .
- Subtask 2 (15 points): ; ; ; .
- Subtask 3 (25 points): ; ; ; .
- Subtask 4 (50 points): ; ; ; .
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 , colors and each appear twice and color once, yielding two disjoint pairs. A singleton range forms no pair. The full range allows four pairs. In , colors and each appear twice, yielding two pairs. In , only color yields one pair. In , colors , , and each occur twice, yielding three pairs.