#abc228d. D - Linear Probing

D - Linear Probing

Score : 400400 points

问题描述

存在一个包含 N=220N = 2^{20} 项的序列 A=(A0,A1,,AN1)A = (A_0, A_1, \dots, A_{N - 1})。最初,每个项的值均为 1-1

按照顺序处理 QQ 个查询请求。第 ii1iQ1 \leq i \leq Q)个查询由一个整数 tit_i 和另一个整数 xix_i 描述,其中 tit_i 可以为 1122,具体如下:

  • ti=1t_i = 1,按以下顺序执行操作:
    1. 定义一个整数 hh,令其等于 xix_i
    2. AhmodN1A_{h \bmod N} \neq -1 时,持续向 hh 中加 11。根据本题的约束条件,可以证明这个过程在有限次迭代后结束。
    3. AhmodNA_{h \bmod N} 的值替换为 xix_i
  • ti=2t_i = 2,打印当时 AximodNA_{x_i \bmod N} 的值。

这里,对于整数 aabbamodba \bmod b 表示 aa 除以 bb 后的余数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a sequence A=(A0,A1,,AN1)A = (A_0, A_1, \dots, A_{N - 1}) with N=220N = 2^{20} terms. Initially, every term is 1-1.

Process QQ queries in order. The ii-th query (1iQ)(1 \leq i \leq Q) is described by an integer tit_i such that ti=1t_i = 1 or ti=2t_i = 2, and another integer xix_i, as follows.

  • If ti=1t_i = 1, do the following in order.
    1. Define an integer hh as h=xih = x_i.
    2. While AhmodN1A_{h \bmod N} \neq -1, keep adding 11 to hh. We can prove that this process ends after finite iterations under the Constraints of this problem.
    3. Replace the value of AhmodNA_{h \bmod N} with xix_i.
  • If ti=2t_i = 2, print the value of AximodNA_{x_i \bmod N} at that time.

Here, for integers aa and bb, amodba \bmod b denotes the remainder when aa is divided by bb.

Constraints

  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • ti{1,2}(1iQ)t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
  • 0xi1018(1iQ)0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
  • There is at least one ii (1iQ)(1 \leq i \leq Q) such that ti=2t_i = 2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

QQ

t1t_1 x1x_1

\vdots

tQt_{Q} xQx_{Q}

Output

For each query with ti=2t_i = 2, print the response in one line. It is guaranteed that there is at least one such query.

Sample Input 1

4
1 1048577
1 1
2 2097153
2 3

Sample Output 1

1048577
-1

We have x1modN=1x_1 \bmod N = 1, so the first query sets A1=1048577A_1 = 1048577.

In the second query, initially we have h=x2h = x_2, for which AhmodN=A11A_{h \bmod N} = A_{1} \neq -1, so we add 11 to hh. Now we have AhmodN=A2=1A_{h \bmod N} = A_{2} = -1, so this query sets A2=1A_2 = 1.

In the third query, we print Ax3modN=A1=1048577A_{x_3 \bmod N} = A_{1} = 1048577.

In the fourth query, we print Ax4modN=A3=1A_{x_4 \bmod N} = A_{3} = -1.

Note that, in this problem, N=220=1048576N = 2^{20} = 1048576 is a constant and not given in input.

update @ 2024/3/10 09:58:52