A - 図鑑コンプリート

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 266 点

問題文

高橋君は、野生の花の観察記録をつけています。今日の散歩で、N 個の花を見つけて、それぞれの写真を撮りました。

それぞれの花には、1 から 100 までの品種番号が付けられています。高橋君は図鑑を作成するために、同じ品種の花が複数見つかった場合は 1 枚だけを図鑑に載せることにしました。つまり、図鑑には各品種につき最大 1 枚の写真が載ります。

各花の品種番号が見つけた順に与えられたとき、図鑑に載せる写真の枚数(すなわち、見つけた花の品種の種類数)と、図鑑に載るすべての品種番号の合計を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 100
  • 入力はすべて整数

入力

N
S_1 S_2 \ldots S_N
  • 1 行目には、見つけた花の個数を表す整数 N が与えられる。
  • 2 行目には、各花の品種番号を表す N 個の整数 S_1, S_2, \ldots, S_N がスペース区切りで与えられる。S_i は i 番目に見つけた花の品種番号を表す。

出力

図鑑に載せる写真の枚数と、図鑑に載るすべての品種番号の合計を、スペース区切りで 1 行に出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

4 13

入力例 2

10
7 2 7 3 2 8 3 7 1 4

出力例 2

6 25

入力例 3

20
50 25 75 25 100 50 30 75 25 100 1 30 50 99 1 75 42 99 42 88

出力例 3

9 510

Score : 266 pts

Problem Statement

Takahashi is keeping observation records of wild flowers. During today's walk, he found N flowers and took a photo of each one.

Each flower is assigned a species number from 1 to 100. To create an encyclopedia, Takahashi decided that if multiple flowers of the same species are found, only one photo will be included in the encyclopedia. In other words, the encyclopedia contains at most one photo per species.

Given the species numbers of each flower in the order they were found, determine the number of photos to be included in the encyclopedia (i.e., the number of distinct species among the found flowers) and the sum of all species numbers included in the encyclopedia.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 100
  • All inputs are integers

Input

N
S_1 S_2 \ldots S_N
  • The first line contains an integer N representing the number of flowers found.
  • The second line contains N integers S_1, S_2, \ldots, S_N separated by spaces, representing the species numbers of each flower. S_i represents the species number of the i-th flower found.

Output

Print the number of photos to be included in the encyclopedia and the sum of all species numbers included in the encyclopedia, separated by a space, on a single line.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

4 13

Sample Input 2

10
7 2 7 3 2 8 3 7 1 4

Sample Output 2

6 25

Sample Input 3

20
50 25 75 25 100 50 30 75 25 100 1 30 50 99 1 75 42 99 42 88

Sample Output 3

9 510
B - 郵便配達と手紙の重み

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333 点

問題文

高橋君は郵便局の配達員です。担当する配達エリアには、郵便局を含めて一直線に並ぶ N 個の地点があり、順に地点 1, 地点 2, \ldots, 地点 N と番号が付けられています。隣り合う地点の間の距離はいずれも 1 です。地点 1 に郵便局があります。

各地点 i(2 \leq i \leq N)には B_i 通の手紙を届ける必要があります。郵便局である地点 1 に届ける手紙はありません。

高橋君は手紙を届けるために、以下のような 配達ルート を 0 回以上、好きな回数だけ行うことができます。

  1. 出発: 郵便局(地点 1)から出発する。出発時に、まだ届けていない手紙の中から好きなものを好きな通数だけ選んで持つ。持たないこともできる。1 回のルートで持てる手紙の通数に上限はない。
  2. 往路: 地点 1 から地点番号が増える方向(地点 1 \to 2 \to 3 \to \cdots)へ 1 地点ずつ進む。往路で到達した各地点において、手元に持っているその地点宛ての手紙を好きな通数だけ届けることができる(届けなくてもよい)。手紙はその届け先の地点でのみ届けることができ、異なる地点で届けることはできない。往路の途中、到達したどの地点ででも往路を終了し、帰路に移ることができる(地点 1 で即座に帰路に移ることもできるが、その場合移動が発生しないため疲労度は 0 である)。
  3. 帰路: 往路を終了した地点から郵便局(地点 1)に直接戻る。帰路では疲労度は一切発生しない。また、帰路では手紙を届けることもできない。届けなかった手紙は郵便局に持ち帰り、以降のルートで再び持ち出すことができる。

同じ地点宛ての手紙を複数回のルートに分けて届けることもできます。

1 回のルートにおける 疲労度 は次のように計算されます。往路において地点 p から隣の地点 p+1 へ移動するとき(p \geq 1)、そのときに手元に持っている手紙の通数(持ち出した手紙のうち、まだどの地点にも届けていない手元に残っている手紙の通数)に等しい疲労度が発生します。例えば、地点 1 を出発して地点 2 に向かう移動でも、手元の手紙の通数分の疲労度が発生します。疲労度が発生するのは往路の移動時のみです。1 回のルートの疲労度は、そのルートの往路における全ての移動で発生した疲労度の合計です。

全ての手紙を届けるために必要な、全ルートの疲労度の合計の最小値を求めてください。

制約

  • 2 \leq N \leq 10^6
  • 0 \leq B_i \leq 10^7(2 \leq i \leq N)
  • 入力は全て整数である
  • この制約のもとで、答えは符号付き 64bit 整数に収まる

入力

N
B_2 B_3 \ldots B_N
  • 1 行目には、地点の数を表す整数 N が与えられる。
  • 2 行目には、地点 2 から地点 N それぞれに届ける手紙の通数 B_2, B_3, \ldots, B_N が、スペース区切りで与えられる。

出力

全ての手紙を届けるために必要な疲労度合計の最小値を 1 行で出力せよ。


入力例 1

4
2 1 3

出力例 1

13

入力例 2

5
0 4 0 2

出力例 2

16

入力例 3

12
5 0 8 3 12 1 0 7 4 10 6

出力例 3

365

入力例 4

30
100 250 0 75 320 18 999 40 0 560 123 456 789 10 2048 37 81 600 5 900 333 0 712 64 128 1024 16 450 777

出力例 4

184621

入力例 5

2
10000000

出力例 5

10000000

Score : 333 pts

Problem Statement

Takahashi is a mail carrier. In his delivery area, there are N points arranged in a straight line, including the post office, numbered 1, 2, \ldots, N in order. The distance between any two adjacent points is 1. Point 1 contains the post office.

For each point i (2 \leq i \leq N), B_i letters must be delivered. No letters need to be delivered to point 1 (the post office).

To deliver the letters, Takahashi can perform the following delivery route 0 or more times, as many times as he likes:

  1. Departure: Start at the post office (point 1). Upon departure, choose any number of undelivered letters to carry. He may choose to carry none. There is no limit to the number of letters he can carry in a single route.
  2. Outbound Journey: Move 1 point at a time from point 1 in the direction of increasing point numbers (point 1 \to 2 \to 3 \to \cdots). At each point reached during the outbound journey, he can deliver any number of letters currently in his possession that are addressed to that point (he may also choose not to deliver any). Letters can only be delivered to their designated destination point, not to any other points. He can end the outbound journey and transition to the return journey at any point he has reached (he can even transition to the return journey immediately at point 1, in which case no movement occurs and the fatigue incurred is 0).
  3. Return Journey: Return directly to the post office (point 1) from the point where the outbound journey ended. No fatigue is incurred during the return journey. Additionally, no letters can be delivered during the return journey. Undelivered letters are brought back to the post office and can be carried again in subsequent routes.

Letters addressed to the same point can be delivered across multiple different routes.

The fatigue incurred in a single route is calculated as follows: When moving from point p to the adjacent point p+1 (p \geq 1) during the outbound journey, fatigue equal to the number of letters currently in his possession (the number of carried letters that have not yet been delivered to any point and remain in his hands) is incurred. For example, even during the movement from point 1 to point 2 upon departure, fatigue equal to the number of letters in his possession is incurred. Fatigue is only incurred during outbound movements. The fatigue of a single route is the sum of the fatigue incurred during all movements in that route's outbound journey.

Find the minimum sum of fatigue across all routes required to deliver all letters.

Constraints

  • 2 \leq N \leq 10^6
  • 0 \leq B_i \leq 10^7 (2 \leq i \leq N)
  • All input values are integers.
  • Under these constraints, the answer fits within a signed 64-bit integer.

Input

N
B_2 B_3 \ldots B_N
  • The first line contains an integer N, representing the number of points.
  • The second line contains the number of letters to be delivered to points 2 through N, B_2, B_3, \ldots, B_N, separated by spaces.

Output

Print the minimum total fatigue required to deliver all letters in a single line.


Sample Input 1

4
2 1 3

Sample Output 1

13

Sample Input 2

5
0 4 0 2

Sample Output 2

16

Sample Input 3

12
5 0 8 3 12 1 0 7 4 10 6

Sample Output 3

365

Sample Input 4

30
100 250 0 75 320 18 999 40 0 560 123 456 789 10 2048 37 81 600 5 900 333 0 712 64 128 1024 16 450 777

Sample Output 4

184621

Sample Input 5

2
10000000

Sample Output 5

10000000
C - バッテリー残量と省エネモード

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366 点

問題文

高橋君はノートパソコンを使って N 個のタスクを順番に処理します。タスクは 1 番目から N 番目まで順に実行する必要があります。

i 番目のタスクを完了するには、少なくとも H_i の処理能力が必要です。

高橋君のノートパソコンは初期バッテリー残量 M を持っています。各タスクに対して、以下の 2 種類のモードのうちいずれか一方を選んで実行します。

  • 省エネモード:タスクに対して A の処理能力を発揮する。バッテリー残量は変化しない。
  • フルパワーモード:現在のバッテリー残量を p とするとき、タスクに対して p の処理能力を発揮する。ただし、フルパワーモードを使用するとバッテリーが大きく消耗し、使用後のバッテリー残量は \lfloor p / 2 \rfloor になる(\lfloor x \rfloor は x の小数点以下を切り捨てた値)。

各タスクに対してモードの選択はちょうど 1 回行います。発揮した処理能力がそのタスクの必要処理能力以上であれば、そのタスクを完了できます。

高橋君が N 個すべてのタスクを完了できるかどうか判定してください。完了できる場合、フルパワーモードを使う回数の最小値を求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 10^{18}
  • 1 \leq A \leq 10^{18}
  • 1 \leq H_i \leq 10^{18}
  • 入力はすべて整数

入力

N M A
H_1 H_2 \ldots H_N
  • 1 行目には、タスクの数 N、初期バッテリー残量 M、省エネモードの処理能力 A が、スペース区切りで与えられる。
  • 2 行目には、各タスクの必要処理能力 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。

出力

すべてのタスクを完了することが可能な場合、フルパワーモードを使う回数の最小値を 1 行で出力してください。

すべてのタスクを完了することが不可能な場合、-1 を 1 行で出力してください。


入力例 1

5 20 5
3 10 4 8 5

出力例 1

2

入力例 2

4 10 3
2 9 6 4

出力例 2

-1

入力例 3

15 1000 50
10 60 45 500 50 200 30 90 49 60 20 50 1 40 45

出力例 3

5

入力例 4

50 1000000000000 1000
500 1000000000000 999 400000000000 1000 200000000000 700 100000000000 1000 60000000000 800 30000000000 200 15000000000 900 7000000000 999 3000000000 123 1500000000 1000 900000000 50 400000000 888 200000000 777 100000000 666 50000000 555 25000000 444 12000000 333 6000000 222 3000000 111 1500000 1000 700000 950 350000 900 200000 800 100000 700 50000

出力例 4

25

入力例 5

1 1000000000000000000 1
1000000000000000000

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi processes N tasks in order using his laptop. The tasks must be executed sequentially from the 1-st task to the N-th task.

To complete the i-th task, a processing power of at least H_i is required.

Takahashi's laptop has an initial battery level of M. For each task, he chooses and executes it in one of the following two modes:

  • Power Saving Mode: Exerts a processing power of A for the task. The battery level does not change.
  • Full Power Mode: Let p be the current battery level. Exerts a processing power of p for the task. However, using Full Power Mode consumes a large amount of battery, and the battery level after use becomes \lfloor p / 2 \rfloor (where \lfloor x \rfloor denotes the greatest integer less than or equal to x).

For each task, the mode selection is made exactly once. If the exerted processing power is greater than or equal to the required processing power for that task, the task can be completed.

Determine whether Takahashi can complete all N tasks. If he can, find the minimum number of times he needs to use Full Power Mode.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 10^{18}
  • 1 \leq A \leq 10^{18}
  • 1 \leq H_i \leq 10^{18}
  • All input values are integers.

Input

N M A
H_1 H_2 \ldots H_N
  • The first line contains the number of tasks N, the initial battery level M, and the processing power of the Power Saving Mode A, separated by spaces.
  • The second line contains the required processing power for each task, H_1, H_2, \ldots, H_N, separated by spaces.

Output

If it is possible to complete all tasks, print the minimum number of times Full Power Mode is used in a single line.

If it is impossible to complete all tasks, print -1 in a single line.


Sample Input 1

5 20 5
3 10 4 8 5

Sample Output 1

2

Sample Input 2

4 10 3
2 9 6 4

Sample Output 2

-1

Sample Input 3

15 1000 50
10 60 45 500 50 200 30 90 49 60 20 50 1 40 45

Sample Output 3

5

Sample Input 4

50 1000000000000 1000
500 1000000000000 999 400000000000 1000 200000000000 700 100000000000 1000 60000000000 800 30000000000 200 15000000000 900 7000000000 999 3000000000 123 1500000000 1000 900000000 50 400000000 888 200000000 777 100000000 666 50000000 555 25000000 444 12000000 333 6000000 222 3000000 111 1500000 1000 700000 950 350000 900 200000 800 100000 700 50000

Sample Output 4

25

Sample Input 5

1 1000000000000000000 1
1000000000000000000

Sample Output 5

1
D - 配送センターの荷物割り当て

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

高橋君は配送センターの管理者です。この配送センターには N 台のトラックがあり、今日届いた M 個の荷物のうち、できるだけ多くの荷物をトラックに積み込んで配送したいと考えています。

荷物 j(1 \leq j \leq M)の重量は D_j です。

トラック i(1 \leq i \leq N)には、荷物 1 個あたりの重量上限 E_i と、積載可能な荷物の個数上限 C_i が定められています。トラック i には、重量が E_i 以下の荷物(すなわち D_j \leq E_i を満たす荷物 j)のみを積み込むことができ、積み込める荷物の個数は最大 C_i 個です。この重量の条件は荷物ごとに個別に判定されます。すなわち、1 台のトラックに複数の荷物を積む場合でも、個々の荷物の重量がそれぞれ E_i 以下であればよく、積み込んだ荷物の合計重量に関する制約はありません。

各荷物はトラックに積み込む場合、ちょうど 1 台のトラックに積み込みます。同じ荷物を複数のトラックに積み込むことはできません。また、すべての荷物を配送する必要はなく、どのトラックにも積み込まない荷物があっても構いません。

配送できる荷物の最大個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq E_i \leq 10^9
  • 1 \leq C_i \leq M
  • 1 \leq D_j \leq 10^9
  • 入力はすべて整数である。

入力

N M
E_1 C_1
E_2 C_2
\vdots
E_N C_N
D_1 D_2 \ldots D_M
  • 1 行目には、トラックの台数を表す N と、荷物の個数を表す M が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各トラックの情報が与えられる。
  • 1 + i 行目(1 \leq i \leq N)では、トラック i の荷物 1 個あたりの重量上限 E_i と、積載可能な荷物の個数上限 C_i が、スペース区切りで与えられる。
  • N + 2 行目では、各荷物の重量 D_1, D_2, \ldots, D_M が、スペース区切りで与えられる。

出力

配送できる荷物の最大個数を 1 行で出力せよ。


入力例 1

3 5
5 2
10 1
3 2
2 4 6 8 11

出力例 1

3

入力例 2

2 4
3 2
5 2
6 7 8 9

出力例 2

0

入力例 3

7 15
4 3
8 2
15 4
10 1
6 5
20 2
12 3
1 5 6 7 8 9 10 11 12 13 14 15 16 20 21

出力例 3

14

入力例 4

20 60
100 3
50 4
200 5
75 2
150 6
30 5
500 4
250 3
10 10
90 2
120 4
60 6
300 5
40 3
180 7
220 2
15 8
80 4
1000 6
130 3
5 12 18 25 33 37 42 48 55 59 63 70 77 83 88 92 99 105 111 119 125 132 140 148 155 163 172 181 190 205 215 230 245 260 275 290 310 330 350 370 390 410 430 450 470 490 510 530 550 570 590 610 700 800 900 1000 1100 1200 1300 1400

出力例 4

46

入力例 5

1 1
1000000000 1
1000000000

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi is the manager of a distribution center. This distribution center has N trucks, and he wants to load and deliver as many packages as possible out of the M packages that arrived today.

The weight of package j (1 \leq j \leq M) is D_j.

For truck i (1 \leq i \leq N), a weight limit per package E_i and a maximum number of packages C_i are specified. Truck i can only load packages whose weight is at most E_i (i.e., packages j satisfying D_j \leq E_i), and the number of packages it can carry is at most C_i. The weight condition is checked individually for each package. That is, even when loading multiple packages onto a single truck, it is sufficient that each individual package's weight is at most E_i; there is no constraint on the total weight of the loaded packages.

Each package, if loaded onto a truck, must be loaded onto exactly one truck. The same package cannot be loaded onto multiple trucks. Also, it is not necessary to deliver all packages; some packages may remain unloaded on any truck.

Find the maximum number of packages that can be delivered.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq E_i \leq 10^9
  • 1 \leq C_i \leq M
  • 1 \leq D_j \leq 10^9
  • All input values are integers.

Input

N M
E_1 C_1
E_2 C_2
\vdots
E_N C_N
D_1 D_2 \ldots D_M
  • The first line contains N, the number of trucks, and M, the number of packages, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the information for each truck is given.
  • The (1 + i)-th line (1 \leq i \leq N) contains the weight limit per package E_i and the maximum number of packages C_i for truck i, separated by a space.
  • The (N + 2)-th line contains the weights of the packages D_1, D_2, \ldots, D_M, separated by spaces.

Output

Output the maximum number of packages that can be delivered, in a single line.


Sample Input 1

3 5
5 2
10 1
3 2
2 4 6 8 11

Sample Output 1

3

Sample Input 2

2 4
3 2
5 2
6 7 8 9

Sample Output 2

0

Sample Input 3

7 15
4 3
8 2
15 4
10 1
6 5
20 2
12 3
1 5 6 7 8 9 10 11 12 13 14 15 16 20 21

Sample Output 3

14

Sample Input 4

20 60
100 3
50 4
200 5
75 2
150 6
30 5
500 4
250 3
10 10
90 2
120 4
60 6
300 5
40 3
180 7
220 2
15 8
80 4
1000 6
130 3
5 12 18 25 33 37 42 48 55 59 63 70 77 83 88 92 99 105 111 119 125 132 140 148 155 163 172 181 190 205 215 230 245 260 275 290 310 330 350 370 390 410 430 450 470 490 510 530 550 570 590 610 700 800 900 1000 1100 1200 1300 1400

Sample Output 4

46

Sample Input 5

1 1
1000000000 1
1000000000

Sample Output 5

1
E - 本棚の整理

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 466 点

問題文

高橋君は図書館の司書をしています。図書館には横一列に並んだ N 個の棚があり、左から順に棚 1, 2, \ldots, N と番号が付いています。

最初、各棚に置かれている本の冊数は 0 です。高橋君はこれから Q 回の作業を順番に行います。各作業は以下の 3 種類のいずれかです。

  • 作業 1 : 整数 l, r が与えられる。棚 l, l+1, \ldots, r のそれぞれに本を 1 冊ずつ追加する。すなわち、この範囲に含まれる各棚の冊数が 1 増える。
  • 作業 2 : 整数 p が与えられる。棚 p にある本をすべて取り除き、棚 p の冊数を 0 にする。(冊数がすでに 0 の場合は何も起こらない。)
  • 作業 3 : 整数 l, r が与えられる。棚 l, l+1, \ldots, r に現在置かれている本の合計冊数を求める。

各作業 3 について、答えを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 作業 1 および作業 3 において、1 \leq l \leq r \leq N
  • 作業 2 において、1 \leq p \leq N
  • 作業 3 は少なくとも 1 回存在する
  • 入力はすべて整数である
  • 各作業 3 の時点での答えは 0 以上 2^{63} - 1 以下であることが保証される(すなわち符号付き 64 ビット整数に収まる)

入力

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
  • 1 行目には、棚の数を表す整数 N と作業の回数を表す整数 Q が、スペース区切りで与えられる。
  • 続く Q 行にわたって、各作業の内容が 1 行ずつ与えられる。i 番目の作業 \mathrm{query}_i の形式は、作業の種類に応じて以下のとおりである。
  • 作業 1 の場合 : 1 l r — 棚 l から棚 r までの各棚に本を 1 冊ずつ追加する。ここで l は範囲の左端、r は範囲の右端を表す棚番号である。
  • 作業 2 の場合 : 2 p — 棚 p の本をすべて取り除き、冊数を 0 にする。ここで p は対象の棚番号である。
  • 作業 3 の場合 : 3 l r — 棚 l から棚 r までの本の合計冊数を求める。ここで l は範囲の左端、r は範囲の右端を表す棚番号である。

出力

作業 3 が与えられるたびに、その時点での該当範囲の本の合計冊数を 1 行に出力せよ。すべての作業 3 の答えを、入力で与えられた順に出力すること。


入力例 1

5 7
3 1 5
1 2 4
3 1 5
1 3 5
2 3
3 2 4
3 5 5

出力例 1

0
3
3
1

入力例 2

3 8
2 2
3 1 3
1 1 1
1 1 3
3 1 1
2 1
3 1 2
3 3 3

出力例 2

0
2
1
1

入力例 3

10 16
1 1 10
1 3 7
3 1 10
2 5
3 4 6
1 5 5
1 6 10
3 5 10
2 10
1 1 3
3 1 3
2 1
2 5
3 1 10
1 2 9
3 7 9

出力例 3

15
4
13
7
17
10

入力例 4

30 30
1 1 30
1 1 15
1 16 30
3 1 30
2 1
2 30
3 1 5
1 5 25
3 20 30
2 10
2 20
1 10 20
3 10 20
1 1 1
1 30 30
3 1 30
2 15
1 14 16
3 14 16
1 2 29
2 16
3 15 17
1 1 30
3 1 1
2 1
3 1 30
1 7 7
2 7
3 7 7
3 28 30

出力例 4

60
8
26
38
84
11
7
2
133
0
10

入力例 5

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

出力例 5

0
2
0

Score : 466 pts

Problem Statement

Takahashi works as a librarian at a library. The library has N shelves arranged in a horizontal row, numbered 1, 2, \ldots, N from left to right.

Initially, the number of books on each shelf is 0. Takahashi will perform Q operations in order. Each operation is one of the following three types:

  • Operation 1: Given integers l, r. Add 1 book to each of shelves l, l+1, \ldots, r. That is, the number of books on each shelf in this range increases by 1.
  • Operation 2: Given an integer p. Remove all books from shelf p, setting the number of books on shelf p to 0. (If the number of books is already 0, nothing happens.)
  • Operation 3: Given integers l, r. Find the total number of books currently placed on shelves l, l+1, \ldots, r.

For each Operation 3, output the answer.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • For Operations 1 and 3, 1 \leq l \leq r \leq N
  • For Operation 2, 1 \leq p \leq N
  • There is at least one Operation 3
  • All input values are integers
  • It is guaranteed that the answer at the time of each Operation 3 is between 0 and 2^{63} - 1, inclusive (i.e., it fits in a signed 64-bit integer)

Input

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
  • The first line contains an integer N representing the number of shelves and an integer Q representing the number of operations, separated by a space.
  • The following Q lines each describe one operation. The format of the i-th operation \mathrm{query}_i depends on the type of operation:
  • Operation 1: 1 l r — Add 1 book to each shelf from shelf l to shelf r. Here, l is the shelf number of the left endpoint and r is the shelf number of the right endpoint of the range.
  • Operation 2: 2 p — Remove all books from shelf p, setting the number of books to 0. Here, p is the target shelf number.
  • Operation 3: 3 l r — Find the total number of books from shelf l to shelf r. Here, l is the shelf number of the left endpoint and r is the shelf number of the right endpoint of the range.

Output

Each time Operation 3 is given, output the total number of books in the specified range at that point in time on a single line. Output the answers for all Operation 3s in the order they appear in the input.


Sample Input 1

5 7
3 1 5
1 2 4
3 1 5
1 3 5
2 3
3 2 4
3 5 5

Sample Output 1

0
3
3
1

Sample Input 2

3 8
2 2
3 1 3
1 1 1
1 1 3
3 1 1
2 1
3 1 2
3 3 3

Sample Output 2

0
2
1
1

Sample Input 3

10 16
1 1 10
1 3 7
3 1 10
2 5
3 4 6
1 5 5
1 6 10
3 5 10
2 10
1 1 3
3 1 3
2 1
2 5
3 1 10
1 2 9
3 7 9

Sample Output 3

15
4
13
7
17
10

Sample Input 4

30 30
1 1 30
1 1 15
1 16 30
3 1 30
2 1
2 30
3 1 5
1 5 25
3 20 30
2 10
2 20
1 10 20
3 10 20
1 1 1
1 30 30
3 1 30
2 15
1 14 16
3 14 16
1 2 29
2 16
3 15 17
1 1 30
3 1 1
2 1
3 1 30
1 7 7
2 7
3 7 7
3 28 30

Sample Output 4

60
8
26
38
84
11
7
2
133
0
10

Sample Input 5

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

Sample Output 5

0
2
0