A - Signed Difficulty

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

配点 : 100

問題文

実数 X.Y が与えられます。ただし Y はちょうど 1 桁です。

  • 0 \leq Y \leq 2 ならば、X-
  • 3 \leq Y \leq 6 ならば、X
  • 7 \leq Y \leq 9 ならば、X+

と出力してください。

制約

  • 1 \leq X \leq 15
  • 0 \leq Y \leq 9
  • XY は整数

入力

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

X.Y

出力

答えを出力せよ。


入力例 1

15.8

出力例 1

15+

15 + のような出力は不正解とみなされます。
X+ の間や、X- の間には空白を入れずに出力してください。


入力例 2

1.0

出力例 2

1-

1.001 などの値が入力として与えられることはありません。


入力例 3

12.5

出力例 3

12

Score : 100 points

Problem Statement

You are given a real number X.Y, where Y is a single digit.

Print the following: (quotes for clarity)

  • X-, if 0 \leq Y \leq 2;
  • X, if 3 \leq Y \leq 6;
  • X+, if 7 \leq Y \leq 9.

Constraints

  • 1 \leq X \leq 15
  • 0 \leq Y \leq 9
  • X and Y are integers.

Input

Input is given from Standard Input in the following format:

X.Y

Output

Print the answer.


Sample Input 1

15.8

Sample Output 1

15+

Here, printing 15 + will not be accepted: do not print a space between X and +, or between X and -.


Sample Input 2

1.0

Sample Output 2

1-

You will not get inputs such as 1.00 and 1.


Sample Input 3

12.5

Sample Output 3

12
B - Leyland Number

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

配点 : 100

問題文

正整数 A,B が与えられます。
A^B+B^A の値を出力してください。

制約

  • 2 \leq A \leq B \leq 9
  • 入力される数値はすべて整数

入力

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

A B

出力

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


入力例 1

2 8

出力例 1

320

A = 2, B = 8 のとき、A^B = 256, B^A = 64 なので A^B + B^A = 320 です。


入力例 2

9 9

出力例 2

774840978

入力例 3

5 6

出力例 3

23401

Score : 100 points

Problem Statement

You are given positive integers A and B.
Print the value A^B+B^A.

Constraints

  • 2 \leq A \leq B \leq 9
  • All input values are integers.

Input

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

A B

Output

Print the answer as an integer.


Sample Input 1

2 8

Sample Output 1

320

For A = 2, B = 8, we have A^B = 256, B^A = 64, so A^B + B^A = 320.


Sample Input 2

9 9

Sample Output 2

774840978

Sample Input 3

5 6

Sample Output 3

23401
C - Star or Not

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

配点 : 200

問題文

N 頂点 N-1 辺の木が与えられます。
頂点には 1,2,\ldots,N の番号がついており、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

この木がスターであるか判定してください。

ただしスターとは、1 つの頂点から、他の全ての頂点に 1 本ずつ辺が出ている木のことです。

注記

「木」については、Wikipedia「木(数学)」 を参照してください。

制約

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • 与えられるグラフは木である

入力

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

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

出力

与えられたグラフがスターであるなら Yes と、スターでないなら No と出力せよ。


入力例 1

5
1 4
2 4
3 4
4 5

出力例 1

Yes

与えられたグラフはスターです。


入力例 2

4
2 4
1 4
2 3

出力例 2

No

与えられたグラフはスターではありません。


入力例 3

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

出力例 3

Yes

Score : 200 points

Problem Statement

You are given a tree with N vertices and N-1 edges.
The vertices are numbered 1,2,\ldots,N. The i-th edge connects Vertex a_i and Vertex b_i.

Determine whether this tree is a star.

Here, a star is a tree where there is a vertex directly connected to all other vertices.

Notes

For the definition of a tree, see Tree (graph theory) - Wikipedia.

Constraints

  • 3 \leq N \leq 10^5
  • 1 \leq a_i \lt b_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
\vdots
a_{N-1} b_{N-1}

Output

If the given graph is a star, print Yes; otherwise, print No.


Sample Input 1

5
1 4
2 4
3 4
4 5

Sample Output 1

Yes

The given graph is a star.


Sample Input 2

4
2 4
1 4
2 3

Sample Output 2

No

The given graph is not a star.


Sample Input 3

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

Sample Output 3

Yes
D - Who is Saikyo?

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

配点 : 300

問題文

N 人の競技プログラマーがいます。順に 人 1, 人 2, \dots, 人 N と呼びます。
競技プログラマーの間には 強さ と呼ばれる関係性があり、相異なる 2 人組 (X, 人 Y) 全てに対して 「人 X は人 Y より強い」または「人 Y は人 X より強い」のどちらか一方が成り立ちます。
強さ は 推移律 が成り立ちます。言い換えると、相異なる 3 人組 (X, 人 Y, 人 Z) 全てに対して次の条件が成り立ちます。

  • X が人 Y よりも強く、かつ人 Y が人 Z よりも強いとき、人 X は人 Z よりも強い。

X が自分以外のどの人 Y に対しても「人 X は人 Y より強い」という関係が成り立つ時、人 X最強のプログラマー と呼びます。(上記の制約下においてそのような人がちょうど 1 人存在することが証明できます)

あなたは M 個の強さに関する情報を知っています。i 個目の情報は「人 A_i は人 B_i より強い」という情報です。
あなたは情報を元に N 人の中から最強のプログラマーを特定することができますか?
最強のプログラマーを特定できる場合はその人の番号を出力してください。特定できない場合、つまり最強のプログラマーとしてあり得る人が複数人いる場合は -1 を出力してください。

制約

  • 2 \leq N \leq 50
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • i \neq j ならば (A_i, B_i) \neq (A_j, B_j)
  • 全ての情報が正しくなるように、全ての相異なる 2 人組にどちらが強いかを割り当てる方法が少なくとも 1 通り存在する

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

最強のプログラマーを特定できる場合はその人の番号を出力せよ。特定できない場合は -1 を出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

1

あなたは「人 1 は人 2 より強い」「人 2 は人 3 より強い」という情報が分かっています。
推移律から「人 1 は人 3 より強い」という情報もわかるので、人 1 は最強のプログラマーです。


入力例 2

3 2
1 3
2 3

出力例 2

-1

最強のプログラマーである可能性がある人は人 1 と人 2 です。最強のプログラマーを一意に特定できないので -1 を出力してください。


入力例 3

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

出力例 3

-1

Score : 300 points

Problem Statement

There are N competitive programmers numbered person 1, person 2, \ldots, and person N.
There is a relation called superiority between the programmers. For all pairs of distinct programmers (person X, person Y), exactly one of the following two relations holds: "person X is stronger than person Y" or "person Y is stronger than person X."
The superiority is transitive. In other words, for all triplets of distinct programmers (person X, person Y, person Z), it holds that:

  • if person X is stronger than person Y and person Y is stronger than person Z, then person X is stronger than person Z.

A person X is said to be the strongest programmer if person X is stronger than person Y for all people Y other than person X. (Under the constraints above, we can prove that there is always exactly one such person.)

You have M pieces of information on their superiority. The i-th of them is that "person A_i is stronger than person B_i."
Can you determine the strongest programmer among the N based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print -1.

Constraints

  • 2 \leq N \leq 50
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • If i \neq j, then (A_i, B_i) \neq (A_j, B_j).
  • There is at least one way to determine superiorities for all pairs of distinct programmers, that is consistent with the given information.

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

If you can uniquely determine the strongest programmer, print the person's number; otherwise, print -1.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

1

You have two pieces of information: "person 1 is stronger than person 2" and "person 2 is stronger than person 3."
By the transitivity, you can also infer that "person 1 is stronger than person 3," so person 1 is the strongest programmer.


Sample Input 2

3 2
1 3
2 3

Sample Output 2

-1

Both person 1 and person 2 may be the strongest programmer. Since you cannot uniquely determine which is the strongest, you should print -1.


Sample Input 3

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

Sample Output 3

-1
E - Calendar Validator

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

配点 : 300

問題文

10^{100}7 列の行列 A があり、任意の整数対 (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7) についてその (i,j) 成分は (i-1) \times 7 + j です。

NM 列の行列 B が与えられるので、BA から一部の矩形領域を(向きを変えずに)切り出したものであるかを判定してください。

制約

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

出力

BA から一部の矩形領域を切り出したものであれば Yes と、そうでないなら No と出力せよ。


入力例 1

2 3
1 2 3
8 9 10

出力例 1

Yes

与えられる B は、A の左上 23 列を切り出したものとなっています。


入力例 2

2 1
1
2

出力例 2

No

与えられる B90 度回転させると A の左上 12 列と一致しますが、問題文中に「向きを変えずに」とある通り回転による一致は認められていないため、答えは No となります。


入力例 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

出力例 3

Yes

Score : 300 points

Problem Statement

There is a 10^{100} \times 7 matrix A, where the (i,j)-th entry is (i-1) \times 7 + j for every pair of integers (i,j)\ (1 \leq i \leq 10^{100}, 1 \leq j \leq 7).

Given an N \times M matrix B, determine whether B is some (unrotated) rectangular part of A.

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq M \leq 7
  • 1 \leq B_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
B_{1,1} B_{1,2} \ldots B_{1,M}
B_{2,1} B_{2,2} \ldots B_{2,M}
\hspace{1.6cm}\vdots
B_{N,1} B_{N,2} \ldots B_{N,M}

Output

If B is some rectangular part of A, print Yes; otherwise, print No.


Sample Input 1

2 3
1 2 3
8 9 10

Sample Output 1

Yes

The given matrix B is the top-left 2 \times 3 submatrix of A.


Sample Input 2

2 1
1
2

Sample Output 2

No

Although the given matrix B would match the top-left 1 \times 2 submatrix of A after rotating 90 degrees, the Problem Statement asks whether B is an unrotated part of A, so the answer is No.


Sample Input 3

10 4
1346 1347 1348 1349
1353 1354 1355 1356
1360 1361 1362 1363
1367 1368 1369 1370
1374 1375 1376 1377
1381 1382 1383 1384
1388 1389 1390 1391
1395 1396 1397 1398
1402 1403 1404 1405
1409 1410 1411 1412

Sample Output 3

Yes
F - Simple path

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

配点 : 300

問題文

N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。

ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。

単純パスとは? グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_iv_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • 入力はすべて整数
  • 与えられるグラフは木

入力

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

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。


入力例 1

5 2 5
1 2
1 3
3 4
3 5

出力例 1

2 1 3 5

T は以下のような形であり、頂点 2 から頂点 5への単純パスは 頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。


入力例 2

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

出力例 2

1 2

T は以下のような形です。

Score : 300 points

Problem Statement

There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.

You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.

It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.

What is a simple path? For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • All values in the input are integers.
  • The given graph is a tree.

Input

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

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.


Sample Input 1

5 2 5
1 2
1 3
3 4
3 5

Sample Output 1

2 1 3 5

The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.


Sample Input 2

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

Sample Output 2

1 2

The tree T is shown below.

G - Weak Takahashi

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

配点 : 400

問題文

H 行、横 W 行の H \times W マスからなるグリッドがあります。上から i 行目、左から j 列目のマスを (i, j) と表します。
各マスの状態は文字 C_{i, j} で表され、C_{i, j} = . ならばマス (i, j) は空きマスであり、C_{i, j} = # ならばマス (i, j) は壁です。

高橋君がグリッド上を歩こうとしています。彼がマス (i, j) にいるとき、マス (i, j + 1) またはマス (i + 1, j) に移動することができます。ただし、グリッドの外に出るような移動や、壁のマスへの移動を行うことはできません。高橋君は、移動することのできるマスが無くなった時点で立ち止まります。

高橋君がマス (1, 1) から歩き始めるとき、彼が立ち止まるまでに通ることのできるマスは最大で何マスですか?

制約

  • 1 \leq H, W \leq 100
  • H, W は整数
  • C_{i, j} = . または C_{i, j} = # (1 \leq i \leq H, 1 \leq j \leq W)
  • C_{1, 1} = .

入力

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

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

出力

答えを出力せよ。


入力例 1

3 4
.#..
..#.
..##

出力例 1

4

例えば (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) と進むことで、4 マス通ることができます。

5 マス以上通ることはできないので、4 と出力します。


入力例 2

1 1
.

出力例 2

1

入力例 3

5 5
.....
.....
.....
.....
.....

出力例 3

9

Score : 400 points

Problem Statement

There is a H \times W-square grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
Each square is described by a character C_{i, j}, where C_{i, j} = . means (i, j) is an empty square, and C_{i, j} = # means (i, j) is a wall.

Takahashi is about to start walking in this grid. When he is on (i, j), he can go to (i, j + 1) or (i + 1, j). However, he cannot exit the grid or enter a wall square. He will stop when there is no more square to go to.

When starting on (1, 1), at most how many squares can Takahashi visit before he stops?

Constraints

  • 1 \leq H, W \leq 100
  • H and W are integers.
  • C_{i, j} = . or C_{i, j} = #. (1 \leq i \leq H, 1 \leq j \leq W)
  • C_{1, 1} = .

Input

Input is given from Standard Input in the following format:

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

Output

Print the answer.


Sample Input 1

3 4
.#..
..#.
..##

Sample Output 1

4

For example, by going (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2), he can visit 4 squares.

He cannot visit 5 or more squares, so we should print 4.


Sample Input 2

1 1
.

Sample Output 2

1

Sample Input 3

5 5
.....
.....
.....
.....
.....

Sample Output 3

9
H - Swap

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

配点 : 500

問題文

K, E, Y のみからなる文字列 S が与えられます。

S の隣接する 2 文字を入れ替える操作を K 回まで行えるとき、作ることができる文字列は何種類ありますか?

制約

  • 2 \leq |S| \leq 30
  • 0 \leq K \leq 10^9
  • SK, E, Y のみからなる

入力

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

S
K

出力

答えを出力せよ。


入力例 1

KEY
1

出力例 1

3

KEY に対して 1 回以下の操作を行うことで得られる文字列は KEY, EKY, KYE3 種類です。


入力例 2

KKEE
2

出力例 2

4

KKEE に対して 2 回以下の操作を行うことで得られる文字列は KKEE, KEKE, EKKE, KEEK4 種類です。


入力例 3

KKEEYY
1000000000

出力例 3

90

Score : 500 points

Problem Statement

Given is a string S consisting of K, E, Y.

How many strings are there that can be obtained with at most K swaps of two adjacent characters in S?

Constraints

  • 2 \leq |S| \leq 30
  • 0 \leq K \leq 10^9
  • S consists of K, E, Y.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the answer.


Sample Input 1

KEY
1

Sample Output 1

3

With at most one swap, there are three strings that can be obtained: KEY, EKY, KYE.


Sample Input 2

KKEE
2

Sample Output 2

4

With at most two swaps, there are four strings that can be obtained: KKEE, KEKE, EKKE, KEEK.


Sample Input 3

KKEEYY
1000000000

Sample Output 3

90
I - Vacation Query

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

配点 : 550

問題文

0, 1 からなる長さ N の文字列 S が与えられます。Si 文字目を S_i とします。

Q 個のクエリを与えられた順に処理してください。
クエリは 3 個の整数の組 (c, L, R) で表されて、c の値によってクエリの種類が異なります。

  • c=1 のとき : L \leq i \leq R を満たす整数 i について、S_i1 ならば 0 に、0 ならば 1 に変更する。
  • c=2 のとき : SL 文字目から R 文字目までを取り出して出来る文字列を T とする。T に含まれる連続する 1 の長さの最大値を出力する。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq Q \leq 10^5
  • S は長さ N0, 1 からなる文字列
  • c \in \lbrace 1, 2 \rbrace
  • 1 \leq L \leq R \leq N
  • N, Q, c, L, R は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_ii 番目のクエリを意味する。

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

各クエリは次の形式で与えられる。

c L R

出力

c=2 であるクエリの個数を k として、k 行出力せよ。
i 行目には i 番目の c=2 であるクエリに対する答えを出力せよ。


入力例 1

7 6
1101110
2 1 7
2 2 4
1 3 6
2 5 6
1 4 7
2 1 7

出力例 1

3
1
0
7

クエリを順に処理すると次のようになります。

  • はじめ、S= 1101110 です。
  • 1 番目のクエリについて、T = 1101110 です。T に含まれる連続する 1 の中で最も長いものは、T4 文字目から 6 文字目からなる 111 なので、答えは 3 になります。
  • 2 番目のクエリについて、T = 101 です。T に含まれる連続する 1 の中で最も長いものは、T1 文字目あるいは 3 文字目の 1 なので、答えは 1 になります。
  • 3 番目のクエリについて、操作後の S1110000 です。
  • 4 番目のクエリについて、T = 00 です。T1 は含まれないので答えは 0 になります。
  • 5 番目のクエリについて、操作後の S1111111 です。
  • 6 番目のクエリについて、T = 1111111 です。T に含まれる連続する 1 の中で最も長いものは、T1 文字目から 7 文字目からなる 1111111 なので、答えは 7 になります。

Score : 550 points

Problem Statement

You are given a string S of length N consisting of 0 and 1. Let S_i denote the i-th character of S.

Process Q queries in the order they are given.
Each query is represented by a tuple of three integers (c, L, R), where c represents the type of the query.

  • When c=1: For each integer i such that L \leq i \leq R, if S_i is 1, change it to 0; if it is 0, change it to 1.
  • When c=2: Let T be the string obtained by extracting the L-th through R-th characters of S. Print the maximum number of consecutive 1s in T.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq Q \leq 10^5
  • S is a string of length N consisting of 0 and 1.
  • c \in \lbrace 1, 2 \rbrace
  • 1 \leq L \leq R \leq N
  • N, Q, c, L, and R are all integers.

Input

The input is given from Standard Input in the following format, where \mathrm{query}_i represents the i-th query:

N Q
S
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is given in the following format:

c L R

Output

Let k be the number of queries with c=2. Print k lines.
The i-th line should contain the answer to the i-th query with c=2.


Sample Input 1

7 6
1101110
2 1 7
2 2 4
1 3 6
2 5 6
1 4 7
2 1 7

Sample Output 1

3
1
0
7

The queries are processed as follows.

  • Initially, S= 1101110.
  • For the first query, T = 1101110. The longest sequence of consecutive 1s in T is 111 from the 4-th character to 6-th character, so the answer is 3.
  • For the second query, T = 101. The longest sequence of consecutive 1s in T is 1 at the 1-st or 3-rd character, so the answer is 1.
  • For the third query, the operation changes S to 1110000.
  • For the fourth query, T = 00. T does not contain 1, so the answer is 0.
  • For the fifth query, the operation changes S to 1111111.
  • For the sixth query, T = 1111111. The longest sequence of consecutive 1s in T is 1111111 from the 1-st to 7-th characters, so the answer is 7.