Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
以下のような、1 から 9 までの数字が書かれた 3 \times 3 の盤面があります。
1 以上 9 以下の整数 A,B が与えられます。ただし、A < B です。
A が書かれたマスと B が書かれたマスが左右に隣接しているか判定してください。
制約
- 1 \le A < B \le 9
- A, B は整数である。
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
A が書かれたマスと B が書かれたマスが左右に隣接しているならば Yes
、そうでないならば No
を出力せよ。
入力例 1
7 8
出力例 1
Yes
7 が書かれたマスと 8 が書かれたマスは左右に隣り合っているので、Yes
を出力します。
入力例 2
1 9
出力例 2
No
入力例 3
3 4
出力例 3
No
Score : 100 points
Problem Statement
We have the following 3 \times 3 board with integers from 1 through 9 written on it.
You are given two integers A and B between 1 and 9, where A < B.
Determine if the two squares with A and B written on them are adjacent horizontally.
Constraints
- 1 \le A < B \le 9
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print Yes
if the two squares with A and B written on them are adjacent horizontally, and No
otherwise.
Sample Input 1
7 8
Sample Output 1
Yes
The two squares with 7 and 8 written on them are adjacent horizontally, so print Yes
.
Sample Input 2
1 9
Sample Output 2
No
Sample Input 3
3 4
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
上下左右に広がる H 行 W 列のマス目があり、各マスにはコマが置かれているか、何も置かれていないかのどちらかです。
マス目の状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって表され、
S_i の j 文字目が #
のとき上から i 行目かつ左から j 列目のマスにはコマが置かれていることを、
S_i の j 文字目が .
のとき上から i 行目かつ左から j 列目のマスには何も置かれていないことを表しています。
マス目上のマスのうち、コマが置かれているようなものの個数を求めてください。
制約
- 1\leq H,W \leq 10
- H,W は整数
- S_i は
#
と.
のみからなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
コマが置かれているマスの個数を整数で出力せよ。
入力例 1
3 5 #.... ..... .##..
出力例 1
3
- 上から 1 行目かつ左から 1 列目のマス
- 上から 3 行目かつ左から 2 列目のマス
- 上から 3 行目かつ左から 3 列目のマス
の計 3 つのマスにコマが置かれているため、3 を出力します。
入力例 2
1 10 ..........
出力例 2
0
どのマスにもコマは置かれていないため、0 を出力します。
入力例 3
6 5 #.#.# ....# ..##. ####. ..#.. #####
出力例 3
16
Score : 100 points
Problem Statement
There is a grid with H rows from top to bottom and W columns from left to right. Each square has a piece placed on it or is empty.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W.
If the j-th character of S_i is #
, the square at the i-th row from the top and j-th column from the left has a piece on it;
if the j-th character of S_i is .
, the square at the i-th row from the top and j-th column from the left is empty.
How many squares in the grid have pieces on them?
Constraints
- 1\leq H,W \leq 10
- H and W are integers.
- S_i is a string of length W consisting of
#
and.
.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print the number of squares with pieces as an integer.
Sample Input 1
3 5 #.... ..... .##..
Sample Output 1
3
The following three squares have pieces on them:
- the square at the 1-st row from the top and 1-st column from the left;
- the square at the 3-rd row from the top and 2-nd column from the left;
- the square at the 3-rd row from the top and 3-rd column from the left.
Thus, 3 should be printed.
Sample Input 2
1 10 ..........
Sample Output 2
0
Since no square has a piece on it, 0 should be printed.
Sample Input 3
6 5 #.#.# ....# ..##. ####. ..#.. #####
Sample Output 3
16
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) および正整数 M が与えられます。
A の末尾の要素を削除するという操作を 0 回以上 N 回以下行うことで、以下の条件が満たされないようにしたいです。
- 条件:A には 1 以上 M 以下の整数がすべて含まれている。
必要な操作回数の最小値を求めてください。
なお、本問題の制約下において、操作を 0 回以上 N 回以下行うことで上述の条件が満たされないようにすることが必ず可能であることが証明できます。
制約
- 1\leq M \leq N \leq 100
- 1\leq A_i \leq M
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
条件が満たされなくなるために必要な操作回数の最小値を出力せよ。
入力例 1
5 3 3 2 3 1 2
出力例 1
2
最初、A=(3,2,3,1,2) です。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。
A の末尾の要素を削除する操作を 1 回行うと、A=(3,2,3,1) となります。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。
A の末尾の要素を削除する操作をもう 1 回行うと、A=(3,2,3) となります。このとき、A には 1 が含まれないため条件を満たしません。
よって、条件が満たされなくなるために必要な操作回数の最小値は 2 です。
入力例 2
4 3 1 3 1 3
出力例 2
0
A には最初から 2 が含まれず条件を満たさないため、操作を 1 回も行う必要がありません。
入力例 3
10 4 1 3 3 4 2 1 3 1 2 4
出力例 3
6
Score : 200 points
Problem Statement
You are given an integer sequence A = (A_1, A_2, \dots, A_N) of length N and a positive integer M.
Your goal is to make the following condition false by performing this operation between 0 and N times (inclusive): remove the last element of A.
- Condition: A contains every integer from 1 through M.
Find the minimum number of operations required.
Under the constraints of this problem, it can be proved that it is always possible to make the condition false by performing the operation between 0 and N times.
Constraints
- 1 \le M \le N \le 100
- 1 \le A_i \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Output the minimum number of operations required to make the condition false.
Sample Input 1
5 3 3 2 3 1 2
Sample Output 1
2
Initially, A = (3,2,3,1,2). Since A contains every integer from 1 through 3, the condition holds.
If you perform the operation once, A = (3,2,3,1). The condition still holds.
If you perform the operation once more, A = (3,2,3). The integer 1 is missing, so the condition no longer holds.
Therefore, the minimum required number of operations is 2.
Sample Input 2
4 3 1 3 1 3
Sample Output 2
0
Since A initially lacks the integer 2, the condition is already false, so no operation is needed.
Sample Input 3
10 4 1 3 3 4 2 1 3 1 2 4
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 個の駅、駅 1, 駅 2, \ldots, 駅 N が直線上にこの順に並んでいます。
ここで、1\leq i\leq N-1 について、駅 i と駅 (i+1) の間の距離は D_i です。
1\leq i<j\leq N をみたす整数の組 (i,j) それぞれについて、駅 i と駅 j の間の距離を求めてください。
なお、出力形式については出力の欄を参照してください。
制約
- 2 \leq N \leq 50
- 1 \leq D_i \leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D_1 D_2 \ldots D_{N-1}
出力
N-1 行出力せよ。
i 行目 (1\leq i\leq N-1) には、(N-i) 個の整数を空白区切りで出力せよ。
i 行目の j 番目 (1\leq j\leq N-i) には、駅 i と駅 (i+j) の間の距離を出力せよ。
入力例 1
5 5 10 2 3
出力例 1
5 15 17 20 10 12 15 2 5 3
各駅の間の距離は次のようになります。
- 駅 1 と駅 2 の間の距離は 5 です。
- 駅 1 と駅 3 の間の距離は 5+10=15 です。
- 駅 1 と駅 4 の間の距離は 5+10+2=17 です。
- 駅 1 と駅 5 の間の距離は 5+10+2+3=20 です。
- 駅 2 と駅 3 の間の距離は 10 です。
- 駅 2 と駅 4 の間の距離は 10+2=12 です。
- 駅 2 と駅 5 の間の距離は 10+2+3=15 です。
- 駅 3 と駅 4 の間の距離は 2 です。
- 駅 3 と駅 5 の間の距離は 2+3=5 です。
- 駅 4 と駅 5 の間の距離は 3 です。
よって、上記のように出力します。
入力例 2
2 100
出力例 2
100
Score : 200 points
Problem Statement
There are N stations, station 1, station 2, \ldots, station N, arranged in this order on a straight line.
Here, for 1\leq i\leq N-1, the distance between stations i and (i+1) is D_i.
For each pair of integers (i,j) satisfying 1\leq i<j\leq N, find the distance between stations i and j.
For the output format, refer to the Output section.
Constraints
- 2 \leq N \leq 50
- 1 \leq D_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D_1 D_2 \ldots D_{N-1}
Output
Output N-1 lines.
On the i-th line (1\leq i\leq N-1), output (N-i) integers, separated by spaces.
The j-th integer on the i-th line (1\leq j\leq N-i) should be the distance between stations i and (i+j).
Sample Input 1
5 5 10 2 3
Sample Output 1
5 15 17 20 10 12 15 2 5 3
The distances between stations are as follows:
- The distance between stations 1 and 2 is 5.
- The distance between stations 1 and 3 is 5+10=15.
- The distance between stations 1 and 4 is 5+10+2=17.
- The distance between stations 1 and 5 is 5+10+2+3=20.
- The distance between stations 2 and 3 is 10.
- The distance between stations 2 and 4 is 10+2=12.
- The distance between stations 2 and 5 is 10+2+3=15.
- The distance between stations 3 and 4 is 2.
- The distance between stations 3 and 5 is 2+3=5.
- The distance between stations 4 and 5 is 3.
Thus, output as shown above.
Sample Input 2
2 100
Sample Output 2
100
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1,2,\dots,N の番号が、辺には 1,2,\dots,M の番号がつけられており、辺 i は頂点 A_i と頂点 B_i を結んでいます。
このグラフがサイクルグラフであるか判定してください。
単純無向グラフとは
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
サイクルグラフとは
頂点に 1, 2, \dots, N の番号が付けられた N 頂点のグラフがサイクルグラフであるとは、(1, 2, \dots, N) を並べ変えて得られる数列 (v_1, v_2, \dots, v_N) であって、以下の条件を満たすものが存在することをいいます。
制約
- 3\leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1 \leq A_i, B_i \leq N
- 与えられるグラフは単純
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 \vdots A_M B_M
出力
与えられたグラフがサイクルグラフであるなら Yes
、そうでないなら No
と出力せよ。
入力例 1
4 4 2 4 3 1 4 1 2 3
出力例 1
Yes
与えられたグラフは以下の通りであり、これはサイクルグラフです。
入力例 2
4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 2
No
与えられたグラフは以下の通りであり、これはサイクルグラフではありません。
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1,2,\dots,N and the edges are numbered 1,2,\dots,M. Edge i connects vertices A_i and B_i.
Determine whether this graph is a cycle graph.
Definition of simple undirected graph
A simple undirected graph is a graph with undirected edges without self-loops or multi-edges.
Definition of cycle graph
An N-vertex graph with vertices labeled 1,2,\dots,N is a cycle graph when there exists a permutation (v_1,v_2,\dots,v_N) of (1,2,\dots,N) such that:
Constraints
- 3 \le N \le 2\times 10^5
- 0 \le M \le 2\times 10^5
- 1 \le A_i, B_i \le N
- The given graph is simple.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 \vdots A_M B_M
Output
Output Yes
if the given graph is a cycle graph; otherwise, print No
.
Sample Input 1
4 4 2 4 3 1 4 1 2 3
Sample Output 1
Yes
The given graph is as follows, and this is a cycle graph.
Sample Input 2
4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 2
No
The given graph is as follows, and this is not a cycle graph.
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
414 の十進法での表記は 414
であり、これは回文です。
また、414 の八進法での表記は 636
であり、これも回文です。
これを踏まえて、以下の問題を解いてください。
正の整数 A, N が与えられます。 1 以上 N 以下の整数のうち、十進法での表記も A 進法での表記も回文であるようなものの総和を求めてください。
なお、この問題の制約下で答えは 2^{63} 未満であることが証明できます。
制約
- 2 \leq A \leq 9
- 1 \leq N \le 10^{12}
- 入力される数値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
A N
出力
答えを 1 行で出力せよ。
入力例 1
8 1000
出力例 1
2155
条件を満たす整数は 1,2,3,4,5,6,7,9,121,292,333,373,414,585 の 14 個であり、その総和は 2155 です。
入力例 2
8 999999999999
出力例 2
914703021014
入力例 3
6 999999999999
出力例 3
283958331810
Score : 350 points
Problem Statement
The decimal representation of 414 is 414
, which is a palindrome.
Also, the octal representation of 414 is 636
, which is also a palindrome.
Based on this, solve the following problem.
You are given positive integers A and N. Find the sum of all integers between 1 and N, inclusive, whose decimal representation and base-A representation are both palindromes.
Under the constraints of this problem, it can be proved that the answer is less than 2^{63}.
Constraints
- 2 \leq A \leq 9
- 1 \leq N \le 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A N
Output
Output the answer in one line.
Sample Input 1
8 1000
Sample Output 1
2155
The integers satisfying the condition are 1,2,3,4,5,6,7,9,121,292,333,373,414,585 (14 integers), and their sum is 2155.
Sample Input 2
8 999999999999
Sample Output 2
914703021014
Sample Input 3
6 999999999999
Sample Output 3
283958331810
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
長さ N の英小文字からなる文字列 S=S_1S_2\dots S_N が与えられます。あなたは、S に対して以下の操作をちょうど 1 回行います。
- S の長さ 1 以上の連続部分文字列を選んで、左に 1 だけ巡回シフトする。すなわち、整数 1 \leq \ell \leq r \leq N を選んで、S の r 文字目の右に S_\ell を挿入したのち、S の \ell 番目の文字を削除する。
操作後の S としてありえる文字列のうち、辞書順最小のものを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えてください。
制約
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- S は英小文字からなる長さ N の文字列
- T,N は整数
- 1 つの入力ファイルに含まれる N の総和は 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
各テストケース \mathrm{case}_i\;(1 \leq i \leq T) は以下の形式で与えられる。
N S
出力
T 行出力せよ。i \; (1 \leq i \leq T) 行目には、\mathrm{case}_i に対する答えを出力せよ。
入力例 1
3 7 atcoder 1 x 5 snuke
出力例 1
acodert x nsuke
- 1 番目のテストケースでは、2 文字目から 7 文字目を巡回シフトして
acodert
とするのが辞書順最小です。 - 2 番目のテストケースでは、どのように操作しても
x
が得られます。 - 3 番目のテストケースでは、1 文字目から 2 文字目を巡回シフトして
nsuke
とするのが辞書順最小です。
Score : 400 points
Problem Statement
You are given a string S=S_1S_2\dots S_N of length N consisting of lowercase English letters. You will perform the following operation on S exactly once:
- Choose a contiguous substring of S with length at least 1 and cyclically shift it to the left by 1. That is, choose integers 1 \leq \ell \leq r \leq N, insert S_\ell to the right of the r-th character of S, and then delete the \ell-th character of S.
Find the lexicographically smallest string among all possible strings that S can become after the operation.
You are given T test cases, so solve each of them.
Constraints
- 1 \leq T \leq 10^5
- 1 \leq N \leq 10^5
- S is a string of length N consisting of lowercase English letters.
- T and N are integers.
- The sum of N over all test cases in a single input file is at most 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
Each test case \mathrm{case}_i\;(1 \leq i \leq T) is given in the following format:
N S
Output
Output T lines. The i-th (1 \leq i \leq T) line should contain the answer for \mathrm{case}_i.
Sample Input 1
3 7 atcoder 1 x 5 snuke
Sample Output 1
acodert x nsuke
- In the first test case, cyclically shifting from the 2nd to the 7th character gives
acodert
, which is lexicographically smallest. - In the second test case, no matter how you operate, you get
x
. - In the third test case, cyclically shifting from the 1st to the 2nd character gives
nsuke
, which is lexicographically smallest.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
3 次元空間内に N 個の直方体があります。
直方体同士は重なっていません。厳密には、相異なるどの 2 つの直方体の共通部分の体積も 0 です。
i 番目の直方体は、2 点 (X_{i,1},Y_{i,1},Z_{i,1}), (X_{i,2},Y_{i,2},Z_{i,2}) を結ぶ線分を対角線とし、辺は全ていずれかの座標軸に平行です。
各直方体について、他のいくつの直方体と面で接しているか求めてください。
厳密には、各 i に対し、1\leq j \leq N かつ j\neq i である j のうち、i 番目の直方体の表面と j 番目の直方体の表面の共通部分の面積が正であるものの個数を求めてください。
制約
- 1 \leq N \leq 10^5
- 0 \leq X_{i,1} < X_{i,2} \leq 100
- 0 \leq Y_{i,1} < Y_{i,2} \leq 100
- 0 \leq Z_{i,1} < Z_{i,2} \leq 100
- 直方体同士は体積が正の共通部分を持たない
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2} \vdots X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}
出力
答えを出力せよ。
入力例 1
4 0 0 0 1 1 1 0 0 1 1 1 2 1 1 1 2 2 2 3 3 3 4 4 4
出力例 1
1 1 0 0
1 番目の直方体と 2 番目の直方体は、2 点 (0,0,1),(1,1,1) を結ぶ線分を対角線とする長方形を共有しています。
1 番目の直方体と 3 番目の直方体は、点 (1,1,1) を共有していますが、面で接してはいません。
入力例 2
3 0 0 10 10 10 20 3 4 1 15 6 10 0 9 6 1 20 10
出力例 2
2 1 1
入力例 3
8 0 0 0 1 1 1 0 0 1 1 1 2 0 1 0 1 2 1 0 1 1 1 2 2 1 0 0 2 1 1 1 0 1 2 1 2 1 1 0 2 2 1 1 1 1 2 2 2
出力例 3
3 3 3 3 3 3 3 3
Score : 500 points
Problem Statement
There are N rectangular cuboids in a three-dimensional space.
These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has a volume of 0.
The diagonal of the i-th cuboid is a segment that connects two points (X_{i,1},Y_{i,1},Z_{i,1}) and (X_{i,2},Y_{i,2},Z_{i,2}), and its edges are all parallel to one of the coordinate axes.
For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each i, find the number of j with 1\leq j \leq N and j\neq i such that the intersection of the surfaces of the i-th and j-th cuboids has a positive area.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq X_{i,1} < X_{i,2} \leq 100
- 0 \leq Y_{i,1} < Y_{i,2} \leq 100
- 0 \leq Z_{i,1} < Z_{i,2} \leq 100
- Cuboids do not have an intersection with a positive volume.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_{1,1} Y_{1,1} Z_{1,1} X_{1,2} Y_{1,2} Z_{1,2} \vdots X_{N,1} Y_{N,1} Z_{N,1} X_{N,2} Y_{N,2} Z_{N,2}
Output
Print the answer.
Sample Input 1
4 0 0 0 1 1 1 0 0 1 1 1 2 1 1 1 2 2 2 3 3 3 4 4 4
Sample Output 1
1 1 0 0
The 1-st and 2-nd cuboids share a rectangle whose diagonal is the segment connecting two points (0,0,1) and (1,1,1).
The 1-st and 3-rd cuboids share a point (1,1,1), but do not share a surface.
Sample Input 2
3 0 0 10 10 10 20 3 4 1 15 6 10 0 9 6 1 20 10
Sample Output 2
2 1 1
Sample Input 3
8 0 0 0 1 1 1 0 0 1 1 1 2 0 1 0 1 2 1 0 1 1 1 2 2 1 0 0 2 1 1 1 0 1 2 1 2 1 1 0 2 2 1 1 1 1 2 2 2
Sample Output 3
3 3 3 3 3 3 3 3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
0
, 1
からなる長さ N の文字列 S が与えられます。S の i 文字目を S_i とします。
Q 個のクエリを与えられた順に処理してください。
クエリは 3 個の整数の組 (c, L, R) で表されて、c の値によってクエリの種類が異なります。
- c=1 のとき : L \leq i \leq R を満たす整数 i について、S_i が
1
ならば0
に、0
ならば1
に変更する。 - c=2 のとき : S の L 文字目から R 文字目までを取り出して出来る文字列を T とする。T に含まれる連続する
1
の長さの最大値を出力する。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq Q \leq 10^5
- S は長さ N の
0
,1
からなる文字列 - c \in \lbrace 1, 2 \rbrace
- 1 \leq L \leq R \leq N
- N, Q, c, L, R は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_i は i 番目のクエリを意味する。
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
の中で最も長いものは、T の 4 文字目から 6 文字目からなる111
なので、答えは 3 になります。 - 2 番目のクエリについて、T =
101
です。T に含まれる連続する1
の中で最も長いものは、T の 1 文字目あるいは 3 文字目の1
なので、答えは 1 になります。 - 3 番目のクエリについて、操作後の S は
1110000
です。 - 4 番目のクエリについて、T =
00
です。T に1
は含まれないので答えは 0 になります。 - 5 番目のクエリについて、操作後の S は
1111111
です。 - 6 番目のクエリについて、T =
1111111
です。T に含まれる連続する1
の中で最も長いものは、T の 1 文字目から 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 to0
; if it is0
, change it to1
. - 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
1
s 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
and1
. - 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 consecutive1
s in T is111
from the 4-th character to 6-th character, so the answer is 3. - For the second query, T =
101
. The longest sequence of consecutive1
s in T is1
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 contain1
, 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 consecutive1
s in T is1111111
from the 1-st to 7-th characters, so the answer is 7.