A - Range Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N) および正整数 P,Q,R,S が与えられます。
ここで、P,Q,R,S は、1\leq P\leq Q<R\leq S \leq N および Q-P=S-R をみたしています。

数列 AP 番目から Q 番目の項までと R 番目から S 番目の項までを入れ替えた数列を B=(B_1, B_2,\ldots, B_N) とします。
数列 B を出力してください。

制約

  • 1\leq N \leq 100
  • 1\leq A_i\leq 100
  • 1\leq P\leq Q<R\leq S \leq N
  • Q-P=S-R
  • 入力はすべて整数

入力

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

N P Q R S
A_1 A_2 \ldots A_N

出力

B_1, B_2,\ldots, B_N を空白区切りで出力せよ。


入力例 1

8 1 3 5 7
1 2 3 4 5 6 7 8

出力例 1

5 6 7 4 1 2 3 8

数列 A=(1,2,3,4,5,6,7,8)1 番目から 3 番目の項 (1,2,3)5 番目から 7 番目までの項 (5,6,7) を 入れ替えると, B=(5,6,7,4,1,2,3,8) となります。 よってこれを空白区切りで出力します。


入力例 2

5 2 3 4 5
2 2 1 1 1

出力例 2

2 1 1 2 1

数列には同じ整数が複数回現れる事もあります。


入力例 3

2 1 1 2 2
50 100

出力例 3

100 50

入力例 4

10 2 4 7 9
22 75 26 45 72 81 47 29 97 2

出力例 4

22 47 29 97 72 81 75 26 45 2

Score : 100 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N and positive integers P,Q,R, and S.
Here, P,Q,R, and S satisfy 1\leq P\leq Q<R\leq S \leq N and Q-P=S-R.

Let B=(B_1, B_2,\ldots, B_N) be the sequence obtained by swapping the P-th through Q-th terms and the R-th through S-th terms of A.
Print the sequence B.

Constraints

  • 1\leq N \leq 100
  • 1\leq A_i\leq 100
  • 1\leq P\leq Q<R\leq S \leq N
  • Q-P=S-R
  • All values in the input are integers.

Input

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

N P Q R S
A_1 A_2 \ldots A_N

Output

Print B_1, B_2,\ldots, B_N, with spaces in between.


Sample Input 1

8 1 3 5 7
1 2 3 4 5 6 7 8

Sample Output 1

5 6 7 4 1 2 3 8

Swapping the 1-st through 3-rd terms (1,2,3) and the 5-th through 7-th terms (5,6,7) of the sequence A=(1,2,3,4,5,6,7,8) results in B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.


Sample Input 2

5 2 3 4 5
2 2 1 1 1

Sample Output 2

2 1 1 2 1

The same integer may occur multiple times in the sequence.


Sample Input 3

2 1 1 2 2
50 100

Sample Output 3

100 50

Sample Input 4

10 2 4 7 9
22 75 26 45 72 81 47 29 97 2

Sample Output 4

22 47 29 97 72 81 75 26 45 2
B - Glutton Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君は N 個の料理を食べようとしています。

i 番目に食べようとしている料理は、S_i = sweet のとき甘い料理であり、S_i = salty のとき塩辛い料理です。

高橋君は甘い料理を 2 つ連続で食べると気持ち悪くなってしまい、その後料理が食べられなくなってしまいます。

高橋君がすべての料理を食べることができるか判定してください。

制約

  • N1 以上 100 以下の整数
  • S_isweet または salty

入力

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

N
S_1
S_2
\vdots
S_N

出力

高橋君がすべての料理を食べることができるならば Yes を、できないならば No を出力せよ。


入力例 1

5
salty
sweet
salty
salty
sweet

出力例 1

Yes

高橋君は甘い料理を 2 つ連続で食べることがないので、気持ち悪くなることなくすべての料理を食べることができます。


入力例 2

4
sweet
salty
sweet
sweet

出力例 2

Yes

高橋君は気持ち悪くなってしまいますが、すべての料理を食べることができます。


入力例 3

6
salty
sweet
sweet
salty
sweet
sweet

出力例 3

No

高橋君は 3 番目の料理を食べると気持ち悪くなってしまい、4 番目以降の料理が食べられなくなります。

Score : 100 points

Problem Statement

Takahashi is planning to eat N dishes.

The i-th dish he plans to eat is sweet if S_i = sweet, and salty if S_i = salty.

If he eats two sweet dishes consecutively, he will feel sick and be unable to eat any more dishes.

Determine whether he can eat all the dishes.

Constraints

  • N is an integer between 1 and 100, inclusive.
  • Each S_i is sweet or salty.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if Takahashi can eat all the dishes, and No otherwise.


Sample Input 1

5
salty
sweet
salty
salty
sweet

Sample Output 1

Yes

He will not eat two sweet dishes consecutively, so he can eat all the dishes without feeling sick.


Sample Input 2

4
sweet
salty
sweet
sweet

Sample Output 2

Yes

He will feel sick but can still eat all the dishes.


Sample Input 3

6
salty
sweet
sweet
salty
sweet
sweet

Sample Output 3

No

He feels sick when eating the 3rd dish and cannot eat the 4th and subsequent dishes.

C - カップリング選抜

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

アイドルグループ Bit♡Beat では、新曲のカップリングとしてメンバーの中から 2 人のユニット(選抜)を組むことになりました。

グループには N 人のメンバーがおり、それぞれのメンバーにスキル値が定まっています。 i 人目のメンバー (1\le i\le N) のスキル値は A_i です。

プロデューサーであるあなたは、スキル値の合計が ちょうど S になるような 2 人のメンバーの選び方を探しています。

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか求めてください。

制約

  • 2\le N\le 5\times 10^5
  • 1\le S \le 10^9
  • 1\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

N S
A_1 A_2 \ldots A_N

出力

スキル値の合計がちょうど S になるような 2 人のメンバーの選び方が何通りあるか出力せよ。


入力例 1

5 11
5 6 9 2 2

出力例 1

3

1 人目と 2 人目がユニットを組むと、スキル値の合計は 5+6=11 となります。

このほかにも、 3 人目と 4 人目、3 人目と 5 人目がユニットを組んでも条件を満たします。

条件を満たす 2 人のメンバーの選び方は 3 通りであるため、 3 を出力してください。


入力例 2

10 1000000000
1 1 1 1 1 1 1 1 1 1

出力例 2

0

条件を満たす 2 人のメンバーの選び方が存在しない場合もあります。


入力例 3

6 14
7 7 7 7 7 7

出力例 3

15

入力例 4

15 8
3 4 10 9 1 1 1 7 8 9 9 3 8 9 7

出力例 4

6
D - cat

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が与えられます。ここで、文字列の長さはそれぞれ相異なります。

これらの文字列を長さの昇順に並べ替え、この順に結合して得られる文字列を求めてください。

制約

  • 2 \leq N \leq 50
  • N は整数
  • S_i は長さ 1 以上 50 以下の英小文字からなる文字列
  • i \neq j のとき S_iS_j の長さは異なる

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

3
tc
oder
a

出力例 1

atcoder

( tc, oder, a ) を文字列の長さの昇順に並べ替えると ( a, tc, oder ) となります。これらの文字列を順に結合すると文字列 atcoder が得られます。


入力例 2

4
cat
enate
on
c

出力例 2

concatenate

Score : 200 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N, each consisting of lowercase English letters. The lengths of these strings are all distinct.

Sort these strings in ascending order of length, and then concatenate them in that order to form a single string.

Constraints

  • 2 \leq N \leq 50
  • N is an integer.
  • Each S_i is a string consisting of lowercase English letters with length between 1 and 50, inclusive.
  • If i \neq j, the length of S_i is different from the length of S_j.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

3
tc
oder
a

Sample Output 1

atcoder

When we sort (tc, oder, a) in ascending order of length, we get (a, tc, oder). Concatenating them in this order yields the string atcoder.


Sample Input 2

4
cat
enate
on
c

Sample Output 2

concatenate
E - 1111gal password

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

整数 N が与えられるので、以下の条件を全て満たす整数 X の個数を 998244353 で割った余りを求めてください。

  • N 桁の正整数である。
  • X の各桁を上の位から順に X_1,X_2,\dots,X_N とする。このとき以下の条件を全て満たす。
    • 全ての整数 1 \le i \le N に対して、 1 \le X_i \le 9
    • 全ての整数 1 \le i \le N-1 に対して、 |X_i-X_{i+1}| \le 1

制約

  • N は整数
  • 2 \le N \le 10^6

入力

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

N

出力

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


入力例 1

4

出力例 1

203

4 桁の整数として、例えば 1111,1234,7878,6545 が問題文中の条件を満たします。


入力例 2

2

出力例 2

25

入力例 3

1000000

出力例 3

248860093

998244353 で割った余りを求めることに注意してください。

Score : 300 points

Problem Statement

Given an integer N, find the number of integers X that satisfy all of the following conditions, modulo 998244353.

  • X is an N-digit positive integer.
  • Let X_1,X_2,\dots,X_N be the digits of X from top to bottom. They satisfy all of the following:
    • 1 \le X_i \le 9 for all integers 1 \le i \le N;
    • |X_i-X_{i+1}| \le 1 for all integers 1 \le i \le N-1.

Constraints

  • N is an integer.
  • 2 \le N \le 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

4

Sample Output 1

203

Some of the 4-digit integers satisfying the conditions are 1111,1234,7878,6545.


Sample Input 2

2

Sample Output 2

25

Sample Input 3

1000000

Sample Output 3

248860093

Be sure to find the count modulo 998244353.

F - Neo-lexicographic Ordering

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 王国を治める高橋君は、英小文字のアルファベット順を変更することにしました。

新たなアルファベット順はa , b , \ldots, z を並べ替えて得られる文字列 X を用いて表されます。Xi \, (1 \leq i \leq 26) 文字目は、新たな順番において i 番目に小さい英小文字を表します。

AtCoder 王国には N 人の国民がおり、それぞれの国民の名前は S_1, S_2, \dots, S_N です。ここで、S_i \, (1 \leq i \leq N) は英小文字からなります。
これらの名前を、高橋君の定めたアルファベット順に基づく辞書順に従って並べ替えてください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、S_jT_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • Xa , b , \ldots, z を並べ替えて得られる
  • 2 \leq N \leq 50000
  • N は整数
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i は英小文字からなる (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

入力

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

X
N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、高橋君の定めたアルファベット順に基づく辞書順に従って国民の名前を並べ替えたとき、i 番目に小さいものを出力すること。


入力例 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

出力例 1

bzz
bzy
abx
caa

高橋君が新たに設定したアルファベット順において、ba より小さく、zy より小さいです。そのため、国民の名前を辞書順に並べ替えると、小さい順に bzz , bzy , abx , caa となります。


入力例 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

出力例 2

b
a
ac
ab
abc

Score : 300 points

Problem Statement

Takahashi, who governs the Kingdom of AtCoder, has decided to change the alphabetical order of English lowercase letters.

The new alphabetical order is represented by a string X, which is a permutation of a, b, \ldots, z. The i-th character of X (1 \leq i \leq 26) would be the i-th smallest English lowercase letter in the new order.

The kingdom has N citizens, whose names are S_1, S_2, \dots, S_N, where each S_i (1 \leq i \leq N) consists of lowercase English letters.
Sort these names lexicographically according to the alphabetical order decided by Takahashi.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Constraints

  • X is a permutation of a, b, \ldots, z.
  • 2 \leq N \leq 50000
  • N is an integer.
  • 1 \leq |S_i| \leq 10 \, (1 \leq i \leq N)
  • S_i consists of lowercase English letters. (1 \leq i \leq N)
  • S_i \neq S_j (1 \leq i \lt j \leq N)

Input

Input is given from Standard Input in the following format:

X
N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the i-th smallest name when the citizens' names are sorted according to the alphabetical order decided by Takahashi.


Sample Input 1

bacdefghijklmnopqrstuvwxzy
4
abx
bzz
bzy
caa

Sample Output 1

bzz
bzy
abx
caa

In the new alphabetical order set by Takahashi, b is smaller than a and z is smaller than y. Thus, sorting the citizens' names lexicographically would result in bzz, bzy, abx, caa in ascending order.


Sample Input 2

zyxwvutsrqponmlkjihgfedcba
5
a
ab
abc
ac
b

Sample Output 2

b
a
ac
ab
abc
G - XNOR Operation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

この問題は G 問題の部分問題です。

01 からなる空でない文字列 S が次の条件を満たす時、S を美しい文字列と呼びます。

  • (条件) 次の一連の操作を S の長さが 1 になるまで行い、S に残った唯一の文字を 1 にすることができる。
    1. 1 \leq i \leq |S| - 1 を満たす整数 i を自由に選ぶ。
    2. 整数 x を次のように定義する。
      • S_i = 0 かつ S_{i+1}= 0 である場合、x = 1 とする。
      • S_i = 0 かつ S_{i+1}= 1 である場合、x = 0 とする。
      • S_i = 1 かつ S_{i+1}= 0 である場合、x = 0 とする。
      • S_i = 1 かつ S_{i+1}= 1 である場合、x = 1 とする。
    3. S_iS_{i+1} を取り除き、それらがあった場所に x を数字とみなしたものを 1 個挿入する。
      例えば S= 10101 に対して i=2 を選んで操作を行った場合、操作後の文字列は 1001 になる。

01 からなる長さ N の文字列 T があります。
T の部分文字列である美しい文字列の個数を求めてください。ただし、2 つの部分文字列が文字列として同じでも、取り出す位置が異なるならば別々に数えます。

部分文字列とは S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、10101 の部分文字列ですが、11101 の部分文字列ではありません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • N は整数
  • T01 からなる長さ N の文字列

入力

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

N
T

出力

T の部分文字列である美しい文字列の個数を出力せよ。


入力例 1

3
110

出力例 1

3

T1 文字目から 2 文字目までを取り出してできる文字列 11 は美しい文字列です。なぜならば、i=1 として操作を行うと、操作後の文字列は 1 になるからです。
T の部分文字列である美しい文字列は次の 3 個です。

  • T1 文字目を取り出してできる文字列 1
  • T2 文字目を取り出してできる文字列 1
  • T1 文字目から 2 文字目までを取り出してできる文字列 11

入力例 2

4
0000

出力例 2

4

入力例 3

30
011011100101110111100010011010

出力例 3

225

Score : 425 points

Problem Statement

This problem is a subproblem of Problem G.

A non-empty string S consisting of 0 and 1 is called a beautiful string when it satisfies the following condition:

  • (Condition) You can perform the following sequence of operations until the length of S becomes 1 and make the only character remaining in S be 1.
    1. Choose any integer i satisfying 1 \leq i \leq |S| - 1.
    2. Define an integer x as follows:
      • If S_i = 0 and S_{i+1} = 0, let x = 1.
      • If S_i = 0 and S_{i+1} = 1, let x = 0.
      • If S_i = 1 and S_{i+1} = 0, let x = 0.
      • If S_i = 1 and S_{i+1} = 1, let x = 1.
    3. Remove S_i and S_{i+1}, and insert the digit corresponding to x in their place.
      For example, if S= 10101 and you choose i=2, the string after the operation is 1001.

You are given a string T of length N consisting of 0 and 1.
Find the number of beautiful strings that are substrings of T. Even if two substrings are identical as strings, count them separately if they are taken from different positions.

What are substrings? A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, 10 is a substring of 101, but 11 is not a substring of 101.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an integer.
  • T is a string of length N consisting of 0 and 1.

Input

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

N
T

Output

Print the number of beautiful strings that are substrings of T.


Sample Input 1

3
110

Sample Output 1

3

The string 11 obtained by taking the 1st through 2nd characters of T is a beautiful string, because if you choose i=1 and perform the operation, the string becomes 1. The beautiful strings that are substrings of T are the following three strings:

  • The string 1 obtained by taking the 1st character of T.
  • The string 1 obtained by taking the 2nd character of T.
  • The string 11 obtained by taking the 1st through 2nd characters of T.

Sample Input 2

4
0000

Sample Output 2

4

Sample Input 3

30
011011100101110111100010011010

Sample Output 3

225
H - Avoid K Partition

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 K があります。
A をいくつかの連続部分列に分割する方法は 2^{N-1} 通りありますが、そのうち分割後に要素の和が K である列が存在しない分割の方法は何通りありますか。答えを 998244353 で割った余りを求めてください。

ここで、「A をいくつかの連続部分列に分割する」とは次の手順のことを言います。

  • 分割後の数列の個数 k (1 \leq k \leq N) および 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1 を満たす整数列 (i_1, i_2, \dots, i_k, i_{k+1}) を自由に選ぶ。
  • 1 \leq n \leq k について、n 番目の数列を、Ai_n 番目から i_{n+1} - 1 番目までの要素を順番を保ったまま取り出して出来る数列とする。

A = (1, 2, 3, 4, 5) のときの分割の例を以下に挙げます。

  • (1, 2, 3), (4), (5)
  • (1, 2), (3, 4, 5)
  • (1, 2, 3, 4, 5)

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^{15} \leq K \leq 10^{15}
  • -10^9 \leq A_i \leq 10^9
  • 入力される値は全て整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

3 3
1 2 3

出力例 1

2

問題文の条件を満たす分割は次の 2 通りです。

  • (1), (2, 3)
  • (1, 2, 3)

入力例 2

5 0
0 0 0 0 0

出力例 2

0

入力例 3

10 5
-5 -1 -7 6 -6 -2 -5 10 2 -10

出力例 3

428

Score : 475 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and an integer K.
There are 2^{N-1} ways to divide A into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to K? Find the count modulo 998244353.

Here, "to divide A into several contiguous subsequences" means the following procedure.

  • Freely choose the number k (1 \leq k \leq N) of subsequences and an integer sequence (i_1, i_2, \dots, i_k, i_{k+1}) satisfying 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1.
  • For each 1 \leq n \leq k, the n-th subsequence is formed by taking the i_n-th through (i_{n+1} - 1)-th elements of A, maintaining their order.

Here are some examples of divisions for A = (1, 2, 3, 4, 5):

  • (1, 2, 3), (4), (5)
  • (1, 2), (3, 4, 5)
  • (1, 2, 3, 4, 5)

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^{15} \leq K \leq 10^{15}
  • -10^9 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the count modulo 998244353.


Sample Input 1

3 3
1 2 3

Sample Output 1

2

There are two divisions that satisfy the condition in the problem statement:

  • (1), (2, 3)
  • (1, 2, 3)

Sample Input 2

5 0
0 0 0 0 0

Sample Output 2

0

Sample Input 3

10 5
-5 -1 -7 6 -6 -2 -5 10 2 -10

Sample Output 3

428
I - 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.