#P10264. [GESP202403 八级] 接竹竿

[GESP202403 八级] 接竹竿

Background

Related multiple-choice and true/false problems: https://ti.luogu.com.cn/problemset/1148.

Problem Description

Student Xiao Yang wants to play a card game called “Connecting Bamboo Poles”.

The rules are as follows: each card has a value vv. The given cards are placed one by one at the end of a queue of cards. If, before placing a card, there is already a card in the queue with the same value as this card, then Xiao Yang will remove from the queue all cards between these two cards (including the two cards themselves).

Xiao Yang now has a card sequence AA of length nn, where the value of each card is AiA_i (1in1\le i\le n). Xiao Yang has qq queries. In the ii-th query (1iq1\le i\le q), Xiao Yang gives li,ril_i,r_i. Xiao Yang wants to know: if we play “Connecting Bamboo Poles” using all cards with indices in [li,ri][l_i,r_i] in increasing index order, how many cards remain in the queue at the end.

Input Format

One line contains a positive integer TT, the number of test cases.

For each test case, the first line contains a positive integer nn, the length of the card sequence AA.

The second line contains nn positive integers A1,A2,,AnA_1,A_2,\dots,A_n, the values of the cards in AA.

The third line contains a positive integer qq, the number of queries.

The next qq lines each contain two positive integers li,ril_i,r_i, describing a query.

Output Format

For each test case, output qq lines. The ii-th line (1iq1\le i\le q) outputs a non-negative integer, which is the answer to the ii-th query.

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

1
1
0
2

Hint

Sample Explanation

For the first query, Xiao Yang places the cards in the order 1,2,21,2,2. When placing the last card, the two cards with value 22 are removed, so only one card with value 11 remains in the queue.

For the second query, the queue changes as follows:

$\{\}\to\{1\}\to\{1,2\}\to\{1,2,2\}\to\{1\}\to\{1,3\}\to\{1,3,1\}\to\{\}\to\{3\}$. Therefore, in the end only one card with value 33 remains in the queue.

Constraints

Subtask Score TT nn qq maxAi\max A_i Special Condition
11 3030 5\le 5 100\le100 13\le13
22 1.5×104\le 1.5\times10^4 For all queries, the right endpoint equals nn.
33 4040

For all testdata, it is guaranteed that 1T51\le T\le 5, 1n1.5×1041\le n\le 1.5\times 10^4, 1q1.5×1041\le q\le 1.5\times 10^4, and 1Ai131\le A_i\le 13.

Translated by ChatGPT 5