Card Game

시간 제한3초메모리 제한2048 MB

요약
카드 배열의 각 온라인 구간 질의마다 스택처럼 카드를 제거하는 규칙을 적용했을 때 카드 수열에 남는 카드 수를 구한다.
난이도

어려움10점 중 8점

유형
스택, 해시맵, 동적 계획법, 구현
정답자
아직 제출이 없습니다

문제

Randias is playing a card game. In this game, each card has a number written on it. For cards with numbers a_1,a_2,…,a_ma\_{1}, a\_{2}, \ldots, a\_{m}, Randias will play the game in the following way.

Initially, all cards are in his hand. Randias will maintain a card sequence (initially empty). In the ii-th operation, Randias will put the ii-th card (this card has number a_ia\_{i} written on it) at the end of the card sequence. Then:

  • If there are no other cards in the sequence with number a_ia\_{i} written on them, the ii-th operation ends.
  • Otherwise, let the jj-th card in the card sequence have number a_ia\_{i} written on it. Randias will take away all cards between the jj-th card and the newly placed card, including the jj-th card and the newly placed card.

For example, let a=\[2,1,3,1,2,3]a = \[2, 1, 3, 1, 2, 3], and the card sequence s=\[]s = \[] initially.

After the 11-st operation, s=\[2]s = \[2].

After the 22-nd operation, s=\[2,1]s = \[2, 1].

After the 33-rd operation, s=\[2,1,3]s = \[2, 1, 3].

After the 44-th operation, s=\[2]s = \[2] (cards 1,3,11, 3, 1 are taken away).

After the 55-th operation, s=\[]s = \[] (cards 2,22, 2 are taken away).

After the 66-th operation, s=\[3]s = \[3].

Now, Randias is given nn cards a_1,a_2,…,a_na\_{1}, a\_{2}, \ldots, a\_{n}. He has qq queries. The ii-th query is a pair of integers ℓ_i,r_i\ell\_{i}, r\_{i}. With this query, Randias wants to know how many cards will be left in the card sequence if the initial list of cards is a_ℓ_i,a_ℓ_i+1,…,a_r_ia\_{\ell\_{i}}, a\_{\ell\_{i} + 1}, \ldots, a\_{r\_{i}}.

For some reason, Randias hopes you can answer the questions online. That is, you need to decode the next question with the answer for the previous question.

입력

The first line contains two integers nn and qq (1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5) denoting the number of cards and the number of queries.

The following line contains nn integers a_1,a_2,…,a_na\_{1}, a\_{2}, \ldots, a\_{n} (1≤a_i≤n1 \le a\_{i} \le n).

Each of the following qq lines contains two integers ℓ′_i\ell'\_{i} and r′_ir'\_{i} (0≤ℓ′_i,r′_i≤2n0 \le \ell'\_{i} ,r'\_{i} \le 2n). Let the answer for the last query is lastans\mathit{lastans}. Then ℓ_i=ℓ′_i⊕lastans\ell\_{i} = \ell'\_{i} \oplus \mathit{lastans} and r_i=r′_i⊕lastansr\_{i} = r'\_{i} \oplus \mathit{lastans} are the next query. In these formulas, ⊕\oplus is the bitwise exclusive OR operation. It is guaranteed that, after decoding, 1≤ℓ_i≤r_i≤n1 \le \ell\_{i} \le r\_{i} \le n. If you haven't answered any queries before, lastans=0\mathit{lastans} = 0.

출력

For each query, output a line with one integer: the answer to that query.

힌트

For the first example, the segments in the queries are \[5,5]\[5, 5], \[2,5]\[2, 5], \[1,1]\[1, 1], \[1,4]\[1, 4], and \[3,5]\[3, 5].

예제2

  1. 예제 1

    입력
    5 5
    3 3 1 1 1
    5 5
    3 4
    3 3
    0 5
    3 5
    
    예상 출력
    1
    2
    1
    0
    1
    
  2. 예제 2

    입력
    7 7
    2 4 1 2 3 1 2
    1 6
    0 4
    3 3
    0 4
    0 3
    0 6
    2 7
    
    예상 출력
    2
    1
    1
    1
    2
    3
    0