The Tree
InterviewTime limit1sMemory limit1024 MB
Paint whole subtrees of an infinite binary tree, where colors shift by one per left or right step, and report the current color of vertices given by root paths.
- Level
Medium6 of 10
- Topics
- Trie, Tree, String, Implementation
- Solved
- No attempts yet
Problem
You are given an infinite binary tree. The tree has a root, and infinitely many vertices. Each vertex has a left son and a right son, and each vertex except the root has a parent.
Every vertex can be painted in one of colors or be colorless. Initially, all vertices are colorless.
You need to process two types of queries.
color(, ): paint vertex with color , then callcolor(, ) for its left son andcolor(, ) for its right son . This operation repaints the entire (infinite) subtree of vertex . Here is the remainder operation. If a vertex has already been painted, its color changes to the new one.- Given a vertex, output its current color.
Input
The first line contains two integers and , the number of queries and colors (, ). Each of the next queries starts with an integer , the type of the -th query.
If , the next line contains an integer (), the color to paint the vertex. The line after that contains a non-empty string of the characters 'L' and 'R', the path from the root to vertex . 'L' means going to the left son, and 'R' means going to the right son.
If , the next line contains a path string in the same format, describing the vertex whose color should be output.
The sum of the lengths of the paths of all queries does not exceed .
Output
For each query of the second type, print the color of the corresponding vertex on a separate line. If the vertex is colorless, print .