This page is still under construction.

Parts of this page are still being built. What you see may change.

Borrowing a Lump Only to Come Back with One

Time limit2sMemory limit1024 MB

Summary
A tree of vertical segments grows online; each query asks which segment lies L units above a point, and answers feed back into later attachments.
Level

Medium7 of 10

Topics
Tree, Binary search, Prefix sum, DFS
Solved
No attempts yet

Problem

"Borrowing a lump only to come back with one: ad hoc"

The old man with the lump on his face went to the goblin to have his big lump removed, but the goblin, who loves pranks, wants to attach even more lumps instead. For convenience, every lump is treated as a line segment, and at the start the old man has exactly one lump, lump 00, whose length is infinite. The goblin always attaches lumps so that the connection relations among all lumps form a tree. That is, the top of every lump other than lump 00 touches the bottom of exactly one lump.

While attaching lumps, the goblin asks the old man, "What lump is this far above this lump, hmm?" The goblin promised that if all these questions are answered correctly, he will remove every lump at the end. Since the old man is not confident about solving them, you will give the answers on his behalf.

To keep anyone from predicting where lumps will be attached in advance, the goblin uses an unusual method to decide the attachment position.

Input

The first line gives the number of goblin actions QQ (1≤Q≤150 0001 \le Q \le 150\,000).

Across QQ lines, the goblin's actions are given in order, each in the form "query xx LL" or "ad-hoc kk LL". At the moment the goblin is about to act, let the number of lumps attached to the old man be MM.

  • "query xx LL": Starting from the bottom end of lump xx and going LL up along the lumps, you must answer which lump is there. If that point is the boundary between two lumps, answer the number of the upper lump. This answer becomes the "last answer" from then on. (0≤x<M0 \le x < M, 1≤L≤10181 \le L \le 10^{18})
  • "ad-hoc kk LL": Compute x=(k+x = (k + "last answer")mod  M) \mod M, then create a new lump MM of length LL and attach it so that the top of lump MM touches the bottom of lump xx. When attaching lump MM, make sure it touches no lump other than lump xx. If no query has been given yet, the "last answer" is taken to be 00. The sum of all LL given in all ad-hoc actions the goblin performs is at most 101810^{18}. (0≤k<M0 \le k < M)

At least one query is always given.

Output

Output the answers to all query actions in order, one per line.

Examples1

  1. Example 1

    Input
    6
    ad-hoc 0 5
    query 1 3
    ad-hoc 0 3
    query 2 2
    ad-hoc 1 2
    query 3 2
    
    Expected output
    1
    2
    0