XOR Queries

Maintain an array under appends, rollbacks of the last k elements, and range queries for max XOR, count <= x, and k-th smallest.

Hard9TrieSegment treeBinary searchPrefix sumNo attempts yetTime limit2sMemory limit512 MB

Problem

The array AA is empty at the start. Process the following five kinds of queries in the order they are given. Array indices start at 1.

  • 1 x: append xx to the end of AA.
  • 2 L R x: among the LL-th through RR-th elements of AA, print the element yy whose xor with xx is largest. Two different values give two different xor results, so this yy is unique.
  • 3 k: remove the last kk elements of AA.
  • 4 L R x: print how many of the LL-th through RR-th elements of AA are less than or equal to xx.
  • 5 L R k: print the kk-th smallest number among the LL-th through RR-th elements of AA. A value that occurs several times is counted once per occurrence.

Input

The first line contains the number of queries MM (1M500,0001 \le M \le 500{,}000).

Each of the next MM lines contains one query. In queries of type 1, 2 and 4, the value xx satisfies 1x500,0001 \le x \le 500{,}000.

Let NN be the length of AA right before a query runs. Queries of type 2, 4 and 5 satisfy 1LRN1 \le L \le R \le N, and a query of type 3 satisfies 1kN1 \le k \le N. A query of type 5 also satisfies kRL+1k \le R-L+1.

Output

For every query of type 2, 4 and 5, print its answer on its own line, in the order the queries are given.