A - Unsupported Type

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
XA に含まれるか判定してください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • 1 \le X \le 100

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \dots A_N
X

出力

XA に含まれるなら Yes 、含まれないなら No と出力せよ。


入力例 1

5
3 1 4 1 5
4

出力例 1

Yes

A=(3,1,4,1,5), X=4 であり、 XA に含まれます。


入力例 2

4
100 100 100 100
100

出力例 2

Yes

X が複数回 A に含まれる場合もあります。


入力例 3

6
2 3 5 7 11 13
1

出力例 3

No

Score : 100 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N and an integer X.
Determine whether X is contained in A.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • 1 \le X \le 100

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N
X

Output

If X is contained in A, print Yes; otherwise, print No.


Sample Input 1

5
3 1 4 1 5
4

Sample Output 1

Yes

We have A=(3,1,4,1,5) and X=4; X is contained in A.


Sample Input 2

4
100 100 100 100
100

Sample Output 2

Yes

X may be contained in A multiple times.


Sample Input 3

6
2 3 5 7 11 13
1

Sample Output 3

No
B - Pick Two

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

一直線状の倉庫があります。この倉庫に 偶数個 の荷物が保管されています。
倉庫の情報は文字列 S として与えらます。
倉庫は区画 1,2,\dots,|S||S| 個の区画からなり、 S の各文字は以下の情報を表します。

  • Si 文字目が # であるとき、区画 i に荷物が 1 つ置かれている。
  • Si 文字目が . であるとき、区画 i に荷物が置かれていない。

この倉庫にはロボットがあり、このロボットは以下の行動を倉庫から荷物が無くなるまで繰り返します。

  • 荷物をその荷物がある区画番号の小さい方から 2 つ回収する。回収された荷物は倉庫の外へ運び出される。

繰り返しの各回で運び出された 2 つの荷物が倉庫のどの区画にあったものかを求めてください。

制約

  • S#. からなる長さ 2 以上 1000 以下の文字列
  • 倉庫には偶数個の荷物が保管されている
  • 倉庫には 2 つ以上の荷物が保管されている

入力

入力は以下の形式で標準入力から与えられる。

S

出力

倉庫に保管されている荷物の総数を m とすると、 m/2 行出力せよ。
そのうち i 行目には、 i 回目の繰り返しで運び出された荷物が元々あった区画の番号を小さい方から順に カンマ区切り で出力せよ。


入力例 1

.#.##..##.#.###....#

出力例 1

2,4
5,8
9,11
13,14
15,20

区画 2,4,5,8,9,11,13,14,15,20 に荷物が保管されています。

  • 1 回目の繰り返しでは区画 2,4 にあった荷物が運び出されます。
  • 2 回目の繰り返しでは区画 5,8 にあった荷物が運び出されます。
  • 3 回目の繰り返しでは区画 9,11 にあった荷物が運び出されます。
  • 4 回目の繰り返しでは区画 13,14 にあった荷物が運び出されます。
  • 5 回目の繰り返しでは区画 15,20 にあった荷物が運び出されます。

Score : 200 points

Problem Statement

There is a linear warehouse, which stores an even number of packages.
The layout of the warehouse is represented by a string S.
The warehouse has |S| sections, numbered 1,2,\dots,|S|, and the string S describes them as follows:

  • When the i-th character of S is #, one package is placed in section i.
  • When the i-th character of S is ., no package is placed in section i.

The warehouse has a robot, which repeats the following action until there are no more packages in the warehouse:

  • Collect two packages from the sections with the smallest section numbers among the sections containing packages. The collected packages are carried out of the warehouse.

For each iteration, determine which sections of the warehouse the two packages that were carried out were originally located.

Constraints

  • S is a string consisting of # and . with length between 2 and 1000, inclusive.
  • The warehouse stores an even number of packages.
  • The warehouse stores at least two packages.

Input

The input is given from Standard Input in the following format:

S

Output

Let m be the total number of packages stored in the warehouse. Output m/2 lines.
The i-th line should contain the section numbers where the packages carried out in the i-th iteration were originally located, in ascending order, separated by a comma.


Sample Input 1

.#.##..##.#.###....#

Sample Output 1

2,4
5,8
9,11
13,14
15,20

Packages are stored in sections 2,4,5,8,9,11,13,14,15,20.

  • In the 1st iteration, packages from sections 2,4 are carried out.
  • In the 2nd iteration, packages from sections 5,8 are carried out.
  • In the 3rd iteration, packages from sections 9,11 are carried out.
  • In the 4th iteration, packages from sections 13,14 are carried out.
  • In the 5th iteration, packages from sections 15,20 are carried out.
C - Mixture

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

N 種類の薬品 1,2,\dots,N があります。あなたの目標はこれらが全て混ざった状態にすることです。
0, 1 からなる長さ 2^N-1 の文字列 S が与えられます。この文字列は次の情報を表します。

  • まず、 1 種類以上の薬品が混ざった状態 i ( 1 \le i \le 2^N-1 ) を次のように定義する。
    • i2 進法で表記した際に下から k ( 1 \le k \le N ) 桁目が 1 である、またその時に限り、薬品 k が含まれている。
    • 例えば、 132 進法で表記すると 1101_{(2)} となるため、状態 13 は薬品 1,3,4 が混ざった状態を表現します。
  • Si 文字目が 0 であるとき、状態 i安全 である。
  • Si 文字目が 1 であるとき、状態 i危険 である。

あなたは次の操作を使って薬品を混ぜ合わせます。

  • まず、空の瓶を用意する。
  • 次に、以下を繰り返す。
    • まだ瓶に注いでいない薬品を 1 種類選択し、瓶に注ぐ。
    • この時、瓶の中で混ざった薬品が危険な状態であってはならない。

この操作によって全ての薬品が混ざった状態を作れるか判定してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • T1 以上 40000 以下の整数
  • N1 以上 18 以下の整数
  • S0, 1 からなる長さ 2^N-1 の文字列
  • ひとつの入力に含まれる |S| = 2^N-1 の総和は 5 \times 10^5 を超えない

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_ii 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。

N
S

出力

T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。
各テストケースについて、全ての薬品が混ざった状態を作れるなら Yes 、作れないなら No と出力せよ。


入力例 1

5
3
0010000
3
0010110
1
1
2
100
4
001110010101110

出力例 1

Yes
No
No
Yes
Yes

この入力には 5 個のテストケースが含まれます。

1 番目のケースは次の通りです。

  • 3 種類の薬品が存在する。
  • 薬品 1,2 が混ざった状態 3 のみが危険で、他の状態は安全である。
  • 例えば、以下の手順で全ての薬品が混ざった状態を作ることができます。
    • はじめに、瓶に薬品 2 を注ぐ。瓶の中で薬品 2 のみが混ざっており、これは状態 2 なので安全である。
    • 次に、瓶に薬品 3 を注ぐ。瓶の中で薬品 2,3 が混ざっており、これは状態 6 なので安全である。
    • 最後に、瓶に薬品 1 を注ぐ。瓶の中で薬品 1,2,3 が混ざっており、これは状態 7 なので安全である。

2 番目のケースは次の通りです。

  • 3 種類の薬品が存在する。
  • 薬品 1,2 が混ざった状態 3 、薬品 1,3 が混ざった状態 5 、薬品 2,3 が混ざった状態 6 が危険で、他の状態は安全である。
  • このケースについて、全ての薬品が混ざった状態を作ることはできません。

3 番目のケースは次の通りです。

  • 1 種類の薬品が存在する。
  • 薬品 1 のみが混ざった状態 1 が危険であるため、全ての薬品が混ざった状態を作ることはできません。

Score : 350 points

Problem Statement

There are N types of chemicals 1,2,\dots,N. Your goal is to create a state where all of them are mixed.
You are given a string S of length 2^N-1 consisting of 0 and 1, which represents the following information:

  • First, define state i (1 \le i \le 2^N-1) where one or more types of chemicals are mixed as follows:
    • When the k-th digit (1 \le k \le N) from the bottom in the binary representation of i is 1, and only then, chemical k is included.
    • For example, 13 in binary is 1101_{(2)}, so state 13 represents a state where chemicals 1,3,4 are mixed.
  • When the i-th character of S is 0, state i is safe.
  • When the i-th character of S is 1, state i is dangerous.

You mix chemicals using the following operation:

  • First, prepare an empty bottle.
  • Next, repeat the following:
    • Choose one type of chemical that has not yet been poured into the bottle and pour it into the bottle.
    • At this time, the chemicals mixed in the bottle must not be in a dangerous state.

Determine whether this operation can create a state where all chemicals are mixed.

You are given T test cases; solve each of them.

Constraints

  • T is an integer between 1 and 40000, inclusive.
  • N is an integer between 1 and 18, inclusive.
  • S is a string of length 2^N-1 consisting of 0 and 1.
  • The sum of |S| = 2^N-1 in each input does not exceed 5 \times 10^5.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

N
S

Output

Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if it is possible to create a state where all chemicals are mixed, print Yes; otherwise, print No.


Sample Input 1

5
3
0010000
3
0010110
1
1
2
100
4
001110010101110

Sample Output 1

Yes
No
No
Yes
Yes

This input contains five test cases.

The 1st case is as follows:

  • There are three types of chemicals.
  • Only state 3 where chemicals 1,2 are mixed is dangerous, and the other states are safe.
  • For example, you can create a state where all chemicals are mixed with the following procedure:
    • First, pour chemical 2 into the bottle. Only chemical 2 is mixed in the bottle, which is state 2, so it is safe.
    • Next, pour chemical 3 into the bottle. Chemicals 2,3 are mixed in the bottle, which is state 6, so it is safe.
    • Finally, pour chemical 1 into the bottle. Chemicals 1,2,3 are mixed in the bottle, which is state 7, so it is safe.

The 2nd case is as follows:

  • There are three types of chemicals.
  • State 3 where chemicals 1,2 are mixed, state 5 where chemicals 1,3 are mixed, and state 6 where chemicals 2,3 are mixed are dangerous, and the other states are safe.
  • For this case, it is impossible to create a state where all chemicals are mixed.

The 3rd case is as follows:

  • There is one type of chemical.
  • Since state 1 where only chemical 1 is mixed is dangerous, it is impossible to create a state where all chemicals are mixed.
D - Get Many Stickers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

本問題の設定は G 問題と類似しています。G 問題とは、最大化の対象および制約の一部が異なります。

不思議なコーラショップがあります。 このショップは直接コーラを売ることはしませんが、コーラの空き瓶と新しい瓶入りコーラとの交換サービスを提供しています。

はじめ、高橋君は瓶入りコーラを N 本持っており、これから以下のいずれかの行動を選んで取ることを好きな回数繰り返すことができます(0 回でもよい)。

  • 持っている瓶入りコーラ 1 本を飲む。持っている瓶入りコーラの本数が 1 減り、空き瓶の本数が 1 増える。 (行動前に 1 本も瓶入りコーラを持っていない場合、この行動は取れない。)
  • 1 以上 M 以下の整数 i を選ぶ。コーラショップに A_i 本の空き瓶を渡し、引き換えに B_i 本の瓶入りコーラおよび記念のシール 1 枚をもらう。 (行動前に持っている空き瓶の本数が A_i 未満であるような i は選ぶことができない。 選ぶことのできる i が存在しない場合、この行動は取れない。)

高橋君はシールが大好きです。うまく行動を取ったとき、最大で何枚のシールを手に入れることができますか?

なお、高橋君は最初空き瓶を 1 本も持っておらず、シールも 1 枚も持っていないものとします。

制約

  • 1\leq N \leq 10^{18}
  • 1\leq M \leq 2\times 10^5
  • 1\leq B_i < A_i \leq 10^{18}
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

答えを整数として出力せよ。


入力例 1

5 3
5 1
4 3
3 1

出力例 1

3

以下のような行動を考えます。

  • 最初、高橋君は瓶入りコーラを 5 本持っている。
  • 持っている瓶入りコーラ 1 本を飲むことを 5 回繰り返す。高橋君は空き瓶を 5 本持った状態になる。
  • i=2 として交換を行い、4 本の空き瓶と引き換えに 3 本の瓶入りコーラと 1 枚のシールをもらう。高橋君は瓶入りコーラを 3 本、空き瓶を 1 本、シールを 1 枚持った状態になる。
  • 持っている瓶入りコーラ 1 本を飲むことを 3 回繰り返す。高橋君は空き瓶を 4 本、シールを 1 枚持った状態になる。
  • i=2 として交換を行い、4 本の空き瓶と引き換えに 3 本の瓶入りコーラと 1 枚のシールをもらう。高橋君は瓶入りコーラを 3 本、シールを 2 枚持った状態になる。
  • 持っている瓶入りコーラ 1 本を飲むことを 3 回繰り返す。高橋君は空き瓶を 3 本、シールを 2 枚持った状態になる。
  • i=3 として交換を行い、3 本の空き瓶と引き換えに 1 本の瓶入りコーラと 1 枚のシールをもらう。高橋君は瓶入りコーラを 1 本、シールを 3 枚持った状態になる。

これらの行動が終了したあと、高橋君は 3 枚のシールを持っています。 高橋君がどのように行動を取っても 4 枚以上のシールを手に入れることはできないため、答えは 3 です。


入力例 2

3 3
5 1
5 1
4 2

出力例 2

0

元々持っている瓶入りコーラを飲むことしかできません。


入力例 3

415 8
327 299
413 396
99 67
108 51
195 98
262 180
250 175
234 187

出力例 3

11

Score : 400 points

Problem Statement

The setting of this problem is similar to Problem G. It differs from Problem G in the target of maximization and some constraints.

There is a mysterious cola shop. This shop does not sell cola directly, but provides an exchange service between empty cola bottles and new bottled cola.

Initially, Takahashi has N bottled colas, and from now on he can choose and take one of the following actions any number of times (possibly zero):

  • Drink 1 bottled cola that he has. The number of bottled colas he has decreases by 1, and the number of empty bottles increases by 1. (If he has no bottled cola before the action, he cannot take this action.)
  • Choose an integer i between 1 and M, inclusive. Give A_i empty bottles to the cola shop, and receive B_i bottled colas and 1 commemorative sticker in return. (He cannot choose i such that the number of empty bottles he has before the action is less than A_i. If there is no i that can be chosen, he cannot take this action.)

Takahashi loves stickers. When he acts optimally, what is the maximum number of stickers he can obtain?

He initially has no empty bottles and no stickers.

Constraints

  • 1\leq N \leq 10^{18}
  • 1\leq M \leq 2\times 10^5
  • 1\leq B_i < A_i \leq 10^{18}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Output the answer as an integer.


Sample Input 1

5 3
5 1
4 3
3 1

Sample Output 1

3

Consider the following actions:

  • Initially, Takahashi has 5 bottled colas.
  • Repeat drinking 1 bottled cola 5 times. Takahashi has 5 empty bottles.
  • Make an exchange with i=2, giving 4 empty bottles in exchange for 3 bottled colas and 1 sticker. Takahashi has 3 bottled colas, 1 empty bottle, and 1 sticker.
  • Repeat drinking 1 bottled cola 3 times. Takahashi has 4 empty bottles and 1 sticker.
  • Make an exchange with i=2, giving 4 empty bottles in exchange for 3 bottled colas and 1 sticker. Takahashi has 3 bottled colas and 2 stickers.
  • Repeat drinking 1 bottled cola 3 times. Takahashi has 3 empty bottles and 2 stickers.
  • Make an exchange with i=3, giving 3 empty bottles in exchange for 1 bottled cola and 1 sticker. Takahashi has 1 bottled cola and 3 stickers.

After these actions are completed, Takahashi has 3 stickers. No matter how he acts, he cannot obtain 4 or more stickers, so the answer is 3.


Sample Input 2

3 3
5 1
5 1
4 2

Sample Output 2

0

He can only drink the bottled colas he originally had.


Sample Input 3

415 8
327 299
413 396
99 67
108 51
195 98
262 180
250 175
234 187

Sample Output 3

11
E - Hungry Takahashi

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

HW 列のグリッドがあります。 上から i 行目、左から j 列目のマスのことを (i,j) と表記します。 各マスには何枚かのコインが置かれており、マス (i,j) に置かれているコインは A_{i,j} 枚です。

高橋君ははじめマス (1,1) におり、x 枚のコインを持っています。 これから H+W-1 日間にかけていくつかの出来事が起こります。 k\ (1\leq k\leq H+W-1) 日目には以下のことが順に起こります:

  1. 高橋君が、そのときいるマスに置かれたコインを全て回収する。
  2. お腹の空いた高橋君が、手持ちのコインを P_k 枚消費してご飯を買う。ただし、所持しているコインが P_k 枚未満の場合ご飯を買うことはできず、空腹のあまり倒れてしまう。
  3. k < H+W-1 ならば、高橋君が、そのときいるマスの 1 つ右または 1 つ下のいずれかのマスを選んでそこに移動する。ただし、グリッドの外に出てしまうような移動はできない。k=H+W-1 ならば、何もしない。

高橋君が一度も空腹で倒れることなくこれからの H+W-1 日間を終えられるような方法が存在するとき、 高橋君がはじめに持っているコインの枚数 x としてありうる最小の非負整数を求めてください。

制約

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

出力

答えを出力せよ。


入力例 1

2 2
3 2
4 1
1 3 6

出力例 1

2

x=2 のとき、高橋君は次のように行動することで、一度も空腹で倒れずに済みます。

  • 最初、マス (1,1) におり、2 枚のコインを所持している。
  • 1 日目:
    1. マス (1,1) に置かれたコイン 3 枚を回収し、手持ちのコインが 5 枚になる。
    2. コインを 1 枚消費してご飯を買い、手持ちのコインが 4 枚になる。
    3. 1 つ下にあるマス (2,1) に移動する。
  • 2 日目:
    1. マス (2,1) に置かれたコイン 4 枚を回収し、手持ちのコインが 8 枚になる。
    2. コインを 3 枚消費してご飯を買い、手持ちのコインが 5 枚になる。
    3. 1 つ右にあるマス (2,2) に移動する。
  • 3 日目:
    1. マス (2,2) に置かれたコイン 1 枚を回収し、手持ちのコインが 6 枚になる。
    2. コインを 6 枚消費してご飯を買い、手持ちのコインが 0 枚になる。

x1 以下のとき、高橋君はどのように行動してもいずれかのタイミングで空腹で倒れてしまいます。 よって答えは 2 です。


入力例 2

1 1
5
3

出力例 2

0

最初に 1 枚もコインを持っていなかったとしても、高橋君は空腹で倒れることはありません。


入力例 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

出力例 3

20

Score : 450 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. Some coins are placed on each cell, and there are A_{i,j} coins on cell (i,j).

Takahashi is initially at cell (1,1) and has x coins. Over the next H+W-1 days, several events will occur. On the k-th day (1\leq k\leq H+W-1), the following things happen in order:

  1. Takahashi collects all the coins placed on the cell where he is currently located.
  2. Hungry Takahashi consumes P_k coins from his hand to buy food. However, if he has fewer than P_k coins, he cannot buy food and collapses from hunger.
  3. If k < H+W-1, Takahashi moves either one cell right or one cell down. He cannot leave the grid. If k=H+W-1, he does nothing.

When there exists a way for Takahashi to finish the next H+W-1 days without ever collapsing from hunger, find the minimum non-negative integer x that can be the number of coins Takahashi initially has.

Constraints

  • H,W\geq 1
  • H\times W \leq 2\times 10^5
  • 1\leq A_{i,j}\leq 10^9
  • 1\leq P_k\leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

H W
A_{1,1} A_{1,2} \dots A_{1,W}
A_{2,1} A_{2,2} \dots A_{2,W}
\vdots
A_{H,1} A_{H,2} \dots A_{H,W}
P_1 P_2 \dots P_{H+W-1}

Output

Output the answer.


Sample Input 1

2 2
3 2
4 1
1 3 6

Sample Output 1

2

When x=2, Takahashi can act as follows to avoid collapsing from hunger:

  • Initially, he is at cell (1,1) and has 2 coins.
  • Day 1:
    1. He collects 3 coins placed on cell (1,1), so he has 5 coins.
    2. He consumes 1 coin to buy food, so he has 4 coins.
    3. He moves to cell (2,1) which is 1 below.
  • Day 2:
    1. He collects 4 coins placed on cell (2,1), so he has 8 coins.
    2. He consumes 3 coins to buy food, so he has 5 coins.
    3. He moves to cell (2,2) which is 1 to the right.
  • Day 3:
    1. He collects 1 coin placed on cell (2,2), so he has 6 coins.
    2. He consumes 6 coins to buy food, so he has 0 coins.

When x is 1 or less, Takahashi will collapse from hunger at some point no matter how he acts. Therefore, the answer is 2.


Sample Input 2

1 1
5
3

Sample Output 2

0

Even if Takahashi initially has no coins, he will not collapse from hunger.


Sample Input 3

4 7
35 29 36 88 58 15 25
99 7 49 61 67 4 57
96 92 3 49 49 36 90
72 89 40 44 24 53 45
55 43 23 71 77 6 94 19 27 21

Sample Output 3

20
F - Max Combo

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ N の英小文字からなる文字列 S があります。以下のクエリを全部で Q 個処理してください。

  • タイプ 1 : Si 文字目を x に変更する。
  • タイプ 2 : 現時点の Sl 文字目から r 文字目までを抜き出した文字列を t とする。この文字列に対して次のように定義される f(t) を求めよ。
    • f(t)t 中の同じ文字の連続の最大長である。
    • 厳密には、 1 \le a \le b \le |t| かつ ta 文字目から b 文字目までが全て等しいような整数 a,b を選ぶとき、 f(t)b-a+1 の値としてありうる最大値である。
    • 例えば f( aaaccbbbb )=4f( bbaaabb )=3f( x )=1 である。

制約

  • N1 以上 5 \times 10^5 以下の整数
  • S は英小文字からなる長さ N の文字列
  • Q1 以上 5 \times 10^5 以下の整数
  • タイプ 1 のクエリは以下の制約を満たす
    • i1 以上 N 以下の整数
    • x は英小文字
  • タイプ 2 のクエリは以下の制約を満たす
    • l,r1 \le l \le r \le N を満たす整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
S
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q

但し、 \rm{Query}_ii 個目のクエリを表す。

タイプ 1 のクエリは以下の形式で与えられる。

1 i x

タイプ 2 のクエリは以下の形式で与えられる。

2 l r

出力

タイプ 2 のクエリが現れる度に、その解答を 1 行に出力せよ。
なお、入出力が大きくなる場合があるので、高速な方法で入出力を行うことを推奨する。


入力例 1

10 5
babaacczcc
2 1 4
1 3 a
2 1 10
1 8 c
2 1 10

出力例 1

1
4
5

この入力には 5 個のクエリが含まれます。

  • はじめ、文字列 Sbabaacczcc です。
  • 1 番目のクエリはタイプ 2 で、 l=1,r=4 です。
    • 抜き出した文字列は baba であり、 f( baba )=1 です。
  • 2 番目のクエリはタイプ 1 で、 i=3,x= a です。
    • 変更後の文字列 S= baaaacczcc です。
  • 3 番目のクエリはタイプ 2 で、 l=1,r=10 です。
    • 抜き出した文字列は baaaacczcc であり、 f( baaaacczcc )=4 です。
  • 4 番目のクエリはタイプ 1 で、 i=8,x= c です。
    • 変更後の文字列 S= baaaaccccc です。
  • 5 番目のクエリはタイプ 2 で、 l=1,r=10 です。
    • 抜き出した文字列は baaaaccccc であり、 f( baaaaccccc )=5 です。

入力例 2

1 1
a
1 1 z

出力例 2


出力が空である場合もあります。

Score : 525 points

Problem Statement

There is a string S of length N consisting of lowercase English letters. Process a total of Q queries as follows:

  • Type 1 : Change the i-th character of S to x.
  • Type 2 : Let t be the substring of the current S from the l-th character through the r-th character. Find f(t) defined as follows for this string:
    • f(t) is the maximum length of consecutive identical characters in t.
    • More precisely, when choosing integers a,b such that 1 \le a \le b \le |t| and all characters from the a-th through the b-th character of t are equal, f(t) is the maximum possible value of b-a+1.
    • For example, f( aaaccbbbb )=4, f( bbaaabb )=3, f( x )=1.

Constraints

  • N is an integer between 1 and 5 \times 10^5, inclusive.
  • S is a string of length N consisting of lowercase English letters.
  • Q is an integer between 1 and 5 \times 10^5, inclusive.
  • Type 1 queries satisfy the following constraints:
    • i is an integer between 1 and N, inclusive.
    • x is a lowercase English letter.
  • Type 2 queries satisfy the following constraints:
    • l,r are integers satisfying 1 \le l \le r \le N.

Input

The input is given from Standard Input in the following format:

N Q
S
\rm{Query}_1
\rm{Query}_2
\vdots
\rm{Query}_Q

Here, \rm{Query}_i represents the i-th query.

Type 1 queries are given in the following format:

1 i x

Type 2 queries are given in the following format:

2 l r

Output

Every time a type 2 query appears, output the answer on one line.
The use of fast input and output methods is recommended because of potentially large input and output.


Sample Input 1

10 5
babaacczcc
2 1 4
1 3 a
2 1 10
1 8 c
2 1 10

Sample Output 1

1
4
5

This input contains five queries.

  • Initially, the string S is babaacczcc.
  • The 1st query is type 2 with l=1,r=4.
    • The extracted string is baba, and f( baba )=1.
  • The 2nd query is type 1 with i=3,x= a.
    • The string S after the change is baaaacczcc.
  • The 3rd query is type 2 with l=1,r=10.
    • The extracted string is baaaacczcc, and f( baaaacczcc )=4.
  • The 4th query is type 1 with i=8,x= c.
    • The string S after the change is baaaaccccc.
  • The 5th query is type 2 with l=1,r=10.
    • The extracted string is baaaaccccc, and f( baaaaccccc )=5.

Sample Input 2

1 1
a
1 1 z

Sample Output 2


The output may be empty.

G - Get Many Cola

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

本問題の設定は D 問題と類似しています。D 問題とは、最大化の対象および制約の一部が異なります。

不思議なコーラショップがあります。 このショップは直接コーラを売ることはしませんが、コーラの空き瓶と新しい瓶入りコーラとの交換サービスを提供しています。

はじめ、高橋君は瓶入りコーラを N 本持っており、これから以下のいずれかの行動を選んで取ることを好きな回数繰り返すことができます(0 回でもよい)。

  • 持っている瓶入りコーラ 1 本を飲む。持っている瓶入りコーラの本数が 1 減り、空き瓶の本数が 1 増える。 (行動前に 1 本も瓶入りコーラを持っていない場合、この行動は取れない。)
  • 1 以上 M 以下の整数 i を選ぶ。コーラショップに A_i 本の空き瓶を渡し、引き換えに B_i 本の瓶入りコーラをもらう。 (行動前に持っている空き瓶の本数が A_i 未満であるような i は選ぶことができない。 選ぶことのできる i が存在しない場合、この行動は取れない。)

高橋君はコーラが大好きです。うまく行動を取ったとき、最大で何本のコーラを飲むことができますか?

なお、高橋君は最初空き瓶を 1 本も持っていないものとします。

制約

  • 1\leq N \leq 10^{15}
  • 1\leq M \leq 2\times 10^5
  • 1\leq B_i < A_i \leq 300
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

答えを整数として出力せよ。


入力例 1

5 3
5 4
5 1
4 2

出力例 1

11

以下のような行動を考えます。

  • 最初、高橋君は瓶入りコーラを 5 本持っている。
  • 持っている瓶入りコーラ 1 本を飲むことを 5 回繰り返す。高橋君は空き瓶を 5 本持った状態になる。
  • i=1 として交換を行い、5 本の空き瓶と引き換えに 4 本の瓶入りコーラをもらう。高橋君は瓶入りコーラを 4 本持った状態になる。
  • 持っている瓶入りコーラ 1 本を飲むことを 4 回繰り返す。高橋君は空き瓶を 4 本持った状態になる。
  • i=3 として交換を行い、4 本の空き瓶と引き換えに 2 本の瓶入りコーラをもらう。高橋君は瓶入りコーラを 2 本持った状態になる。
  • 持っている瓶入りコーラ 1 本を飲むことを 2 回繰り返す。高橋君は空き瓶を 2 本持った状態になる。

このとき、高橋君は合計で 5+4+2=11 本のコーラを飲むことができます。 高橋君がどのように行動を取っても 12 本以上のコーラを飲むことはできないため、答えは 11 です。


入力例 2

3 3
5 1
4 2
5 1

出力例 2

3

元々持っている瓶入りコーラを飲むことしかできません。


入力例 3

925532735634776 8
91 40
273 265
286 153
155 126
92 83
291 228
216 90
234 9

出力例 3

31583804603529464

Score : 575 points

Problem Statement

The setting of this problem is similar to Problem D. It differs from Problem D in the target of maximization and some constraints.

There is a mysterious cola shop. This shop does not sell cola directly, but provides an exchange service between empty cola bottles and new bottled cola.

Initially, Takahashi has N bottled colas, and from now on he can choose and take one of the following actions any number of times (possibly zero):

  • Drink 1 bottled cola that he has. The number of bottled colas he has decreases by 1, and the number of empty bottles increases by 1. (If he has no bottled cola before the action, he cannot take this action.)
  • Choose an integer i between 1 and M, inclusive. Give A_i empty bottles to the cola shop, and receive B_i bottled colas in return. (He cannot choose i such that the number of empty bottles he has before the action is less than A_i. If there is no i that can be chosen, he cannot take this action.)

Takahashi loves cola. When he acts optimally, what is the maximum number of colas he can drink?

He initially has no empty bottles.

Constraints

  • 1\leq N \leq 10^{15}
  • 1\leq M \leq 2\times 10^5
  • 1\leq B_i < A_i \leq 300
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Output the answer as an integer.


Sample Input 1

5 3
5 4
5 1
4 2

Sample Output 1

11

Consider the following actions:

  • Initially, Takahashi has 5 bottled colas.
  • Repeat drinking 1 bottled cola 5 times. Takahashi has 5 empty bottles.
  • Make an exchange with i=1, giving 5 empty bottles in exchange for 4 bottled colas. Takahashi has 4 bottled colas.
  • Repeat drinking 1 bottled cola 4 times. Takahashi has 4 empty bottles.
  • Make an exchange with i=3, giving 4 empty bottles in exchange for 2 bottled colas. Takahashi has 2 bottled colas.
  • Repeat drinking 1 bottled cola 2 times. Takahashi has 2 empty bottles.

At this point, Takahashi has drunk a total of 5+4+2=11 colas. No matter how he acts, he cannot drink 12 or more colas, so the answer is 11.


Sample Input 2

3 3
5 1
4 2
5 1

Sample Output 2

3

He can only drink the bottled colas he originally had.


Sample Input 3

925532735634776 8
91 40
273 265
286 153
155 126
92 83
291 228
216 90
234 9

Sample Output 3

31583804603529464