#P4738. [CERC2017] Cumulative Code
[CERC2017] Cumulative Code
题目描述
As you probably know, a tree is a graph consisting of nodes and undirected edges in which any two nodes are connected by exactly one path. In a labeled tree each node is labeled with a different integer between and . The Prüfer code of a labeled tree is a unique sequence associated with the tree, generated by repeatedly removing nodes from the tree until only two nodes remain. More precisely, in each step we remove the leaf with the smallest label and append the label of its neighbour to the end of the code. Recall, a leaf is a node with exactly one neighbour. Therefore, the Prüfer code of a labeled tree is an integer sequence of length . It can be shown that the original tree can be easily reconstructed from its Prüfer code. The complete binary tree of depth , denoted with , is a labeled tree with nodes where node is connected to nodes and for all . Denote the Prüfer code of with . Since the Prüfer code of can be quite long, you do not have to print it out. Instead, you need to answer questions about the sums of certain elements on the code. Each question consists of three integers: and . The answer is the sum of the of the s Prüfer code elements .
输入格式
The first line contains two integers and — the depth of the complete binary tree and the number of questions. The of the following lines contains the question:three positive integers and such that and are all at most .
输出格式
Output 1 lines. The line should contain a single integer — the answer to the question.
题目大意
给一棵深度为 ,点数为 的完全二叉树,其中根节点编号为 ,节点 的儿子为 和 。
记它的 prufer 序列为 ,下标从 开始。给定 次询问,每次给出三个参数 、、,求 。
3 5
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
2
2
1
3
3
4 4
2 1 5
4 4 3
4 8 1
10 3 2
18
15
5
13
7 1
1 1 125
4031
提示
In the first example above, when constructing the Prüfer code for the nodes are removed in the following order: . Therefore, the Prüfer code of is .