#P1028. Problem 3. Infinite Adventure

Problem 3. Infinite Adventure

当前没有测试数据。

一、Problem Name

Infinite Adventure

二、Problem Description

Bessie is planning an infinite adventure in a land with NN (1N1051\leq N\leq10^5) cities. In each city ii, there is a portal, as well as a cycling time TiT_i. All TiT_i's are powers of 22, and T1++TN105T_1+\cdots+T_N\leq10^5. If you enter city ii's portal on day tt, then you instantly exit the portal in city ci,tmodTic_{i,t\bmod T_i}.

Bessie has QQ (1Q51041\leq Q\leq5\cdot10^4) plans for her trip, each of which consists of a tuple (v,t,Δ)(v,t,\Delta). In each plan, she will start in city vv on day tt. She will then do the following Δ\Delta times: She will follow the portal in her current city, then wait one day. For each of her plans, she wants to know what city she will end up in.

三、Input Format (input arrives from the terminal / stdin)

  1. The first line contains two space - separated integers: NN, the number of nodes, and QQ, the number of queries.
  2. The second line contains NN space - separated integers: T1,T2,,TNT_1,T_2,\cdots,T_N (1Ti1\leq T_i, TiT_i is a power of 22, and T1++TN105T_1+\cdots+T_N\leq10^5).
  3. For i=1,2,,Ni = 1,2,\cdots,N, line i+2i + 2 contains TiT_i space - separated positive integers, namely ci,0,,ci,Ti1c_{i,0},\cdots,c_{i,T_i - 1} (1ci,tN1\leq c_{i,t}\leq N).
  4. For j=1,2,,Qj = 1,2,\cdots,Q, line j+N+2j + N + 2 contains three space - separated positive integers, vj,tj,Δjv_j,t_j,\Delta_j (1vjN1\leq v_j\leq N, 1tj10181\leq t_j\leq10^{18}, and 1Δj10181\leq\Delta_j\leq10^{18}) representing the jjth query.

四、Output Format (print output to the终端 / stdout)

Print QQ lines. The jjth line must contain the answer to the jjth query.

五、Sample Input and Output

Sample Input 1

5 4
1 2 1 2 8
2
3 4
4
2 3
5 5 5 5 5 1 5 5
2 4 3
3 3 6
5 3 2
5 3 7

Sample Output 1

2
2
5
4

Explanation: Bessie's first three adventures proceed as follows: In the first adventure, she goes from city 2 at time 4 to city 3 at time 5, to city 4 at time 6, to city 2 at time 7. In the second adventure, she goes from city 3 at time 3 to city 4 at time 4, to city 2 at time 5, to city 4 at time 6, to city 2 at time 7, to city 4 at time 8, to city 2 at time 9. In the third adventure, she goes from city 5 at time 3 to city 5 at time 4, to city 5 at time 5.

Sample Input 2

5 5
1 2 1 2 8
2
3 4
4
2 3
5 5 5 5 5 1 5 5
2 4 3
3 2 6
5 3 2
5 3 7
5 3 1000000000000000000

Sample Output 2

2
3
5
4
2

六、Scoring Rules

  1. Input 3: Δj2102\Delta_j\leq2\cdot10^2.
  2. Inputs 4 - 5: N,Tj2103N,\sum T_j\leq2\cdot10^3.
  3. Inputs 6 - 8: N,Tj104N,\sum T_j\leq10^4.
  4. Inputs 9 - 18: No additional constraints.

Problem credits: Brandon Wang.