PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000031. Gom các số thành đoạn liên tiếp (Data Stream as Disjoint Intervals)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Gom các số thành đoạn liên tiếp (Data Stream as Disjoint Intervals)

Data Stream as Disjoint Intervals

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Initially no non-negative integer has been observed. The operation A x records integer xx; recording the same value again changes nothing. The operation G` reports the set of observed integers as the maximal disjoint consecutive integer intervals [l,r][l,r], inclusive at both endpoints. Output intervals in increasing order of their left endpoint.

Input

The first line contains QQ. The next QQ lines each contain either A x or just G.

Output

For each G, first print the number kk of intervals, followed by kk lines each containing the endpoints l,rl,r. If none exist, print 0.

Subtasks

  • Subtask 1 (20 points): 1≤Q≤201\le Q\le 20; 0≤x≤1040\le x\le10^4; at most 100100 operations G.
  • Subtask 2 (30 points): 1≤Q≤8001\le Q\le 800; 0≤x≤1040\le x\le10^4; at most 100100 operations G.
  • Subtask 3 (50 points): 1≤Q≤300001\le Q\le 30000; 0≤x≤1040\le x\le10^4; at most 100100 operations G.

Examples

Input

7
A 1
A 3
G
A 2
G
A 7
G

Output

2
1 1
3 3
1
1 3
2
1 3
7 7

Explanation

Values 1 and 3 form separate intervals. Adding 2 merges them, and adding 7 creates another interval.