This problem can be solved using .
First, take an array sized . Let’s populate the array with crabs according to their numbering. Only indices will be filled and the last indexes will remain empty. For each event, we will take a crab, and place it after the last used index. And we will have to keep track of their health.
This whole process can be done using with . Our Segment tree will have the following properties:
Each node will save that if there any health decrease update for this range of crabs for lazy updates.
Check the health of crab numbered .
Decrease the health of all crabs after the position of crab to the crab placed at the top of the pile. We will save the update of decrease as a lazy value for our desired range.
Place crab after the current crab, that is on the top of the pile.