A - Orange

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

みかんが N 個あります。高橋君はみかんが残っている限り 1 つずつ食べていきます。ただし、みかんを 3 つ食べたら満足して食べるのをやめます。みかんはいくつ残りますか。

制約

  • 1\leq N\leq 100
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

5

出力例 1

2

高橋君はみかんを 3 つ食べると満足するので、みかんは 2 個残ります。


入力例 2

2

出力例 2

0

高橋君は満足する前にすべてのみかんを食べてしまうので、みかんは残りません。

Problem Statement

There are N oranges. He repeats eating one orange as long as there is at least one, but he will be satisfied and stop eating after eating three. How many oranges will be left?

Constraints

  • 1\leq N\leq 100
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

5

Sample Output 1

2

He will be satisfied after eating three oranges, so two oranges will be left.


Sample Input 2

2

Sample Output 2

0

He eats up all the oranges before being satisfied, so no oranges will be left.

B - Southern Island

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

とある南の島に住む子供たちは皆学校が嫌いです。 彼らは、その日の天候が悪いとすぐに学校に遅刻したり欠席したりしてしまいます。 具体的には、

  • 雨が降っている場合、学校を欠席する。
  • 雨は降っていないが風が吹いている場合、学校に遅刻して出席する。
  • 雨が降っておらず風も吹いていない場合、学校に遅刻せず出席する。

今日の天候は 2 つの整数 W,R によって表されます。 W=1 のとき今日は風が吹いており、W=0 のとき吹いていません。 同様に、R=1 のとき今日は雨が降っており、R=0 のとき降っていません。

この島の子供たちが今日学校に遅刻せず出席するならば attend を、遅刻して出席するならば late を、欠席するならば absent をそれぞれ出力してください。

制約

  • W,R はそれぞれ 0 または 1

入力

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

W R

出力

答えを出力せよ。


入力例 1

0 1

出力例 1

absent

今日は雨が降っているので、子供たちは学校を欠席します。


入力例 2

1 0

出力例 2

late

今日は雨は降っていませんが風が吹いているので、子供たちは学校に遅刻して出席します。


入力例 3

0 0

出力例 3

attend

今日は雨が降っておらず風も吹いていないので、子供たちは学校に遅刻せず出席します。

Problem Statement

The children on a southern island all hate school. In bad weather, they do not hesitate to be late for or absent from school. Specifically,

  • They will be absent from school if it is rainy.
  • They will be late for school if it is not rainy but windy.
  • They will attend school without being late if it is not rainy nor windy.

Today's weather is described by two integers W and R. It is windy today if W=1 and it is not if W=0; likewise, it is rainy today if R=1 and it is not if R=0.

Print attend if the children on the island will attend school without being late, late if they will be late, and absent if they will be absent today.

Constraints

  • Each of W and R is 0 or 1.

Input

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

W R

Output

Print the answer.


Sample Input 1

0 1

Sample Output 1

absent

It is rainy today, so they will be absent from school.


Sample Input 2

1 0

Sample Output 2

late

It is not rainy but windy today, so they will be late for school.


Sample Input 3

0 0

Sample Output 3

attend

It is not rainy nor windy today, so they will attend school without being late.

C - AEIOU

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

英小文字からなる文字列 S が与えられます。S に含まれる母音をすべて大文字に置き換えた文字列を出力してください。ただし、母音とは a, e, i, o, u5 文字を指します。

制約

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

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

AtcOdEr

atcoder に含まれる母音は a, o, e3 つなので、これらを大文字に置き換えた AtcOdEr を出力してください。


入力例 2

rhythm

出力例 2

rhythm

本問題では、y は母音ではないことに注意してください。


入力例 3

wow

出力例 3

wOw

本問題では、w は母音ではないことに注意してください。

Problem Statement

Given a string S consisting of lowercase English letters, make all the vowels uppercase and print the result. Here, vowels consist of five letters: a, e, i, o, and u.

Constraints

  • S is a string of lowercase English letters of length between 1 and 100, inclusive.

Input

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

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

AtcOdEr

atcoder contains three vowels a, o, and e. Make them uppercase to obtain AtcOdEr and print it.


Sample Input 2

rhythm

Sample Output 2

rhythm

Note that y is not a vowel in this problem.


Sample Input 3

wow

Sample Output 3

wOw

Note that w is not a vowel in this problem.

D - S+ring Cards

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

文字列が書かれたカードが N 枚与えられます。i 番目のカードには文字列 S_i が書かれています (1\leq i\leq N)

以下の条件を満たすように N 枚のカードから相異なる 3 枚のカードを選ぶことができるか判定してください。

  • 選んだ 3 枚のカードのうちある 2 枚のカードに書かれている文字列をいずれかの順番で連結したものが、残りの 1 枚のカードに書かれている文字列と一致する。

制約

  • 3\leq N\leq 10
  • N は整数
  • S_i は英小文字からなる長さ 1 以上 10 以下の文字列
  • S_i\neq S_j(i\neq j)

入力

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

N
S_1
S_2
\vdots
S_N

出力

条件を満たすような 3 枚のカードの選び方が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

4
ing
string
abc
str

出力例 1

Yes

1,2,4 枚目のカードを選び、4,1 枚目に書かれている文字列をこの順に連結させることで 2 枚目のカードに書かれている文字列と一致させることができます。


入力例 2

3
abc
def
abcabc

出力例 2

No

選ぶカードは相異なる必要があります。


入力例 3

6
vxyzbkfvmb
wjrbbfogg
jmeedirhy
bkfvmb
vxyz
acheswkrui

出力例 3

Yes

Problem Statement

You are given N cards with strings written on them. The i-th card has a string S_i written on it (1\leq i\leq N).

Determine if one can choose three distinct cards from the N cards so that:

  • the concatenation of the strings on two of the cards in any order coincides with the string written on the other.

Constraints

  • 3\leq N\leq 10
  • N is an integer.
  • S_i is a string of lowercase English letters of length between 1 and 10, inclusive.
  • S_i\neq S_j(i\neq j)

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if there is a conforming choice of three cards, and No otherwise.


Sample Input 1

4
ing
string
abc
str

Sample Output 1

Yes

Choosing the 1-st, 2-nd, and 4-th cards is valid because the concatenation of the strings written on the 4-th and 1-st in this order coincides with the string written on the 2-nd.


Sample Input 2

3
abc
def
abcabc

Sample Output 2

No

The chosen cards must be pairwise distinct.


Sample Input 3

6
vxyzbkfvmb
wjrbbfogg
jmeedirhy
bkfvmb
vxyz
acheswkrui

Sample Output 3

Yes
E - Piano

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 個の鍵盤を持つピアノがあります。左から i 番目の鍵盤を押すと A_i ヘルツの音がでます。
鍵盤は音の順に並んでいます。すなわち、A_1 < A_2 < \dots < A_N を満たします。

あなたはこのピアノを使って右手の人差し指だけで曲を弾こうとしています。
曲は B_1,B_2,\dots,B_M ヘルツの M 個の音を順に並べたものです。

鍵盤 i の次に鍵盤 j を押すには指を距離 |i-j| 動かす必要があるとき、曲の最初の音を弾いてから最後の音を弾くまでに指を動かす距離を求めてください。

制約

  • 1 \leq N \leq 3\times 10^5
  • 1 \leq M \leq 3\times 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • 鍵盤は音の順に並んでいる。すなわち、 A_1 < A_2 < \dots < A_N を満たす。
  • 曲をピアノで弾くことができる。すなわち、全ての i に対しある k が存在し、A_k=B_i を満たす。
  • 入力は全て整数である。

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

答えを出力せよ。


入力例 1

8 7
262 294 330 349 392 440 494 523
262 262 392 392 440 440 392

出力例 1

6

各音を出すための鍵盤の位置は左から 1,1,5,5,6,6,5 番目であるため、指の移動距離は順に 0,4,0,1,0,1 となり、合計 6 です。


入力例 2

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

出力例 2

92

Problem Statement

There is a piano with N keys. Key i plays an A_i-hertz tone.
The keys are in ascending order; that is, A_1 < A_2 < \dots < A_N.

You want to play a song with this piano using only your right index finger.
The song is a sequence of M tones, each of B_1,B_2,\dots, and B_M hertz, in this order.

Pressing key j after key i requires moving the finger by the distance |i-j|. Find the total distance required for the finger to move since playing the first tone until playing the last.

Constraints

  • 1 \leq N \leq 3\times 10^5
  • 1 \leq M \leq 3\times 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • The keys are in ascending order; that is, A_1 < A_2 < \dots < A_N.
  • The song can be played by the piano; in other words, for every i there exists k such that A_k=B_i.
  • 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
B_1 B_2 \dots B_M

Output

Print the answer.


Sample Input 1

8 7
262 294 330 349 392 440 494 523
262 262 392 392 440 440 392

Sample Output 1

6

You need to press keys 1,1,5,5,6,6, and 5 to play the tones, so the finger's traveling distances are 0,4,0,1,0, and 1 in order, for a total of 6.


Sample Input 2

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

Sample Output 2

92
F - Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) と長さ M の整数列 B=(B_1,B_2,\ldots,B_M) が与えられます。

AB の部分列であるか判定してください。つまり、 B から 0 個以上の要素を取り除いた後、残りの要素を元の順序で連結することで BA と一致させることができるか判定してください。

制約

  • 1\le N\le M\le 2\times 10^5
  • 1\le A_i,B_i\le 10^9
  • 入力される値は全て整数である

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

AB の部分列であるならば Yes を、そうでないならば No を出力せよ。


入力例 1

3 5
3 1 4
3 6 1 1 4

出力例 1

Yes

B から B_2,B_4 を削除すると (3,1,4) となり、 A と一致させることができます。このことから AB の部分列であることが分かるので、 Yes を出力してください。


入力例 2

2 3
5 4
4 7 5

出力例 2

No

B のどの要素を削除しても A と一致させることはできないので、 No を出力してください。


入力例 3

3 3
7 7 7
7 7 7

出力例 3

Yes

元から AB が一致している場合も答えは Yes です。


入力例 4

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

出力例 4

Yes

Problem Statement

You are given a length-N integer sequence A=(A_1,A_2,\ldots,A_N) and a length-M integer sequence B=(B_1,B_2,\ldots,B_M).

Determine if A is a subsequence of B. In other words, determine if one can remove zero or more elements of B and concatenate the remaining elements without changing the order to make B coincide with A.

Constraints

  • 1\le N\le M\le 2\times 10^5
  • 1\le A_i,B_i\le 10^9
  • All input values are integers.

Input

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print Yes if A is a subsequence of B, and No otherwise.


Sample Input 1

3 5
3 1 4
3 6 1 1 4

Sample Output 1

Yes

Removing B_2 and B_4 from B yields (3,1,4), which coincides with A. Thus, A turns out to be a subsequence of B, so print Yes.


Sample Input 2

2 3
5 4
4 7 5

Sample Output 2

No

Removing any element from B does not yield A, so print No.


Sample Input 3

3 3
7 7 7
7 7 7

Sample Output 3

Yes

The answer is Yes even if A and B are originally equal.


Sample Input 4

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

Sample Output 4

Yes
G - Level Up

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

あなたはゲームをプレイしています。

このゲームでは、レベル x のキャラクタをレベル x+1 に上げるためには Ax+B 個の宝石を消費する必要があります。

宝石を N 個持っているとき、レベル 0 のキャラクタのレベルをいくつまで上げることができますか?

Q 個のテストケースに答えてください。

制約

  • 1 \leq Q \leq 10^5
  • 1 \leq A,B \leq 10^9
  • 0 \leq N \leq 10^{18}
  • 入力は全て整数である

入力

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

Q
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_Q

\mathrm{testcase}_ii 番目のテストケースを表し、各テストケースは以下の形式で与えられる。

N A B

出力

答えを出力せよ。


入力例 1

3
10 2 3
1000000000000000000 1 1
0 1000000000 1000000000

出力例 1

2
1414213561
0

1 つ目のテストケースにおいて、レベルを上げるために必要な宝石の個数は以下の通りです。

  • レベル 0 からレベル 1 に上げるために 3
  • レベル 1 からレベル 2 に上げるために 5
  • レベル 2 からレベル 3 に上げるために 7

宝石を 10 個持っているとき、レベル 0 のキャラクタをレベル 2 まで上げることができます。

Problem Statement

You are playing a video game.

In this game, raising a character from level x to level (x+1) requires consuming (Ax+B) gems.

If you have N gems, to what level can you raise a level-0 character?

Answer Q test cases.

Constraints

  • 1 \leq Q \leq 10^5
  • 1 \leq A,B \leq 10^9
  • 0 \leq N \leq 10^{18}
  • All input values are integers.

Input

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

Q
\mathrm{testcase}_1
\mathrm{testcase}_2
\vdots
\mathrm{testcase}_Q

\mathrm{testcase}_i represents the i-th test case given in the following format:

N A B

Output

Print the answer.


Sample Input 1

3
10 2 3
1000000000000000000 1 1
0 1000000000 1000000000

Sample Output 1

2
1414213561
0

For the first test case, the number of gems required to raise the level is as follows:

  • 3 gems to raise it from level 0 to level 1.
  • 5 gems to raise it from level 1 to level 2.
  • 7 gems to raise it from level 2 to level 3.

If you have 10 gems, you can raise a level-0 character up to level 2.

H - Bus Line

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

左右一列に N 個のバス停が並んでいます。バス停は左から 1,2,\ldots,N と番号が付けられています。

M 本のバス路線があります。i 番目 (1 \leq i \leq M) のバス路線はバス停 L_i からバス停 R_i までを往復するバス路線で、バス停 L_i からバス停 R_i までの間の全てのバス停に停まります。

各バス停について、そのバス停に停まるバス路線の本数を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq L_i,R_i \leq N (1 \leq i \leq M)
  • L_i \neq R_i (1 \leq i \leq M)
  • 入力は全て整数である

入力

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

出力

N 行出力せよ。i 行目には、バス停 i に停まるバス路線の本数を出力せよ。


入力例 1

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

出力例 1

2 4 4 3 2

1 番目のバス路線はバス停 1,2,3 に、 2 番目のバス路線はバス停 2,3,4,5 に、 3 番目のバス路線はバス停 1,2,3,4 に、 4 番目のバス路線はバス停 2,3 に、 5 番目のバス路線はバス停 4,5 に停まります。

つまり、バス停 1 に停まる路線は 1,3 番目の路線、バス停 2 に停まる路線は 1,2,3,4 番目の路線、バス停 3 に停まる路線は 1,2,3,4 番目の路線、バス停 4 に停まる路線は 2,3,5 番目の路線、バス停 5 に停まる路線は 2,5 番目の路線です。

よって、出力すべき値は順に 2,4,4,3,2 となります。


入力例 2

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

出力例 2

2 5 4 4 4 4 6 4 2 0

Problem Statement

N bus stops are arranged from left to right, numbered 1,2,\ldots,N in order.

There are M bus routes. Route i (1 \leq i \leq M) operates bidirectionally between bus stops L_i and R_i, stopping at every bus stop in between, including these two.

Find how many routes stop at each bus stop.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq L_i,R_i \leq N (1 \leq i \leq M)
  • L_i \neq R_i (1 \leq i \leq M)
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print N lines. The i-th line should contain the number of routes that stop at bus stop i.


Sample Input 1

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

Sample Output 1

2 4 4 3 2

Route 1 stops at bus stops 1,2,3; route 2 at 2,3,4,5; route 3 at 1,2,3,4; route 4 at 2,3; and route 5 at 4,5.

In other words, routes 1 and 3 stop at bus stop 1; routes 1,2,3,4 stop at 2; routes 1,2,3,4 stop at 3; route 2,3,5 stop at 4; and routes 2,5 stop at 5.

Thus, the numbers to be printed are 2,4,4,3,2, in order.


Sample Input 2

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

Sample Output 2

2 5 4 4 4 4 6 4 2 0
I - Big X

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

各辺に 10^9 個のマスが並ぶマス目があり、最初は全てのマスが白です。このうち上から i マス目、左から j マス目を (i,j) と呼びます。
このマス目に操作を N 回行います。そのうち i 回目は次の通りです。

  • (P_i,Q_i) を中心として X 字状にマス目を黒く塗る。即ち、全ての整数 k について以下の 2 種類のマスを黒く塗る。
    • 1 \le P_i+k \le 10^9 かつ 1 \le Q_i+k \le 10^9 ならば (P_i+k,Q_i+k) を黒く塗る。
    • 1 \le P_i+k \le 10^9 かつ 1 \le Q_i-k \le 10^9 ならば (P_i+k,Q_i-k) を黒く塗る。

操作後のマス目のうち、上から A マス目から B マス目、左から C マス目から D マス目からなる部分長方形を出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 1 \le P_i, Q_i \le 10^9
  • 1 \le A \le B \le 10^9
  • 1 \le C \le D \le 10^9
  • (B-A+1) \times (D-C+1) \le 2 \times 10^5

入力

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

N A B C D
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

出力

全体で B-A+1 行出力せよ。そのうち i 行目には D-C+1 文字の文字列を出力せよ。
出力のうち i 行目の j 文字目は、次の通りにせよ。

  • (A+i-1,C+j-1) が白いなら、 .
  • (A+i-1,C+j-1) が黒いなら、 #

入力例 1

3 2 6 3 9
3 5
5 1
4 9

出力例 1

.#.##..
#.#..#.
.#.#..#
#...##.
....##.

最初、マス目の左上は次の通りです。

..........
..........
..........
..........
..........
..........
..........

1 回目の操作で、 (3,5) を中心として X 字状にマス目を塗ります。操作後のマス目は次の通りです。

..#...#...
...#.#....
....#.....
...#.#....
..#...#...
.#.....#..
#.......#.

2 回目の操作で、 (5,1) を中心として X 字状にマス目を塗ります。操作後のマス目は次の通りです。

..#.#.#...
...#.#....
..#.#.....
.#.#.#....
#.#...#...
.#.....#..
#.#.....#.

3 回目の操作で、 (4,9) を中心として X 字状にマス目を塗ります。操作後のマス目は次の通りです。

..#.###...
...#.##...
..#.#..#.#
.#.#.#..#.
#.#...##.#
.#....##..
#.#..#..#.

このうち、上から 2 マス目から 6 マス目、左から 3 マス目から 9 マス目を出力します。その結果は次の通りです。

.#.##..
#.#..#.
.#.#..#
#...##.
....##.

Problem Statement

There is a grid with 10^9 cells on each side, and all the cells are initially white. The cell in the i-th row from the top and j-th column from the left is called (i,j).
We will perform N operations. The i-th operation is as follows:

  • Paint cells in an X shape centered at (P_i,Q_i). More formally, paint the following cells black for all integers k:
    • Paint (P_i+k,Q_i+k) black if 1 \le P_i+k \le 10^9 and 1 \le Q_i+k \le 10^9.
    • Paint (P_i+k,Q_i-k) black if 1 \le P_i+k \le 10^9 and 1 \le Q_i-k \le 10^9.

Print the subrectangle in the A-th through B-th rows form the top and the C-th through D-th columns from the left in the resulting grid.

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le P_i, Q_i \le 10^9
  • 1 \le A \le B \le 10^9
  • 1 \le C \le D \le 10^9
  • (B-A+1) \times (D-C+1) \le 2 \times 10^5

Input

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

N A B C D
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

Output

Print (B-A+1) lines. The i-th line should be a string of length (D-C+1), whose j-th character is:

  • . if (A+i-1,C+j-1) is white;
  • # if (A+i-1,C+j-1) is black.

Sample Input 1

3 2 6 3 9
3 5
5 1
4 9

Sample Output 1

.#.##..
#.#..#.
.#.#..#
#...##.
....##.

The top-left cells initially look like this:

..........
..........
..........
..........
..........
..........
..........

The first operation paints cells in an X shape centered at (3,5), resulting in the following:

..#...#...
...#.#....
....#.....
...#.#....
..#...#...
.#.....#..
#.......#.

The second operation paints cells in an X shape centered at (5,1), resulting in the following:

..#.#.#...
...#.#....
..#.#.....
.#.#.#....
#.#...#...
.#.....#..
#.#.....#.

The third operation paints cells in an X shape centered at (4,9), resulting in the following:

..#.###...
...#.##...
..#.#..#.#
.#.#.#..#.
#.#...##.#
.#....##..
#.#..#..#.

Print the cells in the 2-th through 6-th rows form the top and the 3-th through 9-th columns from the left, as shown below:

.#.##..
#.#..#.
.#.#..#
#...##.
....##.
J - XOR of XOR

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 頂点の木 T があります。辺 i (1\le i\le N - 1) は頂点 u_i と頂点 v_i を結んでおり、重みは w_i です。

1\le i < j\le N を満たす整数の組 (i,j) に対し、 f(i,j) を木 T の頂点 i から頂点 j への単純パスに含まれる辺の重みの総 XOR と定義します。

1\le i < j\le N を満たす整数の組 (i,j) 全てに対する f(i,j) の総 XOR を求めてください。

XOR とは 非負整数 A,B の XOR A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k \, (k \geq 0) の位の数は、A,B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、 3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101=110)。
一般に k 個の整数 p_1, \dots, p_k の総 XOR は (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) と定義され、これは p_1, \dots, p_k の順番によらないことが証明できます。

制約

  • 2\le N\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • 与えられるグラフは木である
  • 入力される値は全て整数である

入力

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

N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}

出力

答えを出力せよ。


入力例 1

3
2 3 4
1 2 3

出力例 1

0

例えば、頂点 1 から頂点 3 への単純パスに含まれる辺の重みは 3,4 なので f(1,3)34 の XOR である 7 となります。

他にも f(1,2)=3, f(2,3)=4 が分かるので、答えは 7,3,4 の総 XORである 0 です。


入力例 2

4
1 4 8
1 2 2
1 3 5

出力例 2

15

入力例 3

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

出力例 3

13

Problem Statement

There is an N-vertex tree T. Edge i (1\le i\le N - 1) connects vertices u_i and v_i and has a weight w_i.

For an integer pair (i,j) with 1\le i < j\le N, let f(i,j) be the total XOR of the weights of the edges contained in the simple path from vertex i to vertex j of the tree T.

Find the total XOR of f(i,j) over all integer pairs (i,j) with 1\le i < j\le N.

What is XOR? The XOR A \oplus B of non-negative integers A and B is defined as follows.
  • The 2^ks place (k \geq 0) of A \oplus B in its binary representation is 1 if exactly one of 2^ks places of A and B in their binary representations is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101=110).
In general, the total XOR of k integers p_1, \dots, and p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k), which can be proven independent of the order of p_1, \dots, p_k.

Constraints

  • 2\le N\le 2\times 10^5
  • 1\le u_i < v_i\le N
  • 0\le w_i< 2^{30}
  • The given graph is a tree.
  • All input values are integers.

Input

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

N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}

Output

Print the answer.


Sample Input 1

3
2 3 4
1 2 3

Sample Output 1

0

For example, the weights of the edges contained in the simple path from vertex 1 to vertex 3 are 3 and 4, so f(1,3) is the XOR of 3 and 4, which is 7.

We can also find f(1,2)=3 and f(2,3)=4, so the answer is the total XOR of 7,3, and 4, which is 0.


Sample Input 2

4
1 4 8
1 2 2
1 3 5

Sample Output 2

15

Sample Input 3

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

Sample Output 3

13
K - Otemae

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

正整数 N と、 (1,2,\dots,N) の並べ替え A=(A_1,A_2,\dots,A_N) が与えられます。各 i=1,2,\dots,N に対して、以下の値を求めてください。

  • 1 \leq j < i かつ A_j > A_i を満たす整数 j が存在するならば、そのような j の最大値。存在しなければ -1

制約

  • 1 \leq N \leq 5 \times 10^5
  • A(1,2,\dots,N) の並べ替え

入力

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

N
A_1 A_2 \dots A_N

出力

i=1,2,\dots,N に対する答えをこの順で空白区切りで 1 行に出力せよ。


入力例 1

4
3 1 4 2

出力例 1

-1 1 -1 3
  • i=1: 条件を満たす j は存在しないので、答えは -1 です。
  • i=2: j=1 が条件を満たすので、答えは 1 です。
  • i=3: 条件を満たす j は存在しないので、答えは -1 です。
  • i=4: j=1,3 が条件を満たします。そのうち、大きい方の 3 が答えです。

入力例 2

7
3 7 2 1 6 5 4

出力例 2

-1 -1 2 3 2 5 6

Problem Statement

You are given a positive integer N and a permutation A=(A_1,A_2,\dots,A_N) of (1,2,\dots,N). Find the following value for each i=1,2,\dots,N:

  • the maximum integer j such that 1 \leq j < i and A_j > A_i if such an integer exists, or -1 otherwise.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • A is a permutation of (1,2,\dots,N).

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answers for i=1,2,\dots,N in this order, with spaces in between.


Sample Input 1

4
3 1 4 2

Sample Output 1

-1 1 -1 3
  • i=1: No j satisfies the condition, so the answer is -1.
  • i=2: j=1 satisfies the condition, so the answer is 1.
  • i=3: No j satisfies the condition, so the answer is -1.
  • i=4: j=1,3 satisfy the condition, so the answer is 3, the larger of them.

Sample Input 2

7
3 7 2 1 6 5 4

Sample Output 2

-1 -1 2 3 2 5 6
L - Deterministic League

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

1 から N までの番号がついた N 人の人がいます。それぞれの人の強さは 1 以上 N 以下の整数で表され、強さは全員異なります。

2 人が対戦すると、強さの値が大きい方が必ず勝利します。

この N 人で総当たり戦をした際の途中結果の表が与えられます。これは N 個の文字列 S_1,S_2,\ldots,S_N として表され、 以下の条件を満たします。

  • S_ij 文字目が o のとき:人 i と人 j が対戦し、人 i が勝利した。
  • S_ij 文字目が x のとき:人 i と人 j が対戦し、人 j が勝利した。
  • S_ij 文字目が ? のとき:人 i と人 j はまだ対戦していない。
  • S_ij 文字目が - のとき: i=j である。

矛盾の生じないように S_1,S_2,\ldots,S_N?ox に置き換えてください。ただし、どのように置き換えても矛盾が生じる場合は、そのことを指摘してください。

より厳密には、以下の条件を満たすように S_1,S_2,\ldots,S_N?ox に置き換えられるか判定し、置き換えられる場合はその一例を示してください。

  • 以下の条件を満たす (1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N) が存在する。
    • i\neq j を満たす整数の組 (i,j) に対し、 P_i>P_j ならば S_ij 文字目は o で、 P_i<P_j ならば S_ij 文字目は x である。

制約

  • 1\le N\le 10^3
  • N は整数
  • S_1,S_2,\ldots,S_Nox?- からなる長さ N の文字列
  • S_ij 文字目が o ならば S_ji 文字目は x
  • S_ij 文字目が x ならば S_ji 文字目は o
  • S_ii 文字目は -
  • i\neq j ならば S_ij 文字目は - ではない

入力

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

N
S_1
S_2
\vdots
S_N

出力

どのように置き換えても矛盾する場合は No を出力せよ。

矛盾しないような置き換え方が存在する場合は、まず Yes と一行に出力し、その後に矛盾のないように置き換えた後の S_1,S_2,\ldots,S_N を改行区切りで順に出力せよ。

条件を満たす解が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

4
-?x?
?-o?
ox-?
???-

出力例 1

Yes
-xxx
o-oo
ox-o
oxx-

例えば、人 1,2,3,4 の強さをそれぞれ 1,4,3,2 とすると途中結果の表と勝敗に矛盾が生じません。

その他にも、例えば以下の出力も条件を満たします。

Yes
-xxx
o-ox
ox-x
ooo-

入力例 2

4
-oxo
x-xx
oo-o
xox-

出力例 2

Yes
-oxo
x-xx
oo-o
xox-

? が存在しない場合もあります。


入力例 3

3
-ox
x-o
ox-

出力例 3

No

1,2,3 の強さをどのように決めても途中結果の表と矛盾が生じます。


入力例 4

6
-x?x??
o-o??o
?x-???
o??-o?
???x-o
?x??x-

出力例 4

Yes
-xxxxx
o-oxxo
ox-xxx
ooo-oo
ooox-o
oxoxx-

Problem Statement

There are N people numbered 1 through N. The strength of each person is represented by an integer between 1 and N, and all strengths are distinct.

If two people battles, the one with the larger strength always wins.

You are given preliminary results of a round-robin competition between the N people. They are represented by N strings S_1,S_2,\ldots, and S_N and satisfy the following conditions:

  • If the j-th character of S_i is o: person i has battled person j and won.
  • If the j-th character of S_i is x: person i has battled person j and lost.
  • If the j-th character of S_i is ?: person i has not battled person j yet.
  • If the j-th character of S_i is -: it holds that i=j.

Fill each ? in S_1,S_2,\ldots, and S_N with o or x consistently. If any assignment causes a contradiction, report that fact.

More precisely, determine if one can replace each occurrence of ? in S_1,S_2,\ldots, and S_N with either o or x subject to the following condition, and print one such assignment if it is possible.

  • There exists a permutation P=(P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) such that:
    • for every integer pair (i,j) with i\neq j, the j-th character of S_i is o if P_i>P_j and x if P_i<P_j.

Constraints

  • 1\le N\le 10^3
  • N is an integer.
  • Each of S_1,S_2,\ldots, and S_N is a string of length N consisting of o, x, ?, and -.
  • If the j-th character of S_i is o, then the i-th character of j is x.
  • If the j-th character of S_i is x, then the i-th character of j is o.
  • The i-th character of S_i is -.
  • If i\neq j, then the j-th character of S_i is not -.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print No if any assignment causes a contradiction.

If there is a consistent assignment, first print Yes in a single line, and then S_1,S_2,\ldots, and S_N resulting from a consistent assignment, with newlines in between.

If there are multiple conforming solutions, printing any of them is accepted.


Sample Input 1

4
-?x?
?-o?
ox-?
???-

Sample Output 1

Yes
-xxx
o-oo
ox-o
oxx-

For example, it is consistent with the preliminary results to assume that the strengths of people 1,2,3, and 4 are 1,4,3, and 2, respectively.

Another possible assignment is:

Yes
-xxx
o-ox
ox-x
ooo-

Sample Input 2

4
-oxo
x-xx
oo-o
xox-

Sample Output 2

Yes
-oxo
x-xx
oo-o
xox-

There may be no ?.


Sample Input 3

3
-ox
x-o
ox-

Sample Output 3

No

Any assignment of strengths of people 1,2, and 3 is inconsistent with the preliminary results.


Sample Input 4

6
-x?x??
o-o??o
?x-???
o??-o?
???x-o
?x??x-

Sample Output 4

Yes
-xxxxx
o-oxxo
ox-xxx
ooo-oo
ooox-o
oxoxx-
M - Inversion Number of Large Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N2 つの数列 A=(A_1,\ldots,A_N), B=(B_1,\ldots,B_N) が与えられます。

数列 C を、空の数列から始めて、i=1,2,\ldots,N の順に、C の末尾に B_iA_i 個追加することで得られる長さ \sum_{i=1}^N A_i の数列とします。

C の転倒数を求めてください。

転倒数とは 数列 C = (C_1, C_2, \dots, C_M) の転倒数とは、1 \leq i < j \leq M かつ C_i > C_j を満たす添字の組 (i, j) の個数のことです。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^3
  • 1 \leq B_i \leq 10^9
  • 入力は全て整数である

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

出力

答えを出力せよ。


入力例 1

3
4 1 2
10 30 20

出力例 1

2

数列 C(10,10,10,10,30,20,20) となります。転倒数は 2 です。


入力例 2

4
1000 1000 1000 1000
1 1 1 1

出力例 2

0

数列 C の転倒数は 0 です。


入力例 3

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

出力例 3

499

Problem Statement

You are given two sequences A=(A_1,\ldots,A_N) and B=(B_1,\ldots,B_N) of length N.

Define a sequence C as the one obtained by starting with an empty sequence and appending A_i copies of B_i, resulting in a length of \sum_{i=1}^N A_i.

Find the inversion number of C.

What is inversion number? The inversion number of the sequence C = (C_1, C_2, \dots, C_M) is defined as the number of pairs of indices (i, j) such that 1 \leq i < j \leq M and C_i > C_j.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^3
  • 1 \leq B_i \leq 10^9
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

Output

Print the answer.


Sample Input 1

3
4 1 2
10 30 20

Sample Output 1

2

The sequence C is (10,10,10,10,30,20,20), whose inversion number is 2.


Sample Input 2

4
1000 1000 1000 1000
1 1 1 1

Sample Output 2

0

The inversion number of C is 0.


Sample Input 3

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

Sample Output 3

499
N - Shortest Container

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の英小文字からなる文字列 S が与えられます。

S を部分文字列として K 箇所含むような英小文字からなる文字列のうち長さが最小の文字列の長さを求めてください。

ただし、部分文字列とは連続する部分列のことを指します。例えば、xxxyxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq K \leq 10^9
  • S は英小文字からなる長さ N の文字列である

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

7 2
strings

出力例 1

13

strings を部分文字列として 2 箇所含むような文字列には stringstrings があり、長さは 13 です。また、長さ 12 以下の文字列で条件を満たすものは存在しないため、答えは 13 です。


入力例 2

5 30
abcab

出力例 2

92

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Find the minimum length of a string consisting of lowercase English letters that contains S as a substring at least K times.

Here, a substring refers to a (consecutive) subarray. For example, xxx is a substring of yxxxy, but not of xxyxx.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq K \leq 10^9
  • S is a string of length N consisting of lowercase English letters.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

7 2
strings

Sample Output 1

13

The string stringstrings of length 13 contains strings as a substring twice. No string of length 12 or less satisfies the condition, so the answer is 13.


Sample Input 2

5 30
abcab

Sample Output 2

92
O - Maximum Range Sum

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N) が与えられます。

今から Q 個のクエリが与えられるので、順に処理してください。 それぞれのクエリは以下のいずれかの形式で与えられます:

  • 1 p x : A_px に変更する。
  • 2 p x : B_px に変更する。
  • 3 l r x : l\leq i\leq j\leq r かつ A_i+A_{i+1}+\dots+A_j\equiv x \pmod 3 を満たすような整数の組 (i,j) が存在するか判定し、 存在するならばそのような (i,j) における B_i+B_{i+1}+\dots+B_j の最大値を出力する。

制約

  • 1\leq N,Q \leq 2\times 10^5
  • 0\leq A_i \leq 2
  • -10^9\leq B_i \leq 10^9
  • 1 種類目のクエリについて、
    • 1\leq p \leq N
    • 0\leq x \leq 2
  • 2 種類目のクエリについて、
    • 1\leq p \leq N
    • -10^9\leq x \leq 10^9
  • 3 種類目のクエリについて、
    • 1\leq l\leq r \leq N
    • 0\leq x \leq 2
  • 入力は全て整数

入力

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

N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

ここで、\mathrm{Query}_k\ (1\leq k\leq Q)k 個目のクエリを表し、以下のいずれかである:

1 p x
2 p x
3 l r x

出力

3 種類目のクエリの個数を c として、c 行出力せよ。 k\ (1\leq k \leq c) 行目には、3 種類目のクエリのうち k 個目のものにおいて、条件を満たす整数の組 (i,j) が存在するならばそのような (i,j) における B_i+B_{i+1}+\dots+B_j の最大値を、存在しないならば No を出力せよ。


入力例 1

3 5
0 1 2
3 12 -5
3 1 3 0
1 3 0
3 2 3 1
2 3 2
3 2 3 1

出力例 1

10
12
14
  • 1 個目のクエリ:1\leq i\leq j\leq 3 かつ A_i+A_{i+1}+\dots+A_j\equiv 0 \pmod 3 を満たす整数の組 (i,j)(1,1),(1,3),(2,3)3 つである。 B_i+B_{i+1}+\dots+B_j の値はそれぞれ 3,10,7 なので、最大値である 10 を出力する。
  • 2 個目のクエリ:A_30 に変更する。
  • 3 個目のクエリ:2\leq i\leq j\leq 3 かつ A_i+A_{i+1}+\dots+A_j\equiv 1 \pmod 3 を満たす整数の組 (i,j)(2,2),(2,3)2 つである。 B_i+B_{i+1}+\dots+B_j の値はそれぞれ 12,7 なので、最大値である 12 を出力する。
  • 4 個目のクエリ:B_32 に変更する。
  • 5 個目のクエリ:2\leq i\leq j\leq 3 かつ A_i+A_{i+1}+\dots+A_j\equiv 1 \pmod 3 を満たす整数の組 (i,j)(2,2),(2,3)2 つである。 B_i+B_{i+1}+\dots+B_j の値はそれぞれ 12,14 なので、最大値である 14 を出力する。

入力例 2

1 3
2
100
3 1 1 0
3 1 1 1
3 1 1 2

出力例 2

No
No
100

1 個目のクエリについて、1\leq i\leq j\leq 1 かつ A_i+A_{i+1}+\dots+A_j\equiv 0 \pmod 3 を満たす整数の組 (i,j) は存在しません。


入力例 3

1 1
2
100
1 1 0

出力例 3


3 種類目のクエリが一つもないこともあります。


入力例 4

10 10
1 1 2 0 0 2 0 2 0 0
-52 -52 14 -11 -2 -54 46 8 26 -17
3 1 9 2
2 8 71
1 10 1
2 9 86
1 5 2
3 7 7 0
3 4 5 0
2 10 100
2 1 -95
1 1 1

出力例 4

80
46
-11

Problem Statement

You are given two sequences A=(A_1,\ldots,A_N) and B=(B_1,\ldots,B_N) of length N.

Process the given Q queries in order. Each query is of one of the following kinds:

  • 1 p x: set A_p to x.
  • 2 p x: set B_p to x.
  • 3 l r x: determine if there exists an integer pair (i,j) such that l\leq i\leq j\leq r and A_i+A_{i+1}+\dots+A_j\equiv x \pmod 3. If it exists, print the maximum value of B_i+B_{i+1}+\dots+B_j among such (i,j).

Constraints

  • 1\leq N,Q \leq 2\times 10^5
  • 0\leq A_i \leq 2
  • -10^9\leq B_i \leq 10^9
  • For every query of the first kind:
    • 1\leq p \leq N
    • 0\leq x \leq 2
  • For every query of the second kind:
    • 1\leq p \leq N
    • -10^9\leq x \leq 10^9
  • For every query of the third kind:
    • 1\leq l\leq r \leq N
    • 0\leq x \leq 2
  • All input values are integers.

Input

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

N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{Query}_1
\mathrm{Query}_2
\vdots
\mathrm{Query}_Q

Here, \mathrm{Query}_k\ (1\leq k\leq Q) denotes the k-th query in one of the following format:

1 p x
2 p x
3 l r x

Output

Print c lines, where c is the number of queries of the third kind. The k-th line (1\leq k \leq c) should respond to k-th query of the third kind, containing the maximum value of B_i+B_{i+1}+\dots+B_j among the conforming pairs (i,j) if such a pair exists, or No otherwise.


Sample Input 1

3 5
0 1 2
3 12 -5
3 1 3 0
1 3 0
3 2 3 1
2 3 2
3 2 3 1

Sample Output 1

10
12
14
  • 1-st query: there are three integer pairs (i,j) such that 1\leq i\leq j\leq 3 and A_i+A_{i+1}+\dots+A_j\equiv 0 \pmod 3: namely (1,1),(1,3), and (2,3). The values of B_i+B_{i+1}+\dots+B_j are 3,10, and 7, respectively, so print the maximum value 10 among them.
  • 2-nd query: set A_3 to 0.
  • 3-rd query: there are two integer pairs (i,j) such that 2\leq i\leq j\leq 3 and A_i+A_{i+1}+\dots+A_j\equiv 1 \pmod 3: namely (2,2) and (2,3). The values of B_i+B_{i+1}+\dots+B_j are 12 and 7, respectively, so print the maximum value 12 among them.
  • 4-th query: set B_3 to 2.
  • 5-th query: there are two integer pairs (i,j) such that 2\leq i\leq j\leq 3 and A_i+A_{i+1}+\dots+A_j\equiv 1 \pmod 3: namely (2,2) and (2,3). The values of B_i+B_{i+1}+\dots+B_j are 12 and 14, respectively, so print the maximum value 14 among them.

Sample Input 2

1 3
2
100
3 1 1 0
3 1 1 1
3 1 1 2

Sample Output 2

No
No
100

For the 1-st query, there is no integer pair (i,j) such that 1\leq i\leq j\leq 1 and A_i+A_{i+1}+\dots+A_j\equiv 0 \pmod 3.


Sample Input 3

1 1
2
100
1 1 0

Sample Output 3


There may be no query of the third type.


Sample Input 4

10 10
1 1 2 0 0 2 0 2 0 0
-52 -52 14 -11 -2 -54 46 8 26 -17
3 1 9 2
2 8 71
1 10 1
2 9 86
1 5 2
3 7 7 0
3 4 5 0
2 10 100
2 1 -95
1 1 1

Sample Output 4

80
46
-11