Complete Binary Tree and Queries
Time limit3sMemory limit1024 MB
Given a complete binary tree with N vertices and a movable root, answer queries for the sum of vertex numbers in the subtree of v under the current root.
- Level
Medium5 of 10
- Topics
- Math, Tree, Implementation
- Solved
- No attempts yet
Problem
There is a complete binary tree with vertices. Initially, vertex is the root. For every vertex except vertex , vertex has vertex as its parent.
Process the following queries on this tree.
1 v: Change the root of the tree to vertex .2 v: Print the sum of the numbers of all vertices in the subtree rooted at vertex in the current tree.
Input
The first line contains and separated by a space. (; )
The next lines each contain one query. At least one query of type 2 is given. ()
Output
For each query of type 2, print the answer on its own line.