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

#FT0000015. Đảo tham lam – Đếm các loại mìn (Greed Island)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đảo tham lam – Đếm các loại mìn (Greed Island)

Greed Island

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

On Greed Island, a defensive line consists of nn positions. Each deployment selects a consecutive interval and places a completely new type of mine, never used in an earlier deployment, at every position in that interval.

Command 1 l r deploys a new mine type at all positions of the inclusive interval [l,r][l,r]. Previously deployed types remain in place even when intervals overlap; no mine is ever removed.

Command 2 l r counts the number of distinct mine types that occur at least once within the inclusive query interval [l,r][l,r]. A type covering several positions is counted once only. Two separate deployments always represent different types, even if their intervals are identical.

Answer each query at the moment it occurs, considering all earlier deployments.

Input

  • Line 1: n m gives defense-line length and command count.
  • Next mm lines: each line contains one of the following commands, in execution order:
    • 1 l r: deploy a new mine type on [l,r][l,r].
    • 2 l r: count distinct mine types covering at least one position of [l,r][l,r].

Output

For each 2 l r command, print the number of distinct mine types deployed at least at one position in [l,r][l,r]. Print one answer per line in query order.

Subtasks

Subtask 1 (30 points):

  • 1≤n≤10001\le n\le 1000.
  • 1≤m≤10001\le m\le 1000.
  • 1≤l≤r≤n1\le l\le r\le n in both operation types.
  • Every 1 l r introduces a new mine type distinct from all previously deployed types.

Subtask 2 (70 points):

  • 1≤n≤1000001\le n\le 100000.
  • 1≤m≤1000001\le m\le 100000.
  • 1≤l≤r≤n1\le l\le r\le n in both operation types.
  • Every 1 l r introduces a new mine type distinct from all previously deployed types.

Examples

Input

5 6
1 1 3
1 3 5
2 2 4
2 4 5
1 2 2
2 2 2

Output

2
1
2

Explanation

There are n=5n=5 elements. No mine type has been deployed yet.

  1. Command 1 — 1 1 3: Deploy mine type 1 on [1,3][1,3]; deployed intervals: [1,3][1,3].

  2. Command 2 — 1 3 5: Deploy mine type 2 on [3,5][3,5]; deployed intervals: [1,3][1,3], [3,5][3,5].

  3. Command 3 — 2 2 4: Query [2,4][2,4]; intersecting mine types: 1, 2. There are 22 distinct types; print 2.

  4. Command 4 — 2 4 5: Query [4,5][4,5]; intersecting mine types: 2. There are 11 distinct types; print 1.

  5. Command 5 — 1 2 2: Deploy mine type 3 on [2,2][2,2]; deployed intervals: [1,3][1,3], [3,5][3,5], [2,2][2,2].

  6. Command 6 — 2 2 2: Query [2,2][2,2]; intersecting mine types: 1, 3. There are 22 distinct types; print 2.

Collect the answers to output-producing commands in their input order, printing one answer per line.