#P1972. [SDOI2009] HH 的项链

    ID: 918 远端评测题 2000ms 512MiB 尝试: 2 已通过: 0 难度: 7 上传者: 标签>2009线段树各省省选树状数组山东O2优化主席树前缀和

[SDOI2009] HH 的项链

Description

HH has a necklace made of various beautiful shells. HH believes different shells bring good luck, so after each walk, he randomly picks a segment of shells and thinks about the meaning they convey. HH keeps collecting new shells, so his necklace becomes longer and longer.

One day, he suddenly asked: in a given segment of shells, how many different types of shells are there? This is hard to answer because the necklace is very long. So he turns to you for help to solve this problem.

Input Format

One line with a positive integer nn, the length of the necklace.
The second line contains nn positive integers aia_i, where aia_i denotes the type of the ii-th shell in the necklace.
The third line contains an integer mm, the number of queries by HH.
Then mm lines follow, each containing two integers l,rl, r, denoting the query interval.

Output Format

Output mm lines, each with a single integer, in order, representing the answer to the corresponding query.

6
1 2 3 4 3 5
3
1 2
3 5
2 6

2
2
4

Hint

Constraints
For 20%20\% of the testdata, 1n,m50001 \le n, m \leq 5000.
For 40%40\% of the testdata, 1n,m1051 \le n, m \leq 10^5.
For 60%60\% of the testdata, 1n,m5×1051 \le n, m \leq 5\times 10^5.
For 100%100\% of the testdata, 1n,m,ai1061 \le n, m, a_i \leq 10^6, 1lrn1 \le l \le r \le n.
This problem may require fast input. For the largest testdata, the input size is about 20 MB.

Translated by ChatGPT 5