#P6645. [CCO 2020] Interval Collection

[CCO 2020] Interval Collection

Description

Altina is collecting intervals. A pair of positive integers [l,r][l, r] with l<rl < r is an interval, and we call the length of such an interval rlr - l.

We say that interval [l,r][l, r] contains interval [x,y][x, y] if and only if lxl \le x and yry \le r. In particular, every interval contains itself.

For a non-empty set SS, define its maximum common sub-interval as the longest interval that is contained in every interval in SS. If no such interval exists, it is undefined.

For a non-empty set SS, define its minimum common super-interval as the shortest interval that contains every interval in SS. Note that such an interval always exists.

Initially, there are no intervals in Altina’s collection. Then QQ events will happen, which change Altina’s collection.

  1. Altina adds an interval [l,r][l, r] to her collection. If [l,r][l, r] already exists in the collection, it should be counted as a different interval.
  2. Altina removes an interval [l,r][l, r] that already exists in the collection. If there are multiple identical ones, remove exactly one.

After each event, Altina chooses a non-empty subset SS of her collection, satisfying the following rules:

  • Among all choices, she will choose one where the maximum common sub-interval is undefined. If no such choice exists, she will choose one where the length of the maximum common sub-interval is as small as possible.
  • Among all sets SS that satisfy the rule above, she will choose one where the length of the minimum common super-interval is as small as possible.

After each event, output the length of the minimum common super-interval of the set SS that Altina will choose.

Input Format

The first line contains a positive integer QQ, the number of events.
The next QQ lines each describe an event in one of the following formats:

  • A  l  r\mathtt{A}\;l\;r: add an interval [l,r][l, r] to Altina’s collection.
  • R  l  r\mathtt{R}\;l\;r: remove an interval [l,r][l, r] from Altina’s collection. It is guaranteed that this interval exists in her collection, and that after removal her collection is non-empty.

Output Format

Output QQ lines. Each line contains one positive integer, the length of the minimum common super-interval of the set SS that Altina will choose after that event.

5
A 1 5
A 2 7
A 4 6
A 6 8
R 4 6
4
6
5
4
7

Hint

Sample Explanation

Add [1,5][1,5]. Choosing [1,5][1,5] is optimal, so the answer is 44.

Add [2,7][2,7]. Choosing [1,5],[2,7][1,5],[2,7] is optimal, so the answer is 71=67-1=6.

Add [4,6][4,6]. Choosing [1,5],[4,6][1,5],[4,6] is optimal, so the answer is 61=56-1=5.

Add [6,8][6,8]. Choosing [4,6],[6,8][4,6],[6,8] is optimal, so the answer is 84=48-4=4.

Remove [4,6][4,6]. Choosing [1,5],[6,8][1,5],[6,8] is optimal, so the answer is 81=78-1=7.

Subtasks

This problem uses bundled testdata.

  • Subtask 1 (1212 points): Q500Q\le 500.
  • Subtask 2 (3232 points): Q1.2×104Q\le 1.2\times 10^4.
  • Subtask 3 (2828 points): Q5×104Q\le 5\times 10^4.
  • Subtask 4 (1616 points): For any two intervals (l1,r1)(l_1,r_1) and (l2,r2)(l_2,r_2), it is guaranteed that r1<l2r_1<l_2 or r2<l1r_2<l_1.
  • Subtask 5 (1212 points): No special restrictions.

For 100%100\% of the data, it is guaranteed that 1Q5×1051\le Q\le 5\times 10^5 and 1l,r1061\le l,r\le 10^6.

Notes

This problem is translated from Canadian Computing Olympiad 2020 Day 2 T2 Interval Collection.

Translated by ChatGPT 5