Tree Visits
Time limit5sMemory limit1536 MB
Maintain a value X under increments of 2^C modulo 2^N, mark all nodes on each root-to-leaf walk, and report the count of distinct visited nodes.
- Level
Medium7 of 10
- Topics
- Tree, Bit manipulation, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
There is a perfect binary tree of height . It has nodes in total, and of them are leaves at depth .
You walk down the tree using an -bit binary number . Read the bits of starting from the leftmost one: a 0 takes you to the left child, a 1 takes you to the right child. For example, when , takes you to the leftmost leaf, and takes you to the rightmost leaf. With you move from the root to its right child, then to that child's left child.
At the very beginning and only the root has been visited. Write a program that processes the following two queries.
1 C: change to . Then walk down from the root using the new and visit every node you pass. The root and the leaf you land on both count as visited nodes.2: print how many distinct nodes have been visited so far. A node visited several times is counted once.
Input
The first line contains the height of the tree and the number of queries . ()
Each of the next lines contains one query. A query of the first kind has the form 1 C with . A query of the second kind is the single character 2, and at least one such query is given.
Output
For each query of the second kind, print the number of distinct visited nodes on its own line.