Under construction

Still being built. Things may change

Hilbert's Hotel

Time limit1.5sMemory limit1024 MB

Summary
Simulate Hilbert's Hotel as guests arrive in finite or infinite groups, and answer which group occupies a room or which room holds a given index of a group.
Level

Hard8 of 10

Topics
Math, Implementation, Binary search, Combinatorics
Solved
No attempts yet

Problem

Hilbert's Hotel has infinitely many rooms, numbered 0, 1, 2, ⋯\cdots. At most one guest occupies each room. Since people tend to check in as groups, the hotel keeps a group counter variable GG.

Hilbert's Hotel opened today. Soon after, infinitely many people arrived at once, filling every room. All guests received group number 0, and GG was set to 1.

Ironically, the hotel can accept more guests even though every room is full.

  • If kk (k≥1k \geq 1) people arrive at the hotel, then for each room number xx, the guest in room xx moves to room x+kx+k. The new guests then fill all rooms from 0 to k−1k-1.
  • If infinitely many people arrive at the hotel, then for each room number xx, the guest in room xx moves to room 2x2x. The new guests then fill all rooms with odd numbers.

You must write a program that processes the following queries.

  • 1 k - If k≥1k \geq 1, then kk people arrive at the hotel. If k=0k = 0, then infinitely many people arrive. Assign group number GG to the new guests, then increment GG by 1.
  • 2 g x - Find the xx-th smallest room number occupied by a guest with group number gg. Output that value modulo 109+710^9 + 7, followed by a newline.
  • 3 x - Output the group number of the guest in room xx, followed by a newline.

Input

The first line contains an integer QQ (1≤Q≤300 0001 \leq Q \leq 300\,000), the number of queries. Each of the next QQ lines contains a query. All numbers in the queries are integers.

  • For 1 k queries, 0≤k≤1090 \leq k \leq 10^9.
  • For 2 g x queries, g<Gg < G, 1≤x≤1091 \leq x \leq 10^9, and at least xx guests have group number gg.
  • For 3 x queries, 0≤x≤1090 \leq x \leq 10^9.

Output

Process all queries and output as required. It is guaranteed that the output is not empty.

Hint

If you know about cardinals, assume that "infinite" refers only to "countably infinite." If you do not, you do not have to worry about it.

Examples1

  1. Example 1

    Input
    10
    3 0
    1 3
    2 1 2
    1 0
    3 10
    2 2 5
    1 5
    1 0
    3 5
    2 3 3
    
    Expected output
    0
    1
    0
    9
    4
    4