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+1n+1 vertices (nn is odd), with vertices labeled 0,1,…,n0,1,\ldots,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 00.

In the tree, vertex 00 is the parent of vertex 11, while all other vertices have either 22 children or 00 children.

Bob is lost in one vertex of the tree and wishes to escape the tree by reaching vertex 00. 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 vv (1≤v≤n1 \le v \le n), Bob's movement is determined as follows:

  • If vertex vv is a leaf, Bob always moves to the parent of vv; otherwise, check the next few conditions.
  • If nothing is written on vertex vv, Bob writes 'L' on vertex vv and moves to the left child of vv;
  • If 'L' is written on vertex vv, Bob overwrites it to 'R' and moves to the right child of vv;
  • If 'R' is written on vertex vv, Bob erases it and moves to the parent of vv.

It takes exactly 11 second for Bob to move to an adjacent vertex, so Bob will take exactly xx seconds to perform xx moves.

It has been shown that regardless of which vertex Bob starts on, Bob can reach vertex 00 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 qq queries of the following kind:

  • v  kv\;k: Assuming that Bob started from vertex vv, determine the vertex Bob is on after performing exactly kk moves (1≤v≤n1 \le v \le n).

For each query, let TvT_v be the time taken to reach vertex 00 from vertex vv. Then, it is guaranteed that k<Tvk \lt T_v for every query.

本题沿用问题 E 中的定义,但所求答案不同。

给定一棵包含 n+1n+1 个顶点(nn 为奇数)的二叉树,顶点编号为 0,1,…,n0,1,\ldots,n。每个顶点上至多可写一个字母,且所有顶点初始时均为空。树的根节点为顶点 00。

在该树中,顶点 00 是顶点 11 的父节点;其余所有顶点要么有 22 个子节点,要么没有子节点(即为叶子)。

Bob 迷失在树中的某个顶点上,希望逃出这棵树——即到达顶点 00。对大多数具备常识的人来说,这非常简单。然而,由于 Bob 是个傻子,他发明了一种全新的树遍历方式,称为“傻子优先搜索”(Idiot First Search)。

当 Bob 位于顶点 vv(其中 1≤v≤n1 \le v \le n)时,其移动规则如下:

  • 若顶点 vv 是叶子节点,则 Bob 总是移向 vv 的父节点;否则,继续检查后续条件。
  • 若顶点 vv 上尚未写入任何字母,则 Bob 在 vv 上写入 'L',并移向 vv 的左子节点;
  • 若顶点 vv 上已写有 'L',则 Bob 将其覆盖为 'R',并移向 vv 的右子节点;
  • 若顶点 vv 上已写有 'R',则 Bob 将其擦除,并移向 vv 的父节点。

Bob 移动到一个相邻顶点恰好耗时 11 秒,因此执行 xx 次移动恰好耗时 xx 秒。

已证明:无论 Bob 从哪个顶点出发,他总能在有限(尽管可能大得难以理解)的时间内到达顶点 00。我们不知道是谁证明了这一点;当然不可能是 Bob,但该结论确已获证。

你需要回答 qq 个如下形式的查询:

  • v  kv\;k:假设 Bob 从顶点 vv 出发,求其恰好执行 kk 次移动后所在的顶点(其中 1≤v≤n1 \le v \le n)。

对每个查询,记 TvT_v 为 Bob 从顶点 vv 到达顶点 00 所需的时间。题目保证对每个查询均有 k<Tvk \lt T_v。

输入格式

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 integers nn and qq (1≤n≤300 0011 \le n \le 300\,001, 1≤q≤400 0001 \le q \le 400\,000, nn is odd).

Each of the next nn lines contains two integers lil_i and rir_i denoting the children of vertex ii (0≤li,ri≤n0 \le l_i,r_i \le n).

For each vertex, li=ri=0l_i=r_i=0 is given if the vertex has no children. Otherwise, lil_i and rir_i are the left and right children of vertex ii.

Each of the next qq lines contains two integers vjv_j and kjk_j denoting the jj-th query (1≤vj≤n1 \le v_j \le n, 0≤kj<min⁡(109+7,Tvj)0 \le k_j \color{red}{ \lt } \min(10^9+7,T_{v_j})).

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 nn over all test cases does not exceed 300 001300\,001.

It is guaranteed that the sum of qq over all test cases does not exceed 400 000400\,000.

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

每个测试用例的第一行包含两个整数 nn 和 qq(1≤n≤300 0011 \le n \le 300\,001,1≤q≤400 0001 \le q \le 400\,000,且 nn 为奇数)。

接下来的 nn 行中,第 ii 行包含两个整数 lil_i 和 rir_i,表示顶点 ii 的左右子节点(0≤li,ri≤n0 \le l_i,r_i \le n)。

对每个顶点,若其无子节点,则给出 li=ri=0l_i = r_i = 0;否则,lil_i 和 rir_i 分别为其左、右子节点。

接下来的 qq 行中,第 jj 行包含两个整数 vjv_j 和 kjk_j,表示第 jj 个查询(1≤vj≤n1 \le v_j \le n,0≤kj<min⁡(109+7,Tvj)0 \le k_j \color{red}{ \lt } \min(10^9+7,T_{v_j}))。

保证输入定义了一棵满足题目所述条件的有效二叉树。

保证所有测试用例中 nn 的总和不超过 300 001300\,001。

保证所有测试用例中 qq 的总和不超过 400 000400\,000。

输出格式

For each test case, output the answers to the qq queries on a separate line.

对于每个测试用例,将 qq 个查询的答案分别输出在单独的行上。

输入输出样例

  • 输入#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 00 and vertex 11. Obviously, Bob will be on vertex 11 when 00 moves have been performed after Bob started from vertex 11.

On the second test case, the tree is given as follows.

It takes 1414 seconds for Bob to reach vertex 00 from vertex 33. The moves are as follows:

3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttLcolorred2xrightarrowmathttX1xrightarrowmathttRcolorred3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttRcolorred5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX03 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{L}} \\color{red}{2} \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{R}} \\color{red}{3} \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} \\color{red}{5} \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{X}} 0

Here, the letters above the arrows denote the letter on the vertex before moving to the adjacent vertex, where X\mathtt{X} denotes nothing written.

As highlighted in red, it is shown that:

  • Bob is on vertex 22 when 66 moves have been performed after Bob started on vertex 33;
  • Bob is on vertex 33 when 88 moves have been performed after Bob started on vertex 33;
  • Bob is on vertex 55 when 1111 moves have been performed after Bob started on vertex 33.

在第一个测试用例中,图中仅有两个顶点:顶点 00 和顶点 11。显然,当 Bob 从顶点 11 出发、执行 00 次移动后,他将位于顶点 11。

在第二个测试用例中,该树如下所示。

Bob 从顶点 33 出发,需耗时 1414 秒才能到达顶点 00。其移动过程如下:

3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttR5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttLcolorred2xrightarrowmathttX1xrightarrowmathttRcolorred3xrightarrowmathttL4xrightarrowmathttX3xrightarrowmathttRcolorred5xrightarrowmathttX3xrightarrowmathttX1xrightarrowmathttX03 \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} 5 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{L}} \\color{red}{2} \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{R}} \\color{red}{3} \\xrightarrow{\\mathtt{L}} 4 \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{R}} \\color{red}{5} \\xrightarrow{\\mathtt{X}} 3 \\xrightarrow{\\mathtt{X}} 1 \\xrightarrow{\\mathtt{X}} 0

其中,箭头上的字母表示在移向相邻顶点前,当前顶点上所写的字母;X\mathtt{X} 表示该顶点上未写任何字母。

如红色高亮所示:

  • Bob 从顶点 33 出发、执行 66 次移动后,位于顶点 22;
  • Bob 从顶点 33 出发、执行 88 次移动后,位于顶点 33;
  • Bob 从顶点 33 出发、执行 1111 次移动后,位于顶点 55。

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

首页