Under construction

Still being built. Things may change

Ternary OR and Queries

Interview

Time limit4sMemory limit1024 MB

Summary
Maintain a set S of integers and, for each query x, print the largest OR_3(a, x) over all a in S, where OR_3 takes digit-wise maxima in base 3.
Level

Medium4 of 10

Topics
Trie, Greedy
Solved
No attempts yet

Problem

For two nonnegative integers XX and YY, there exist integers mm, x0x_0, x1x_1, ⋯\cdots, xmx_m, y0y_0, y1y_1, ⋯\cdots, ymy_m that satisfy all of the following conditions.

  • m≥0m \ge 0
  • For every integer ii with 0≤i≤m0 \le i \le m, 0≤xi,yi<30 \le x_i, y_i < 3.
  • X=∑i=0mxi⋅3iX = \sum_{i=0}^{m} x_i \cdot 3^i
  • Y=∑i=0myi⋅3iY = \sum_{i=0}^{m} y_i \cdot 3^i

Then the value of OR3(X,Y):=∑i=0mmax⁡(xi,yi)⋅3iOR_3(X,Y) := \sum_{i=0}^{m} \max(x_i, y_i) \cdot 3^i is uniquely determined.

Problem

Start with the set S={0}S = \{ 0 \}. Write a program that processes MM queries as follows.

  • Type 11 xx: update the set SS to S∪{x}S \cup \{ x \}.
  • Type 22 xx: print max⁡a∈S(OR3(a,x))\max_{a \in S} \left( OR_3(a,x) \right). In other words, for each element aa of the set SS, compute OR3(a,x)OR_3(a,x) and print the largest value.

Note that the queries are cumulative.

Input

The first line contains MM, the number of queries. (1≤M≤500000)(1 \le M \le 500 000)

From the second line onward, each of the next MM lines contains a query in the form qq xx. (1≤q≤2;(1 \le q \le 2; 0≤x<315)0 \le x < 3^{15})

At least one query of type 22 is given. All numbers in the input are integers.

Output

For each query of type 22, print the answer on its own line.

Hint

For real numbers xx and yy, define max⁡(x,y)={x(x≥y)y(x<y)\max(x,y) = \begin{cases} x & (x \ge y) \\ y & (x < y) \end{cases}.

315=143489073^{15} = 14 348 907.

Examples1

  1. Example 1

    Input
    5
    2 8
    1 5
    2 21
    1 7
    2 15
    
    Expected output
    8
    23
    17