Infinite Adventure

시간 제한2초메모리 제한1024 MB

요약
각 질의마다 날짜를 2의 거듭제곱으로 나눈 나머지에 따라 목적지가 달라지는 포털 이동을 최대 10^18번 반복한 뒤 도착 도시를 구한다.
난이도

어려움10점 중 8점

유형
그래프, 이분 탐색, 수학, 구현
정답자
아직 제출이 없습니다

문제

Bessie is planning an infinite adventure in a land with NN (1≤N≤1051\leq N \leq 10^5) cities. In each city ii, there is a portal, as well as a cycling time T_iT\_i. All T_iT\_i's are powers of 22, and T_1+⋯+T_N≤105T\_1 + \cdots + T\_N \leq 10^5. If you enter city ii's portal on day tt, then you instantly exit the portal in city c_i,t mod T_ic\_{i, t\bmod{T\_i}}.

Bessie has QQ (1≤Q≤5⋅1041\leq Q \leq 5\cdot 10^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.

입력

The first line contains two space-separated integers: NN, the number of nodes, and QQ, the number of queries.

The second line contains NN space-separated integers: T_1,T_2,…,T_NT\_1, T\_2, \ldots, T\_N (1≤T_i1\leq T\_i, T_iT\_i is a power of 22, and T_1+⋯+T_N≤105T\_1 + \cdots + T\_N \leq 10^5).

For i=1,2,…,Ni = 1, 2, \ldots, N, line i+2i+2 contains T_iT\_i space-separated positive integers, namely c_i,0,…,c_i,T_i−1c\_{i, 0}, \ldots, c\_{i, T\_i-1} (1≤c_i,t≤N1\leq c\_{i, t} \leq N).

For j=1,2,…,Qj = 1, 2, \ldots, Q, line j+N+2j+N+2 contains three space-separated positive integers, v_j,t_j,Δ_jv\_j, t\_j, \Delta\_j (1≤v_j≤N1\leq v\_j \leq N, 1≤t_j≤10181\leq t\_j \leq 10^{18}, and 1≤Δ_j≤10181\leq \Delta\_j \leq 10^{18}) representing the jjth query.

출력

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

예제2

  1. 예제 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
    
    예상 출력
    2
    2
    5
    4
    
  2. 예제 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
    
    예상 출력
    2
    3
    5
    4
    2