Interval Collection
Time limit3.5sMemory limit512 MB
After each add or remove, find the shortest enclosing interval of a chosen set of intervals that has no common interval, and output its length.
- Level
Hard8 of 10
- Topics
- Sorting, Greedy, Segment tree
- Solved
- No attempts yet
Problem
Altina is starting an interval collection. An interval is a pair of positive integers with . The length of the interval is . An interval contains another interval if and . Every interval contains itself.
For a non-empty set of intervals, the common intervals are all intervals that are contained in every interval in . If the set of common intervals is non-empty, the greatest common interval of is the common interval with the largest length.
For the same set , the enclosing intervals are all intervals that contain every interval in . This set is always non-empty, so the least enclosing interval of is the enclosing interval with the smallest length.
Initially, Altina has no intervals. There are events that change the intervals she owns.
The first type of event adds an interval to her collection. The same may be added more than once, and each copy is treated as a separate interval.
The second type of event removes an existing interval from her collection. If she has several intervals with the same , she removes exactly one of them.
After each event, Altina chooses a non-empty subset of the intervals she owns. She first prefers a set with no greatest common interval, if one exists. If none exists, she picks a set whose greatest common interval has the smallest possible length. Among the sets that satisfy this condition, she picks one whose least enclosing interval has the smallest possible length.
Your task is to output the length of the least enclosing interval of the set she chose after each event.
Input
The first line of input contains (), the total number of add and remove operations. The next lines have one of the following forms:
A: add the interval to Altina's collection.R: remove one copy of the interval from Altina's collection. The interval is guaranteed to exist, and the collection is guaranteed to be non-empty after the removal.
For all subtasks, .
Output
The output consists of lines. Each line contains the length of the least enclosing interval of the set Altina chose after the corresponding event.
Hint
After is added, the only valid choice is , and its least enclosing interval is .
After is added, has the greatest common interval and the least enclosing interval .
After is added, has the greatest common interval and the least enclosing interval .
After is added, has no greatest common interval, and its least enclosing interval is . The set also has no greatest common interval, but its least enclosing interval is longer than .
After is removed, has no greatest common interval, and its least enclosing interval is .