P10198 [USACO24FEB] Infinite Adventure P

Background

**注意:本题的内存限制为 512MB,通常限制的 2 倍。**

Description

Bessie is planning an infinite adventure in a land with $N$ ($1 \le N \le 10^5$) cities. In each city $i$, there is a portal, as well as a cycling time $T_i$. All $T_i$'s are powers of $2$, and $T_1+\dots+T_N \le 10^5$. If you enter city $i$'s portal on day $t$, then you instantly exit the portal in city $c_{i,t\bmod T_i}$. Bessie has $Q$ ($1\le Q\le5\times10^4$) plans for her trip, each of which consists of a tuple $(v,t,\Delta)$. In each plan, she will start in city $v$ on day $t$. 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

The first line contains two space-separated integers: $N$, the number of nodes, and $Q$, the number of queries. The second line contains $N$ space-separated integers: $T_1,T_2,\dots,T_N$ ($1\le T_i$, $T_i$ is a power of $2$, and $T_1+\dots+T_N\le10^5$). For $i=1,2,\dots,N$, line $i+2$ contains $T_i$ space-separated positive integers, namely $c_{i,0},\dots,c_{i,T_i−1}$ ($1\le c_{i,t}\le N$). For $j=1,2,\dots,Q$, line $j+N+2$ contains three space-separated positive integers, $v_j,t_j,\Delta_j$ ($1\le v_j\le N$, $1\le t_j\le10^{18}$, and $1≤\Delta_j≤10^{18}$) representing the $j$ th query.

Output Format

Print $Q$ lines. The $j$ th line must contain the answer to the $j$ th query.

Explanation/Hint

**【SAMPLE1 EXPLAIN】** 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$. **【SCORING】** - Input 3: $\Delta_j \le 2 \times 10^2$. - Inputs 4-5: $N,\sum T_j \le 2 \times 10^3$. - Inputs 6-8: $N,\sum T_j \le 10^4$. - Inputs 9-18: No additional constraints.