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

#FT0000129. Những nét vẽ bằng bút sáp (Crayon)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Những nét vẽ bằng bút sáp (Crayon)

Crayon

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Mary maintains a collection of closed intervals on a number line, initially empty. Process nn operations in order:

  • D L R: draw a new interval [L,R][L,R]; each drawn interval is assigned the next 1-based draw identifier.
  • C I: erase the interval whose draw identifier is II. It is guaranteed to exist and not have been erased before.
  • Q L R: count the currently active drawn intervals intersecting the closed query interval [L,R][L,R] at one or more points.

Identical intervals with different draw IDs count separately. Touching endpoints are intersections. C I erases only that particular drawing.

Input

  • First line: the number of operations nn.
  • Next nn lines: one operation D L R, C I, or Q L R per line.

Output

For each Q operation, print the number of active drawn intervals intersecting the query interval.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; Every interval satisfies 1≤L≤R≤1091\le L\le R\le10^9; C I refers to an existing not-yet-erased drawn interval.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le 4000; Every interval satisfies 1≤L≤R≤1091\le L\le R\le10^9; C I refers to an existing not-yet-erased drawn interval.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; Every interval satisfies 1≤L≤R≤1091\le L\le R\le10^9; C I refers to an existing not-yet-erased drawn interval.

Examples

Example 1

Input

6
D 1 3
D 2 4
Q 2 3
D 2 4
C 2
Q 2 3

Output

2
2

Explanation

The first two drawings are #1 = [1,3][1,3] and #2 = [2,4][2,4], so query [2,3][2,3] intersects both: output 2. Drawing #3 is another [2,4][2,4]. C 2 removes only drawing #2. Drawings #1 and #3 remain and both intersect [2,3][2,3], so the second query also returns 2.

Example 2

Input

7
D 1 2
D 5 9
Q 3 4
Q 2 5
C 1
Q 1 2
Q 8 10

Output

0
2
0
1

Explanation

The first two drawings are [1,2][1,2] and [5,9][5,9]. Query [3,4][3,4] intersects neither; query [2,5][2,5] touches both endpoints and intersects both. After erasing drawing #1, query [1,2][1,2] returns 0, while [8,10][8,10] still intersects drawing #2 and returns 1.