Cumulative Code
Time limit7sMemory limit512 MB
For the complete binary tree of depth k, answer q queries each summing m elements of its Prüfer code at positions a, a+d, ..., a+(m-1)d.
- Level
Hard8 of 10
- Topics
- Math, Tree, Divide and conquer, Implementation
- Solved
- No attempts yet
Problem
A tree is a graph with nodes and undirected edges in which every pair of nodes is joined 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 the sequence built by removing nodes one at a time until only two nodes are left. In each step, remove the leaf with the smallest label and append the label of its only neighbour to the end of the code. A leaf is a node with exactly one neighbour. The Prüfer code of a labeled tree is therefore an integer sequence of length , and the original tree can be reconstructed from it.
The complete binary tree of depth , written , is the labeled tree on nodes in which node is joined to nodes and for every . Write the Prüfer code of as .
The Prüfer code of can be very long, so you do not print it. Instead, answer questions about sums of selected elements of the code. Each question consists of three integers , and , and its answer is .
Input
The first line contains two integers and (, ): the depth of the complete binary tree and the number of questions. Each of the next lines contains one question as three positive integers , and , where , and are all at most .
Output
Print lines. Line contains a single integer, the answer to the -th question.
Hint

When the Prüfer code of is built, the nodes are removed in the order 4, 5, 2, 1, 6. The Prüfer code of is therefore 2, 2, 1, 3, 3.