Under construction

Still being built. Things may change

The Tree

Interview

Time limit1sMemory limit1024 MB

Summary
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 cc colors or be colorless. Initially, all vertices are colorless.

You need to process two types of queries.

  1. color(uu, xx): paint vertex uu with color xx, then call color(LL, (x+1) mod c(x + 1) \bmod c) for its left son LL and color(RR, (x−1+c) mod c(x - 1 + c) \bmod c) for its right son RR. This operation repaints the entire (infinite) subtree of vertex uu. Here  mod \bmod is the remainder operation. If a vertex has already been painted, its color changes to the new one.
  2. Given a vertex, output its current color.

Input

The first line contains two integers qq and cc, the number of queries and colors (1≤q≤5⋅1051 \leq q \leq 5 \cdot 10^5, 1≤c≤1091 \leq c \leq 10^9). Each of the next qq queries starts with an integer tit_i, the type of the ii-th query.

If ti=1t_i = 1, the next line contains an integer xx (0≤x≤c−10 \leq x \leq c - 1), the color to paint the vertex. The line after that contains a non-empty string sis_i of the characters 'L' and 'R', the path from the root to vertex uu. 'L' means going to the left son, and 'R' means going to the right son.

If ti=2t_i = 2, the next line contains a path string sis_i 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 5⋅1055 \cdot 10^5.

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 −1-1.

Examples1

  1. Example 1

    Input
    6 3
    1 2
    L
    2
    LL
    1 0
    LL
    2
    LLR
    2
    LR
    2
    R
    
    Expected output
    0
    2
    1
    -1