CF2223C.Zhily and Signpost

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

While returning to the base, Jily lost his way. He found that the paths ahead formed a tree. At every fork in the road, there was a signpost, but these signposts were affected by an anomalous magnetic field and kept rotating. Zhily became worried about Jily and wants to ask you several questions. Each time, given a specific moment, he asks which node Jily would eventually stop at if he started from the beginning.

You are given a tree with nn nodes, rooted at 11. Node uu has dud_u children, sorted by node index in increasing order, namely su,0,…,su,du−1s_{u,0},\ldots,s_{u,d_u-1}. It takes some time to cross each edge; specifically, the time needed to cross the edge between node uu and its parent is lul_u.

Each non-leaf node in the tree has a signpost pointing to one of its children. When you are at such a node, you must immediately move to the child it points to, continuing in this way until you reach a leaf node, where you stop immediately.

The signposts change over time. At time mm, the signpost of node uu points to its (m mod du+1)(m\bmod d_u+1)-th child, that is, node su,m mod dus_{u,m \bmod d_u}.

There are now qq queries. In each query, you are given mm, and you need to determine the index of the leaf node you will eventually reach if you start from the root at time mm.

在返回基地的路上,Jily 迷路了。他发现前方的道路构成了一棵树。在每个岔路口都有一块指示牌,但这些指示牌受到异常磁场的影响而持续旋转。Zhily 担心 Jily 的安危,因此想向你提出若干问题:每次给定一个特定时刻,询问若 Jily 从起点(树根)出发,最终会停在哪一个节点上。

你将得到一棵含 nn 个节点的树,树根为节点 11。节点 uu 有 dud_u 个子节点,按节点编号升序排列,记为 su,0,…,su,du−1s_{u,0},\ldots,s_{u,d_u-1}。穿过每条边需要一定时间;具体而言,穿过节点 uu 与其父节点之间边所需时间为 lul_u。

树中每个非叶节点均设有一块指示牌,指向其某个子节点。当你位于这样的节点时,必须立即移向该指示牌所指的子节点,并以此方式持续移动,直至抵达一个叶节点,此时立即停止。

指示牌随时间变化。在时刻 mm,节点 uu 的指示牌指向其第 (m mod du+1)(m\bmod d_u+1) 个子节点,即节点 su,m mod dus_{u,m \bmod d_u}。

现共有 qq 个查询。对每个查询,给定时刻 mm,你需要确定:若从树根出发,于时刻 mm 开始行进,最终抵达的叶节点的编号是多少。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two positive integers n,qn,q (1≤n≤5⋅105,1≤q≤1061 \le n \le 5 \cdot 10^5,1 \le q \le 10^6).

The second line contains n−1n-1 positive integers f2,…,fnf_2,\ldots,f_n (1≤fu<u1 \le f_u \lt u), where fuf_u denotes the index of the parent of node uu.

The third line contains n−1n-1 non-negative integers l2,…,lnl_2,\ldots,l_n (0≤lu≤1090 \le l_u \le 10^9), where lul_u denotes the time needed to cross the edge between node uu and its parent.

The fourth line contains qq non-negative integers m1,…,mqm_1,\ldots,m_{q} (0≤mi≤10180 \le m_i \le 10^{18}), representing the qq queries.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

It is guaranteed that the sum of qq over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个正整数 n,qn, q(1≤n≤5⋅105,  1≤q≤1061 \le n \le 5 \cdot 10^5,\; 1 \le q \le 10^6)。

第二行包含 n−1n-1 个正整数 f2,…,fnf_2,\ldots,f_n(1≤fu<u1 \le f_u \lt u),其中 fuf_u 表示节点 uu 的父节点的编号。

第三行包含 n−1n-1 个非负整数 l2,…,lnl_2,\ldots,l_n(0≤lu≤1090 \le l_u \le 10^9),其中 lul_u 表示穿越节点 uu 与其父节点之间边所需的时间。

第四行包含 qq 个非负整数 m1,…,mqm_1,\ldots,m_{q}(0≤mi≤10180 \le m_i \le 10^{18}),表示 qq 个查询。

保证所有测试用例中 nn 的总和不超过 5⋅1055 \cdot 10^5。

保证所有测试用例中 qq 的总和不超过 10610^6。

输出格式

For each test case, output one line containing qq positive integers, representing the answer to each query.

对于每个测试用例,输出一行包含 qq 个正整数,表示每个查询的答案。

输入输出样例

  • 输入#1

    2
    3 1
    1 1
    10 20
    4
    10 5
    1 2 2 2 1 1 3 4 5
    1 2 3 4 5 6 7 8 9
    1 2 3 4 5

    输出#1

    2
    6 7 9 6 7

说明/提示

The tree in the first test case is shown below.

If you start at time 44, then at that moment the signpost of node 11 points to node 22. You will arrive at node 22 at time 1414 and stop there.

The tree in the second test case is shown below.

If you start at time 33, then at that moment the signpost of node 11 points to node 22. You will arrive at node 22 at time 44. At that moment, the signpost of node 22 points to node 44, so you will arrive at node 44 at time 77. At that moment, the signpost of node 44 points to node 99, so you will arrive at node 99 at time 1515 and stop there.

第一个测试用例中的树如下图所示。

若你在时刻 44 出发,则此时节点 11 的路标指向节点 22。你将在时刻 1414 到达节点 22 并在此停止。

第二个测试用例中的树如下图所示。

若你在时刻 33 出发,则此时节点 11 的路标指向节点 22。你将在时刻 44 到达节点 22。此时,节点 22 的路标指向节点 44,因此你将在时刻 77 到达节点 44。此时,节点 44 的路标指向节点 99,因此你将在时刻 1515 到达节点 99 并在此停止。

输入解题思路,AI测评打分。不知道怎么写?

首页