CF2195G.Idiot First Search and Queries
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This problem shares the definitions with problem E. However, it does not ask for the same answer.
There is a binary tree of n+1 vertices (n is odd), with vertices labeled 0,1,…,n. At most one letter can be written on each vertex of the tree, and all vertices initially have nothing written on them. The root of the tree is vertex 0.
In the tree, vertex 0 is the parent of vertex 1, while all other vertices have either 2 children or 0 children.
Bob is lost in one vertex of the tree and wishes to escape the tree by reaching vertex 0. This is very easy for most people with common sense. However, since Bob is an idiot, he created a new way of traversing the tree; introducing the "Idiot First Search".
When Bob is on vertex v (1≤v≤n), Bob's movement is determined as follows:
- If vertex v is a leaf, Bob always moves to the parent of v; otherwise, check the next few conditions.
- If nothing is written on vertex v, Bob writes 'L' on vertex v and moves to the left child of v;
- If 'L' is written on vertex v, Bob overwrites it to 'R' and moves to the right child of v;
- If 'R' is written on vertex v, Bob erases it and moves to the parent of v.
It takes exactly 1 second for Bob to move to an adjacent vertex, so Bob will take exactly x seconds to perform x moves.
It has been shown that regardless of which vertex Bob starts on, Bob can reach vertex 0 in a finite (though possibly inexplicably large) amount of time. We don't know who proved it; surely it can't be Bob, but it is definitely proven.
You are asked to answer q queries of the following kind:
- vk: Assuming that Bob started from vertex v, determine the vertex Bob is on after performing exactly k moves (1≤v≤n).
For each query, let Tv be the time taken to reach vertex 0 from vertex v. Then, it is guaranteed that k<Tv for every query.
本题沿用问题 E 中的定义,但所求答案不同。
给定一棵包含 n+1 个顶点(n 为奇数)的二叉树,顶点编号为 0,1,…,n。每个顶点上至多可写一个字母,且所有顶点初始时均为空。树的根节点为顶点 0。
在该树中,顶点 0 是顶点 1 的父节点;其余所有顶点要么有 2 个子节点,要么没有子节点(即为叶子)。
Bob 迷失在树中的某个顶点上,希望逃出这棵树——即到达顶点 0。对大多数具备常识的人来说,这非常简单。然而,由于 Bob 是个傻子,他发明了一种全新的树遍历方式,称为“傻子优先搜索”(Idiot First Search)。
当 Bob 位于顶点 v(其中 1≤v≤n)时,其移动规则如下:
- 若顶点 v 是叶子节点,则 Bob 总是移向 v 的父节点;否则,继续检查后续条件。
- 若顶点 v 上尚未写入任何字母,则 Bob 在 v 上写入
'L',并移向 v 的左子节点; - 若顶点 v 上已写有
'L',则 Bob 将其覆盖为'R',并移向 v 的右子节点; - 若顶点 v 上已写有
'R',则 Bob 将其擦除,并移向 v 的父节点。
Bob 移动到一个相邻顶点恰好耗时 1 秒,因此执行 x 次移动恰好耗时 x 秒。
已证明:无论 Bob 从哪个顶点出发,他总能在有限(尽管可能大得难以理解)的时间内到达顶点 0。我们不知道是谁证明了这一点;当然不可能是 Bob,但该结论确已获证。
你需要回答 q 个如下形式的查询:
- vk:假设 Bob 从顶点 v 出发,求其恰好执行 k 次移动后所在的顶点(其中 1≤v≤n)。
对每个查询,记 Tv 为 Bob 从顶点 v 到达顶点 0 所需的时间。题目保证对每个查询均有 k<Tv。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains integers n and q (1≤n≤300001, 1≤q≤400000, n is odd).
Each of the next n lines contains two integers li and ri denoting the children of vertex i (0≤li,ri≤n).
For each vertex, li=ri=0 is given if the vertex has no children. Otherwise, li and ri are the left and right children of vertex i.
Each of the next q lines contains two integers vj and kj denoting the j-th query (1≤vj≤n, 0≤kj<min(109+7,Tvj)).
It is guaranteed that the input defines a valid binary tree satisfying the conditions given in the statement.
It is guaranteed that the sum of n over all test cases does not exceed 300001.
It is guaranteed that the sum of q over all test cases does not exceed 400000.
每个测试包含多个测试用例。第一行包含测试用例数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 q(1≤n≤300001,1≤q≤400000,且 n 为奇数)。
接下来的 n 行中,第 i 行包含两个整数 li 和 ri,表示顶点 i 的左右子节点(0≤li,ri≤n)。
对每个顶点,若其无子节点,则给出 li=ri=0;否则,li 和 ri 分别为其左、右子节点。
接下来的 q 行中,第 j 行包含两个整数 vj 和 kj,表示第 j 个查询(1≤vj≤n,0≤kj<min(109+7,Tvj))。
保证输入定义了一棵满足题目所述条件的有效二叉树。
保证所有测试用例中 n 的总和不超过 300001。
保证所有测试用例中 q 的总和不超过 400000。
输出格式
For each test case, output the answers to the q queries on a separate line.
对于每个测试用例,将 q 个查询的答案分别输出在单独的行上。
输入输出样例
输入#1
3 1 1 0 0 1 0 5 5 2 3 0 0 4 5 0 0 0 0 3 6 3 8 3 11 4 7 5 8 7 7 2 3 4 5 0 0 6 7 0 0 0 0 0 0 1 9 2 18 3 11 3 12 3 13 5 7 7 17
输出#1
1 2 3 5 2 1 2 2 1 3 1 2 4
说明/提示
On the first test case, there are only two vertices, vertex 0 and vertex 1. Obviously, Bob will be on vertex 1 when 0 moves have been performed after Bob started from vertex 1.
On the second test case, the tree is given as follows.

It takes 14 seconds for Bob to reach vertex 0 from vertex 3. The moves are as follows:
3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttLcolorred2xrightarrowmathttX1xrightarrowmathttRcolorred3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttRcolorred5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX0
Here, the letters above the arrows denote the letter on the vertex before moving to the adjacent vertex, where X denotes nothing written.
As highlighted in red, it is shown that:
- Bob is on vertex 2 when 6 moves have been performed after Bob started on vertex 3;
- Bob is on vertex 3 when 8 moves have been performed after Bob started on vertex 3;
- Bob is on vertex 5 when 11 moves have been performed after Bob started on vertex 3.
在第一个测试用例中,图中仅有两个顶点:顶点 0 和顶点 1。显然,当 Bob 从顶点 1 出发、执行 0 次移动后,他将位于顶点 1。
在第二个测试用例中,该树如下所示。

Bob 从顶点 3 出发,需耗时 14 秒才能到达顶点 0。其移动过程如下:
3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttLcolorred2xrightarrowmathttX1xrightarrowmathttRcolorred3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttRcolorred5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX0
其中,箭头上的字母表示在移向相邻顶点前,当前顶点上所写的字母;X 表示该顶点上未写任何字母。
如红色高亮所示:
- Bob 从顶点 3 出发、执行 6 次移动后,位于顶点 2;
- Bob 从顶点 3 出发、执行 8 次移动后,位于顶点 3;
- Bob 从顶点 3 出发、执行 11 次移动后,位于顶点 5。
输入解题思路,AI测评打分。不知道怎么写?