A - Not Overflow

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

整数 N が与えられます。 N-2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力してください。

制約

  • -2^{63} \leq N < 2^{63}
  • N は整数である。

入力

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

N

出力

N-2^{31} 以上かつ 2^{31} 未満ならば Yes を、そうでないならば No を出力せよ。


入力例 1

10

出力例 1

Yes

10-2^{31} 以上かつ 2^{31} 未満であるので、Yes を出力します。


入力例 2

-9876543210

出力例 2

No

-9876543210-2^{31} 未満であるので、No を出力します。


入力例 3

483597848400000

出力例 3

No

4835978484000002^{31} 以上であるので、No を出力します。

Score : 100 points

Problem Statement

You are given an integer N. If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.

Constraints

  • -2^{63} \leq N < 2^{63}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is between -2^{31} and 2^{31}-1 (inclusive), print Yes; otherwise, print No.


Sample Input 1

10

Sample Output 1

Yes

10 is between -2^{31} and 2^{31}-1, so Yes should be printed.


Sample Input 2

-9876543210

Sample Output 2

No

-9876543210 is less than -2^{31}, so No should be printed.


Sample Input 3

483597848400000

Sample Output 3

No

483597848400000 is greater than 2^{31}-1, so No should be printed.

B - Matrix Transposition

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

HW 列の行列 A が与えられます。
A の上から i 行目、左から j 列目の要素は A_{i,j} です。

ここで、WH 列の行列 B を、上から i 行目、左から j 列目の要素が A_{j,i} と一致するような行列として定めます。
すなわち、BA の転置行列です。

B を出力してください。

制約

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

入力

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

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

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

B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}

入力例 1

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

出力例 1

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

たとえば A_{2,1}=4 なので、転置行列 B の上から 1 行目、左から 2 列目の要素は 4 になります。


入力例 2

2 2
1000000000 1000000000
1000000000 1000000000

出力例 2

1000000000 1000000000
1000000000 1000000000

Score : 200 points

Problem Statement

You are given an H-by-W matrix A.
The element at the i-th row from the top and j-th column from the left of A is A_{i,j}.

Let B be a W-by-H matrix whose element at the i-th row from the top and j-th column from the left equals A_{j, i}.
That is, B is the transpose of A.

Print B.

Constraints

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

Input

Input is given from Standard Input in the following format:

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

Output

Print B in the following format:

B_{1,1} B_{1,2} \ldots B_{1,H}
B_{2,1} B_{2,2} \ldots B_{2,H}
\vdots
B_{W,1} B_{W,2} \ldots B_{W,H}

Sample Input 1

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

Sample Output 1

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

For example, we have A_{2,1}=4, so the element at the 1-st row from the top and 2-nd column from the left of the transpose B is 4.


Sample Input 2

2 2
1000000000 1000000000
1000000000 1000000000

Sample Output 2

1000000000 1000000000
1000000000 1000000000
C - kasaka

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

英小文字からなる文字列 S が与えられます。 S の先頭に a をいくつか( 0 個でも良い)つけ加えて回文にすることができるか判定してください。

ただし、長さ N の文字列 A=A_1A_2\ldots A_N が回文であるとは、すべての 1\leq i\leq N について A_i=A_{N+1-i} が成り立っていることをいいます。

制約

  • 1 \leq \lvert S \rvert \leq 10^6
  • S は英小文字のみからなる。

入力

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

S

出力

S の先頭に a をいくつかつけ加えて回文にすることができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

kasaka

出力例 1

Yes

kasaka の先頭に a1 つ付け加えることによって、akasaka となり回文となるため Yes を出力します。


入力例 2

atcoder

出力例 2

No

atcoder の先頭に a をいくつ付け加えても回文となる事はありません。


入力例 3

php

出力例 3

Yes

php はそれ自体回文です。S の先頭に付け加える a0 個でも許されるため、Yes を出力します。

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Determine whether adding some number of a's (possibly zero) at the beginning of S can make it a palindrome.

Here, a string of length N, A=A_1A_2\ldots A_N, is said to be a palindrome when A_i=A_{N+1-i} for every 1\leq i\leq N.

Constraints

  • 1 \leq \lvert S \rvert \leq 10^6
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If adding some number of a's (possibly zero) at the beginning of S can make it a palindrome, print Yes; otherwise, print No.


Sample Input 1

kasaka

Sample Output 1

Yes

By adding one a at the beginning of kasaka, we have akasaka, which is a palindrome, so Yes should be printed.


Sample Input 2

atcoder

Sample Output 2

No

Adding any number of a's at the beginning of atcoder does not make it a palindrome.


Sample Input 3

php

Sample Output 3

Yes

php itself is a palindrome. Adding zero a's at the beginning of S is allowed, so Yes should be printed.

D - LR insertion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 個の 0 のみからなる数列 A=(0) があります。
また、LR のみからなる長さ N の文字列 S=s_1s_2\ldots s_N が与えられます。

i=1,2,\ldots ,N の順番で、次の操作を行います。

  • s_iL のとき、A 内にある i-1 のすぐ左に i を挿入する
  • s_iR のとき、A 内にある i-1 のすぐ右に i を挿入する

最終的な A を求めてください。

制約

  • 1\leq N \leq 5\times 10^5
  • N は整数である
  • |S| = N
  • s_iLR のいずれかである

入力

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

N
S

出力

最終的な A を空白区切りで出力せよ。


入力例 1

5
LRRLR

出力例 1

1 2 4 5 3 0

はじめ、A=(0) です。
s_1L なので、A=(1,0) となります。
s_2R なので、A=(1,2,0) となります。
s_3R なので、A=(1,2,3,0) となります。
s_4L なので、A=(1,2,4,3,0) となります。
s_5R なので、A=(1,2,4,5,3,0) となります。


入力例 2

7
LLLLLLL

出力例 2

7 6 5 4 3 2 1 0

Score : 400 points

Problem Statement

There is a sequence that contains one 0, A=(0).
Additionally, you are given a string of length N, S=s_1s_2\ldots s_N, consisting of L and R.

For each i=1, 2, \ldots, N in this order, the following will be done.

  • If s_i is L, insert i to the immediate left of i-1 in A.
  • If s_i is R, insert i to the immediate right of i-1 in A.

Find the final contents of A.

Constraints

  • 1\leq N \leq 5\times 10^5
  • N is an integer.
  • |S| = N
  • s_i is L or R.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final contents of A, separated by spaces.


Sample Input 1

5
LRRLR

Sample Output 1

1 2 4 5 3 0

Initially, A=(0).
S_1 is L, which makes it A=(1,0).
S_2 is R, which makes it A=(1,2,0).
S_3 is R, which makes it A=(1,2,3,0).
S_4 is L, which makes it A=(1,2,4,3,0).
S_5 is R, which makes it A=(1,2,4,5,3,0).


Sample Input 2

7
LLLLLLL

Sample Output 2

7 6 5 4 3 2 1 0
E - Skiing

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder スキー場には広場 1 、広場 2\ldots 、広場 NN 個の広場があり、広場 i の標高は H_i です。 また、2 つの広場を双方向に結ぶ M 本の坂があり、i (1 \leq i \leq M) 本目の坂は広場 U_i と広場 V_i を双方向に結んでいます。どの 2 つの広場の間もいくつかの坂を使って移動することができます。

高橋君は坂を使うことによってのみ広場の間を移動でき、坂を通るごとに楽しさが変化します。具体的には広場 X と広場 Y を直接結ぶ坂を使って広場 X から広場 Y まで移動したとき次のように楽しさが変化します。

  • 広場 X が広場 Y より標高が真に高い場合、その標高差、すなわち H_X-H_Y だけ楽しさが増加する。
  • 広場 X が広場 Y より標高が真に低い場合、その標高差の 2 倍、すなわち 2(H_Y-H_X) だけ楽しさが減少する。
  • 広場 X と広場 Y の標高が等しい場合、楽しさは変化しない。

楽しさは負の値になることもあります。

最初、高橋君は広場 1 におり、楽しさは 0 です。 高橋君はいくつかの坂( 0 本でも良い)を移動した後に好きな広場で行動を終えることができるとしたとき、行動を終えた時点の高橋君の楽しさとしてありうる最大の値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq \min( 2\times 10^5,\frac{N(N-1)}{2})
  • 0 \leq H_i\leq 10^8 (1 \leq i \leq N)
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • i \neq j ならば (U_i,V_i) \neq (U_j, V_j)
  • 入力はすべて整数である。
  • どの 2 つの広場の間もいくつかの坂を使って移動することができる。

入力

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

N M
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

答えを出力せよ。


入力例 1

4 4
10 8 12 5
1 2
1 3
2 3
3 4

出力例 1

3

広場 1 \to 広場 3 \to 広場 4 と移動したとき、楽しさは次のように変化します。

  • 広場 1(標高 10 )から坂を使って広場 3(標高 12 )へ移動します。楽しさは 2\times (12-10)=4 だけ減少し、0-4=-4 になります。
  • 広場 3(標高 12 )から坂を使って広場 4(標高 5 )へ移動します。楽しさは 12-5=7 だけ増加し、-4+7=3 になります。

ここで行動を終了したとき終了時の楽しさは 3 であり、このときが最大となります。


入力例 2

2 1
0 10
1 2

出力例 2

0

一度も移動を行わない時、楽しさが最大となります。

Score : 500 points

Problem Statement

AtCoder Ski Area has N open spaces called Space 1, Space 2, \ldots, Space N. The altitude of Space i is H_i. There are M slopes that connect two spaces bidirectionally. The i-th slope (1 \leq i \leq M) connects Space U_i and Space V_i. It is possible to travel between any two spaces using some slopes.

Takahashi can only travel between spaces by using slopes. Each time he goes through a slope, his happiness changes. Specifically, when he goes from Space X to Space Y by using the slope that directly connects them, his happiness changes as follows.

  • If the altitude of Space X is strictly higher than that of Space Y, the happiness increases by their difference: H_X-H_Y.
  • If the altitude of Space X is strictly lower than that of Space Y, the happiness decreases by their difference multiplied by 2: 2(H_Y-H_X).
  • If the altitude of Space X is equal to that of Space Y, the happiness does not change.

The happiness may be a negative value.

Initially, Takahashi is in Space 1, and his happiness is 0. Find his maximum possible happiness after going through any number of slopes (possibly zero), ending in any space.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq \min( 2\times 10^5,\frac{N(N-1)}{2})
  • 0 \leq H_i\leq 10^8 (1 \leq i \leq N)
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • (U_i,V_i) \neq (U_j, V_j) if i \neq j.
  • All values in input are integers.
  • It is possible to travel between any two spaces using some slopes.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print the answer.


Sample Input 1

4 4
10 8 12 5
1 2
1 3
2 3
3 4

Sample Output 1

3

If Takahashi takes the route Space 1 \to Space 3 \to Space 4, his happiness changes as follows.

  • When going from Space 1 (altitude 10) to Space 3 (altitude 12), it decreases by 2\times (12-10)=4 and becomes 0-4=-4.
  • When going from Space 3 (altitude 12) to Space 4 (altitude 5), it increases by 12-5=7 and becomes -4+7=3.

If he ends the travel here, the final happiness will be 3, which is the maximum possible value.


Sample Input 2

2 1
0 10
1 2

Sample Output 2

0

His happiness is maximized by not moving at all.

F - |LIS| = 3

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

以下の条件を全て満たす数列の個数を、998244353 で割った余りを求めてください。

  • 数列の長さが N
  • 数列の各項は 1 以上 M 以下の整数
  • 最長増加部分列の長さがちょうど 3

注記

数列の部分列とは、数列から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結して得られる数列のことをいいます。
例えば、(10,30)(10,20,30) の部分列ですが、(20,10)(10,20,30) の部分列ではありません。

数列の最長増加部分列とは、数列の狭義単調増加な部分列のうち、長さが最大のもののことをいいます。

制約

  • 3 \leq N \leq 1000
  • 3 \leq M \leq 10
  • 入力は全て整数である

入力

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

N M

出力

答えを出力せよ。


入力例 1

4 5

出力例 1

135

例えば (3,4,1,5) は条件を満たす数列です。
一方 (4,4,1,5) は最長増加部分列の長さが 2 なので条件を満たしません。


入力例 2

3 4

出力例 2

4

入力例 3

111 3

出力例 3

144980434

998244353 で割った余りを求めてください。

Score : 500 points

Problem Statement

Find the number of sequences that satisfy all of the conditions below, modulo 998244353.

  • The length is N.
  • Each of the elements is an integer between 1 and M (inclusive).
  • Its longest increasing subsequence has the length of exactly 3.

Notes

A subsequence of a sequence is the result of removing zero or more elements from it and concatenating the remaining elements without changing the order. For example, (10,30) is a subsequence of (10,20,30), while (20,10) is not a subsequence of (10,20,30).

A longest increasing subsequence of a sequence is its strictly increasing subsequence with the greatest length.

Constraints

  • 3 \leq N \leq 1000
  • 3 \leq M \leq 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.


Sample Input 1

4 5

Sample Output 1

135

One sequence that satisfies the conditions is (3,4,1,5).
On the other hand, (4,4,1,5) do not, because its longest increasing subsequence has the length of 2.


Sample Input 2

3 4

Sample Output 2

4

Sample Input 3

111 3

Sample Output 3

144980434

Find the count modulo 998244353.

G - Range Sort Query

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1,2,\ldots,N を並び替えた長さ N の順列 P=(P_1,P_2,\ldots,P_N) と整数 X が与えられます。

また、Q 個のクエリが与えられます。 i 番目のクエリは 3 つの数の組 (C_i,L_i,R_i) で表されます。各クエリでは順列 P に対して次の操作を行います。

  • C_i=1 のとき : P_{L_i},P_{L_i+1},\ldots,P_{R_i} を昇順に並び替える。
  • C_i=2 のとき : P_{L_i},P_{L_i+1},\ldots,P_{R_i} を降順に並び替える。

クエリを 1 番目から順に最後まで処理したとき、最終的な順列において P_i=X となる i を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq X \leq N
  • (P_1,P_2,\ldots,P_N)(1,2,\ldots,N) の並び替えである。
  • 1 \leq C_i \leq 2
  • 1 \leq L_i \leq R_i \leq N
  • 入力は全て整数である。

入力

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

N Q X
P_1 P_2 \ldots P_N
C_1 L_1 R_1
C_2 L_2 R_2
\vdots
C_Q L_Q R_Q

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

最初、順列は P=[1,4,5,2,3] です。 これはクエリによって次のように変化します。

  • 1 つめのクエリでは 3 番目から 5 番目の要素を昇順にソートします。順列は P=[1,4,2,3,5] となります。
  • 2 つめのクエリでは 1 番目から 3 番目の要素を降順にソートします。順列は P=[4,2,1,3,5] となります。

最終的な順列において P_3=1 であるので、3 を出力します。


入力例 2

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

出力例 2

7

最終的な順列は P=[1,2,6,5,7,4,3] となります。

Score : 600 points

Problem Statement

Given is a permutation P=(P_1,P_2,\ldots,P_N) of 1,2,\ldots,N, and an integer X.

Additionally, Q queries are given. The i-th query is represented as a triple of numbers (C_i,L_i,R_i). Each query does the following operation on the permutation P.

  • If C_i=1: sort P_{L_i},P_{L_i+1},\ldots,P_{R_i} in ascending order.
  • If C_i=2: sort P_{L_i},P_{L_i+1},\ldots,P_{R_i} in descending order.

In the final permutation P after executing all queries in the given order, find i such that P_i=X.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq X \leq N
  • (P_1,P_2,\ldots,P_N) is a permutation of (1,2,\ldots,N).
  • 1 \leq C_i \leq 2
  • 1 \leq L_i \leq R_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q X
P_1 P_2 \ldots P_N
C_1 L_1 R_1
C_2 L_2 R_2
\vdots
C_Q L_Q R_Q

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

Initially, the permutation is P=[1,4,5,2,3]. The queries change it as follows.

  • 1-st query sorts the 3-rd through 5-th elements in ascending order, making P=[1,4,2,3,5].
  • 2-nd query sorts the 1-st through 3-rd elements in descending order, making P=[4,2,1,3,5].

In the final permutation, we have P_3=1, so 3 should be printed.


Sample Input 2

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

Sample Output 2

7

The final permutation is P=[1,2,6,5,7,4,3].

Ex - Hakata

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

英小文字からなる文字列 S があります。
毎日回文のことばかりを考えている高橋博多くんは、S の部分文字列のうち回文となっているものをいくつか選び、小倉楽子さんに教えることにしました。

小倉楽子さんは、教えられた回文のうち 2 つであって、一方が他方の部分文字列になっているようなものが存在すると、怒ります。

小倉楽子さんが怒らないという条件のもとで、高橋博多くんは最大でいくつの回文を選ぶことができますか?

注記

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

制約

  • 1 \leq |S| \leq 200
  • S は英小文字からなる

入力

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

S

出力

答えを出力せよ。


入力例 1

ababb

出力例 1

3

abababbb3 つの回文を選ぶことができます。


入力例 2

xyz

出力例 2

3

xyz3 つの回文を選ぶことができます。


入力例 3

xxxxxxxxxx

出力例 3

1

Score : 600 points

Problem Statement

We have a string S consisting of lowercase English letters.
Bob just thinks about palindromes every day. He decided to choose some of the substrings of S that are palindromes and tell them to Anna.

Anna gets angry if one of the palindromes told by Bob is a substring of another.

How many palindromes can Bob choose while not making Anna angry?

Notes

A substring of S is the result of deleting zero or more characters from the beginning and the end of S.
For example, ab is a substring of abc, while ac is not a substring of abc.

Constraints

  • 1 \leq |S| \leq 200
  • S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

ababb

Sample Output 1

3

Three palindromes aba, bab, bb can be chosen.


Sample Input 2

xyz

Sample Output 2

3

Three palindromes x, y, z can be chosen.


Sample Input 3

xxxxxxxxxx

Sample Output 3

1