Borrowing a Lump Only to Come Back with One
Time limit2sMemory limit1024 MB
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 , 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 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 ().
Across lines, the goblin's actions are given in order, each in the form "query " or "ad-hoc ". At the moment the goblin is about to act, let the number of lumps attached to the old man be .
- "
query": Starting from the bottom end of lump and going 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. (, ) - "
ad-hoc": Compute "last answer", then create a new lump of length and attach it so that the top of lump touches the bottom of lump . When attaching lump , make sure it touches no lump other than lump . If noqueryhas been given yet, the "last answer" is taken to be . The sum of all given in allad-hocactions the goblin performs is at most . ()
At least one query is always given.
Output
Output the answers to all query actions in order, one per line.