Under construction

Still being built. Things may change

Interval Collection

Time limit3.5sMemory limit512 MB

Summary
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 [l,r][l, r] with l<rl < r. The length of the interval is r−lr - l. An interval [l,r][l, r] contains another interval [x,y][x, y] if l≤xl \le x and y≤ry \le r. Every interval contains itself.

For a non-empty set SS of intervals, the common intervals are all intervals [x,y][x, y] that are contained in every interval [l,r][l, r] in SS. If the set of common intervals is non-empty, the greatest common interval of SS is the common interval with the largest length.

For the same set SS, the enclosing intervals are all intervals [x,y][x, y] that contain every interval [l,r][l, r] in SS. This set is always non-empty, so the least enclosing interval of SS is the enclosing interval with the smallest length.

Initially, Altina has no intervals. There are QQ events that change the intervals she owns.

The first type of event adds an interval [l,r][l, r] to her collection. The same [l,r][l, r] may be added more than once, and each copy is treated as a separate interval.

The second type of event removes an existing interval [l,r][l, r] from her collection. If she has several intervals with the same [l,r][l, r], she removes exactly one of them.

After each event, Altina chooses a non-empty subset SS 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 SS she chose after each event.

Input

The first line of input contains QQ (1≤Q≤500 0001 \le Q \le 500\,000), the total number of add and remove operations. The next QQ lines have one of the following forms:

  • A ll rr: add the interval [l,r][l, r] to Altina's collection.
  • R ll rr: remove one copy of the interval [l,r][l, r] 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, 1≤l<r≤1 000 0001 \le l < r \le 1\,000\,000.

Output

The output consists of QQ lines. Each line contains the length of the least enclosing interval of the set SS Altina chose after the corresponding event.

Hint

After [1,5][1, 5] is added, the only valid choice is S={[1,5]}S = \{[1, 5]\}, and its least enclosing interval is [1,5][1, 5].

After [2,7][2, 7] is added, S={[1,5],[2,7]}S = \{[1, 5], [2, 7]\} has the greatest common interval [2,5][2, 5] and the least enclosing interval [1,7][1, 7].

After [4,6][4, 6] is added, S={[1,5],[4,6]}S = \{[1, 5], [4, 6]\} has the greatest common interval [4,5][4, 5] and the least enclosing interval [1,6][1, 6].

After [6,8][6, 8] is added, S={[4,6],[6,8]}S = \{[4, 6], [6, 8]\} has no greatest common interval, and its least enclosing interval is [4,8][4, 8]. The set S={[1,5],[6,8]}S = \{[1, 5], [6, 8]\} also has no greatest common interval, but its least enclosing interval [1,8][1, 8] is longer than [4,8][4, 8].

After [4,6][4, 6] is removed, S={[1,5],[6,8]}S = \{[1, 5], [6, 8]\} has no greatest common interval, and its least enclosing interval is [1,8][1, 8].

Examples1

  1. Example 1

    Input
    5
    A 1 5
    A 2 7
    A 4 6
    A 6 8
    R 4 6
    
    Expected output
    4
    6
    5
    4
    7