E - Move Segment

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

配点 : 300

問題文

0, 1 のみからなる長さ N の文字列 S が与えられます。
S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列を出力してください。

なお、S には 1 の塊が K 個以上含まれることが保証されます。

より正確な説明は以下の通りです。

  • Sl 文字目から r 文字目までからなる部分文字列を S_{l\ldots r} と表す。
  • S の部分文字列 S_{l\ldots r}1 の塊であるとは、以下の条件を全て満たすことと定める。
    • S_l=S_{l+1}=\cdots=S_r= 1
    • l=1 または S_{l-1}= 0
    • r=N または S_{r+1}= 0
  • S に含まれる 1 の塊が S_{l_1\ldots r_1},\ldots,S_{l_m\ldots r_m} で全てであり、l_1 < \cdots < l_m を満たしているとする。
    このとき、以下で定義される長さ N の文字列 T を、「S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列」と定める
    • 1 \leq i \leq r_{K-1} のとき T_i = S_i
    • r_{K-1}+1 \leq i \leq r_{K-1}+(r_K-l_K)+1 のとき T_i= 1
    • r_{K-1}+(r_K-l_K)+2 \leq i \leq r_K のとき T_i= 0
    • r_K+1 \leq i \leq N のとき T_i=S_i

制約

  • 1 \leq N \leq 5\times 10^5
  • S0, 1 のみからなる長さ N の文字列
  • 2 \leq K
  • S には 1 の塊が K 個以上含まれる

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

15 3
010011100011001

出力例 1

010011111000001

S には、2 文字目から 2 文字目、5 文字目から 7 文字目、11 文字目から 12 文字目、15 文字目から 15 文字目の 4 つの 1 の塊があります。


入力例 2

10 2
1011111111

出力例 2

1111111110

Score : 300 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.
Move the K-th 1-block from the beginning in S to immediately after the (K-1)-th 1-block, and print the resulting string.

It is guaranteed that S contains at least K 1-blocks.

Here is a more precise description.

  • Let S_{l\ldots r} denote the substring of S from the l-th character through the r-th character.
  • We define a substring S_{l\ldots r} of S to be a 1-block if it satisfies all of the following conditions:
    • S_l = S_{l+1} = \cdots = S_r = 1
    • l = 1 or S_{l-1} = 0
    • r = N or S_{r+1} = 0
  • Suppose that all 1-blocks in S are S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m}, where l_1 < l_2 < \cdots < l_m.

    Then, we define the length N string T, obtained by moving the K-th 1-block to immediately after the (K-1)-th 1-block, as follows:

    • T_i = S_i for 1 \leq i \leq r_{K-1}
    • T_i = 1 for r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1
    • T_i = 0 for r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K
    • T_i = S_i for r_K + 1 \leq i \leq N

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 2 \leq K
  • S contains at least K 1-blocks.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

15 3
010011100011001

Sample Output 1

010011111000001

S has four 1-blocks: from the 2nd to the 2nd character, from the 5th to the 7th character, from the 11th to the 12th character, and from the 15th to the 15th character.


Sample Input 2

10 2
1011111111

Sample Output 2

1111111110
F - Almost Equal

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

配点 : 250

問題文

英小文字からなる長さ M の文字列 NS_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。

これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。

  • 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。

制約

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
  • S_i は互いに異なる。

入力

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

N M
S_1
S_2
\vdots
S_N

出力

問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 4
bbed
abcd
abed
fbed

出力例 1

Yes

abcd , abed , bbed , fbed の順に並び替えると条件を満たします。


入力例 2

2 5
abcde
abced

出力例 2

No

どのように並び替えても条件を満たすことは出来ません。


入力例 3

8 4
fast
face
cast
race
fact
rice
nice
case

出力例 3

Yes

Score : 250 points

Problem Statement

You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.

Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:

  • for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.

Constraints

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
  • S_i are pairwise distinct.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print Yes if one can obtain a conforming sequence; print No otherwise.


Sample Input 1

4 4
bbed
abcd
abed
fbed

Sample Output 1

Yes

One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.


Sample Input 2

2 5
abcde
abced

Sample Output 2

No

No matter how the strings are rearranged, the condition is never satisfied.


Sample Input 3

8 4
fast
face
cast
race
fact
rice
nice
case

Sample Output 3

Yes
G - Cuboid Sum Query

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

配点 : 400

問題文

正整数 N と、 1 \leq x,y,z \leq N を満たす整数の組 (x,y,z) に対して、整数 A_{x,y,z} が与えられます。

次の形式の Q 個のクエリが与えられるので、それぞれに答えてください。

i 個目 (1 \leq i \leq Q) のクエリでは 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N,1 \leq Lz_i \leq Rz_i \leq N をすべて満たす整数の組 (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) が与えられるので、

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}

を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x,y,z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数

入力

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

出力

Q 行出力せよ。 i 行目には i 個目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

10
26

1 個目のクエリについて、求めるべき値は A_{1,2,1}+A_{2,2,1}=3+7=10 です。よって、10 を出力します。

2 個目のクエリについて、求めるべき値は A_{2,1,1}+A_{2,1,2}+A_{2,2,1}+A_{2,2,2}=5+6+7+8=26 です。よって、26 を出力します。


入力例 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

出力例 2

687
3917
551
1631
5180
3311
1010
4326

Score : 400 points

Problem Statement

You are given a positive integer N, and an integer A_{x,y,z} for each triple of integers (x, y, z) such that 1 \leq x, y, z \leq N.

You will be given Q queries in the following format, which must be processed in order.

For the i-th query (1 \leq i \leq Q), you are given a tuple of integers (Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i) such that 1 \leq Lx_i \leq Rx_i \leq N, 1 \leq Ly_i \leq Ry_i \leq N, and 1 \leq Lz_i \leq Rz_i \leq N. Find:

\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \leq 2 \times 10^{5}
  • 0 \leq A_{x,y,z} \leq 999 (1 \leq x, y, z \leq N)
  • 1 \leq Lx_i \leq Rx_i \leq N (1 \leq i \leq Q)
  • 1 \leq Ly_i \leq Ry_i \leq N (1 \leq i \leq Q)
  • 1 \leq Lz_i \leq Rz_i \leq N (1 \leq i \leq Q)
  • All input values are integers.

Input

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

N
A_{1,1,1} A_{1,1,2} \ldots A_{1,1,N}
A_{1,2,1} A_{1,2,2} \ldots A_{1,2,N}
\vdots
A_{1,N,1} A_{1,N,2} \ldots A_{1,N,N}
A_{2,1,1} A_{2,1,2} \ldots A_{2,1,N}
A_{2,2,1} A_{2,2,2} \ldots A_{2,2,N}
\vdots
A_{2,N,1} A_{2,N,2} \ldots A_{2,N,N}
\vdots
A_{N,1,1} A_{N,1,2} \ldots A_{N,1,N}
A_{N,2,1} A_{N,2,2} \ldots A_{N,2,N}
\vdots
A_{N,N,1} A_{N,N,2} \ldots A_{N,N,N}
Q
Lx_1 Rx_1 Ly_1 Ry_1 Lz_1 Rz_1
Lx_2 Rx_2 Ly_2 Ry_2 Lz_2 Rz_2
\vdots
Lx_Q Rx_Q Ly_Q Ry_Q Lz_Q Rz_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

10
26

For the 1st query, the sought value is A_{1,2,1} + A_{2,2,1} = 3 + 7 = 10. Thus, print 10.

For the 2nd query, the sought value is A_{2,1,1} + A_{2,1,2} + A_{2,2,1} + A_{2,2,2} = 5 + 6 + 7 + 8 = 26. Thus, print 26.


Sample Input 2

3
733 857 714
956 208 257
123 719 648
840 881 245
245 112 746
306 942 694
58 870 849
13 208 789
687 906 783
8
3 3 3 3 1 1
1 3 2 3 3 3
2 2 2 3 1 1
1 3 1 1 1 1
2 3 2 3 2 3
1 2 1 1 1 2
3 3 2 2 1 3
1 2 2 3 2 3

Sample Output 2

687
3917
551
1631
5180
3311
1010
4326
H - At Least One

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

配点 : 500

問題文

整数 M および N 個の整数の組 (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N) が与えられます。
すべての i について 1 \leq A_i \lt B_i \leq M が成り立っています。

次の条件を満たす数列 S良い数列と呼びます。

  • S は数列 (1,2,3,..., M) の連続部分列である。
  • すべての i について SA_i, B_i の少なくとも一方を含んでいる。

長さ k の良い数列としてあり得るものの個数を f(k) とします。
f(1), f(2), \dots, f(M) を列挙してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq M
  • 入力される値はすべて整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを以下の形式で出力せよ。

f(1) f(2) \dots f(M)

入力例 1

3 5
1 3
1 4
2 5

出力例 1

0 1 3 2 1

良い数列としてあり得るものを列挙すると次のようになります。

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

入力例 2

1 2
1 2

出力例 2

2 1

入力例 3

5 9
1 5
1 7
5 6
5 8
2 6

出力例 3

0 0 1 2 4 4 3 2 1

Score : 500 points

Problem Statement

You are given an integer M and N pairs of integers (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N).
For all i, it holds that 1 \leq A_i \lt B_i \leq M.

A sequence S is said to be a good sequence if the following conditions are satisfied:

  • S is a contiguous subsequence of the sequence (1,2,3,..., M).
  • For all i, S contains at least one of A_i and B_i.

Let f(k) be the number of possible good sequences of length k.
Enumerate f(1), f(2), \dots, f(M).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \lt B_i \leq M
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answers in the following format:

f(1) f(2) \dots f(M)

Sample Input 1

3 5
1 3
1 4
2 5

Sample Output 1

0 1 3 2 1

Here is the list of all possible good sequences.

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

Sample Input 2

1 2
1 2

Sample Output 2

2 1

Sample Input 3

5 9
1 5
1 7
5 6
5 8
2 6

Sample Output 3

0 0 1 2 4 4 3 2 1
I - ABCBA

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

配点 : 500

問題文

S を接頭辞に持つ最短の回文を 1 つ求めてください。

制約

  • S は英大文字からなる長さ 1 以上 500000 以下の文字列

入力

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

S

出力

答えを出力せよ。
解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

ABC

出力例 1

ABCBA

ABCBAS= ABC を接頭辞に持つ最短の回文です。


入力例 2

Z

出力例 2

Z

ZS= Z を接頭辞に持つ最短の回文です。


入力例 3

TREE

出力例 3

TREERT

TREERTS= TREE を接頭辞に持つ最短の回文です。

Score : 500 points

Problem Statement

Find one shortest palindrome that has S as its prefix.

Constraints

  • S is a string of length between 1 and 500000, inclusive, consisting of uppercase English letters.

Input

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

S

Output

Print the answer.
If multiple solutions exist, any of them is accepted.


Sample Input 1

ABC

Sample Output 1

ABCBA

ABCBA is a shortest palindrome that has S= ABC as its prefix.


Sample Input 2

Z

Sample Output 2

Z

Z is a shortest palindrome that has S= Z as its prefix.


Sample Input 3

TREE

Sample Output 3

TREERT

TREERT is a shortest palindrome that has S= TREE as its prefix.